Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion include/network/INetwork.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#ifndef INETWORK
#define INETWORK

#include "../src/model/optimizers.hpp"
#include "../../src/model/optimizers.hpp"
#include "IvisualNetwork.hpp"
#include "tensor.hpp"

namespace nn::model {
class INetwork {
Expand Down
23 changes: 17 additions & 6 deletions src/model/ProgressBar.cpp
Original file line number Diff line number Diff line change
@@ -1,61 +1,72 @@


#include "ProgressBar.hpp"
#include <iomanip>
#include <iostream>

namespace nn {

void ProgressBar::printBar() {
// Print header on first call
if (!headerPrinted) {
std::cout << header << "\n";
headerPrinted = true;
}

// Calculate current percentage (0-100)
int percentage = (total > 0) ? (current * 100 / total) : 0;

// Only update if percentage has changed to reduce console spam
if (percentage == last_percentage) {
return;
}
last_percentage = percentage;

// Calculate number of filled characters in the progress bar
int filled = (BAR_WIDTH * percentage) / 100;

// Render the progress bar with carriage return for in-place updates
std::cout << "\r[";
for (int i = 0; i < BAR_WIDTH; ++i) {
if (i < filled) {
std::cout << "=";
std::cout << "="; // Filled portion
} else {
std::cout << " ";
std::cout << " "; // Empty portion
}
}
std::cout << "] " << std::setw(3) << percentage << "%" << std::flush;
}

void ProgressBar::endPrint() {
current = total;
last_percentage = -1;
last_percentage = -1; // Force update
printBar();

std::cout << std::endl;
std::cout << std::endl; // Move to next line
}


ProgressBar &ProgressBar::operator++() {
++current;
if (current > total) {
current = total;
current = total; // Clamp to maximum value
}
return *this;
}


ProgressBar ProgressBar::operator++(int) {
ProgressBar temp = *this;
++(*this);
return temp;
}


ProgressBar ProgressBar::operator=(int value) {
ProgressBar temp = *this;
current = value;
if (current > total) {
current = total;
current = total; // Clamp to maximum value
}
return temp;
}
Expand Down
53 changes: 53 additions & 0 deletions src/model/ProgressBar.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
/**
* @file ProgressBar.hpp
* @brief Implementation of console progress bar for neural network training
*
* This file implements a visual progress bar that displays training progress
* in the console. It provides real-time feedback to users about training
* completion status with percentage indicators and visual progress indication.
*/

#ifndef PROGRESSBAR
#define PROGRESSBAR

Expand All @@ -21,11 +30,55 @@ class ProgressBar {
header(header_ + ": ") {}
~ProgressBar() = default;

/**
* @brief Prints the progress bar to the console
*
* Renders a visual progress bar showing current completion percentage.
* The bar is only updated when the percentage changes to avoid excessive
* console output. On first call, prints the header message.
*
* Header:
* [======== ] 75%
*/
void printBar();

/**
* @brief Completes the progress bar and moves to next line
*
* Forces the progress bar to show 100% completion and prints a newline
* to move the cursor to the next line for subsequent output.
*/
void endPrint();

/**
* @brief Pre-increment operator for advancing progress
*
* Increments the current progress counter and ensures it doesn't
* exceed the total value.
*
* @return Reference to this ProgressBar for method chaining
*/
ProgressBar &operator++();

/**
* @brief Post-increment operator for advancing progress
*
* Creates a copy of the current state, increments the progress,
* and returns the copy.
*
* @return Copy of the ProgressBar before incrementing
*/
ProgressBar operator++(int);

/**
* @brief Assignment operator for setting progress value
*
* Sets the current progress to a specific value, ensuring it doesn't
* exceed the total. Returns a copy of the previous state.
*
* @param value New progress value to set
* @return Copy of the ProgressBar before assignment
*/
ProgressBar operator=(int value);
};
} // namespace nn
Expand Down
73 changes: 6 additions & 67 deletions src/model/activations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,6 @@

namespace nn::model {

/**
* @brief Applies the activation function to a tensor
*
* This method applies the configured activation function to the input tensor
* and stores the result in the output tensor. The operation is performed
* element-wise across the entire tensor.
*
* @param net Input tensor containing pre-activation values
* @param out Output tensor to store post-activation values
*
* @throws std::invalid_argument If input and output tensors have different sizes
* @throws std::runtime_error If the activation type is unknown
*
* @note The output tensor must have the same shape as the input tensor
* @note This method automatically chooses between CPU and GPU implementations
*/
void Activation::activate(const global::Tensor &net, global::Tensor &out) const {
if (net.numElements() != out.numElements()) {
throw std::invalid_argument(
Expand Down Expand Up @@ -55,22 +39,6 @@ void Activation::activate(const global::Tensor &net, global::Tensor &out) const
}
}

/**
* @brief Applies the derivative of the activation function to a tensor
*
* This method computes the derivative of the configured activation function
* and applies it to the input tensor. This is used during backpropagation
* to compute gradients for the previous layer.
*
* @param net Input tensor containing pre-activation values
* @param out Output tensor to store derivative values (modified in-place)
*
* @throws std::invalid_argument If input and output tensors have different sizes
* @throws std::runtime_error If the activation type is unknown
*
* @note The output tensor is modified in-place (multiplied by the derivative)
* @note This method automatically chooses between CPU and GPU implementations
*/
void Activation::derivativeActivate(const nn::global::Tensor &net,
nn::global::Tensor &out) const {
if (net.numElements() != out.numElements()) {
Expand Down Expand Up @@ -105,19 +73,6 @@ void Activation::derivativeActivate(const nn::global::Tensor &net,
}
}

/**
* @brief Finds the index of the maximum element in a tensor
*
* This utility function finds the index of the element with the maximum value
* in the given tensor. It's commonly used with softmax activation for
* classification tasks.
*
* @param metrix The input tensor to search
* @return The index of the element with the maximum value
*
* @note This function works with both CPU and GPU tensors
* @note For GPU tensors, it uses optimized CUDA kernels
*/
size_t Activation::getMaxElementIndex(const global::Tensor &metrix) {
if (metrix.isGpu) {
return global::tensor_gpu::getMaxElementIndex(metrix.gpu_data,
Expand All @@ -138,10 +93,10 @@ size_t Activation::getMaxElementIndex(const global::Tensor &metrix) {

/**
* @brief Gets the maximum value in a tensor
*
*
* This utility function returns the maximum value in the given tensor.
* It's used internally by softmax for numerical stability.
*
*
* @param metrix The input tensor to search
* @return The maximum value in the tensor
*/
Expand Down Expand Up @@ -233,11 +188,11 @@ global::ValueType Activation::derivativeTanh(const global::ValueType z) {

/**
* @brief Vectorized ReLU activation
*
*
* Applies ReLU activation to all elements in the input tensor and stores
* the result in the output tensor. Uses optimized implementations for
* both CPU and GPU execution.
*
*
* @param net Input tensor containing pre-activation values
* @param out Output tensor to store post-activation values
*/
Expand All @@ -253,11 +208,11 @@ void Activation::relu(const global::Tensor &net, global::Tensor &out) {

/**
* @brief Vectorized ReLU derivative
*
*
* Applies ReLU derivative to all elements in the input tensor and multiplies
* the result with the output tensor (in-place operation). Used during
* backpropagation for gradient computation.
*
*
* @param net Input tensor containing pre-activation values
* @param out Output tensor to be modified in-place with derivative values
*/
Expand Down Expand Up @@ -342,22 +297,6 @@ void Activation::derivativeTanh(const global::Tensor &net,
}
}

/**
* @brief Vectorized Softmax activation
*
* Applies softmax activation to the input tensor, which normalizes the values
* to create a probability distribution. The softmax function is:
* softmax(x_i) = exp(x_i - max(x)) / sum(exp(x_j - max(x)))
*
* This implementation includes numerical stability measures to prevent
* overflow by subtracting the maximum value before computing exponentials.
*
* @param net Input tensor containing pre-activation values
* @param out Output tensor to store normalized probabilities
*
* @note The output values sum to 1.0 (probability distribution)
* @note Uses numerical stability tricks to prevent overflow
*/
void Activation::softmax(const global::Tensor &net, global::Tensor &out) {
if (net.isGpu) {
global::tensor_gpu::softmax(net.gpu_data, out.gpu_data,
Expand Down
Loading