p.238 in NNFS
Sync public mirror / sync (push) Failing after 24s

This commit is contained in:
2025-12-23 14:47:40 +01:00
parent 22d6ea5fad
commit bd2edea8ef
56 changed files with 4446 additions and 147 deletions
@@ -0,0 +1,5 @@
add_executable(dense-neural-network
main.cpp
)
target_link_libraries(dense-neural-network PRIVATE flux)
+13
View File
@@ -0,0 +1,13 @@
#!/bin/bash
set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ROOT_DIR="$(cd "$SCRIPT_DIR/../.." && pwd)"
BUILD_DIR="$ROOT_DIR/build"
if [ -d "$BUILD_DIR" ]; then
echo "Cleaning build directory: $BUILD_DIR"
rm -rf "$BUILD_DIR"/*
else
echo "No build directory to clean."
fi
+149
View File
@@ -0,0 +1,149 @@
#include "./core/omp_config.h"
#include "utils/utils.h"
#include "numerics/numerics.h"
#include "decomp/decomp.h"
#include "modules/neural_networks/neural_networks.h"
//#include <iostream>
//#include <stdexcept>
//#include <chrono>
int main(int argc, char const *argv[])
{
utils::Mf X(10,2, 0);
utils::Matrix<int64_t> y(10,1, 0);
utils::Vector<int64_t> class_targets;
float loss;
float accuracy;
//neural_networks::create_spital_data<float, uint64_t>(10000, 3, X, y);
neural_networks::create_vertical_data<float, int64_t>(100, 3, X, y);
neural_networks::Dense_Layer<float> dense1(2, 3);
neural_networks::Activation_ReLU<float> activation1;
neural_networks::Dense_Layer<float> dense2(3, 3);
neural_networks::Activation_Softmax<float> activation2;
neural_networks::Loss_CategoricalCrossentrophy<float, int64_t> loss_funtion;
float lowest_loss = 9999999;
utils::Mf best_dense_1_weights = dense1.weights;
utils::Vf best_dense_1_biases = dense1.biases;
utils::Mf best_dense_2_weights = dense2.weights;
utils::Vf best_dense_2_biases = dense2.biases;
utils::Vf vectRND;
utils::Vector<int64_t> predections;
for (uint64_t i = 0; i < 10; ++i){
// Generate a new set of weights for iteration
numerics::inplace_matrandom_mul(dense1.weights,0.98f, 1.02f);
numerics::inplace_vecrandom_mul(dense1.biases,0.98f, 1.02f);
numerics::inplace_matrandom_mul(dense2.weights,0.98f, 1.02f);
numerics::inplace_vecrandom_mul(dense2.biases,0.98f, 1.02f);
// Perform a forward pass of the training data through this layer
dense1.forward(X);
activation1.forward(dense1.outputs);
dense2.forward(activation1.outputs);
activation2.forward(dense2.outputs);
// Perform a farward pass through activation function
// it takes the output of the second dense layer here and returns loss
loss = loss_funtion.calculate(activation2.outputs, y);
predections = numerics::matargmax_row<int64_t, float>(activation2.outputs);
if (y.cols() < 1){
class_targets = numerics::matargmax_row<int64_t, int64_t>(y);
}else{
class_targets = y.get_col(0);
}
accuracy = numerics::vecmean_equal<float>(predections, class_targets);
if (loss < lowest_loss){
//std::cout << "New set of weights found, iteration:" << i << ", loss:" << loss << ", acc:" << accuracy << std::endl;
best_dense_1_weights = dense1.weights;
best_dense_1_biases = dense1.biases;
best_dense_2_weights = dense2.weights;
best_dense_2_biases = dense2.biases;
lowest_loss = loss;
} else{
//std::cout << "HERE" << std::endl;
dense1.weights = best_dense_1_weights;
dense1.biases = best_dense_1_biases;
dense2.weights = best_dense_2_weights;
dense2.biases = best_dense_2_biases;
}
}
//std::cout << loss << std::endl;
//std::cout << accuracy << std::endl;
utils::Matrix<float> softmax_outputs{{0.7, 0.1, 0.2},
{0.1, 0.5, 0.4},
{0.02, 0.9, 0.08}};
utils::Matrix<int64_t> clas_targets{{0},{1},{1}};
neural_networks::Activation_Softmax_Loss_CategoricalCrossentropy<float, int64_t> softmax_loss;
softmax_loss.backward(softmax_outputs, clas_targets);
utils::Matrix<float> dvalues1 = softmax_loss.dinputs;
neural_networks::Activation_Softmax<float> activation;
activation.outputs = softmax_outputs;
//neural_networks::Loss_CategoricalCrossentrophy<float, int64_t> loss;
dvalues1.print();
/*
utils::Vd a = utils::linspace<double>(1, 10, 10, true);
a.print();
mesh::Mesh1D<double> mesh(a);
mesh.generate_vertices(0.5, 10.5);
double Gamma = 1.0;
utils::Md A;
utils::Vd b, s(10,1);
core::Configs<double>& cfg = core::Configs<double>::defaults();
cfg.grid = core::GridKind::Uniform;
cfg.left = {core::FDKind::Forward, core::BCKind::Neumann, 0.0};
cfg.right = {core::FDKind::Backward, core::BCKind::Neumann, 0.0};
cfg.solver = core::SolverKind::LU;
fluids::Diffusion1D<double> diffusion(cfg, mesh, Gamma);
diffusion.assemble(A, b, s);
*/
return 0;
}
+27
View File
@@ -0,0 +1,27 @@
#!/bin/bash
set -e
TARGET="dense-neural-network"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ROOT_DIR="$(cd "$SCRIPT_DIR/../.." && pwd)"
BUILD_DIR="$ROOT_DIR/build"
mkdir -p "$BUILD_DIR"
cd "$BUILD_DIR"
cmake .. #-DCMAKE_BUILD_TYPE=Debug
cmake --build . --target "$TARGET"
# Load omp.cfg
if [ -f "$ROOT_DIR/omp.cfg" ]; then
export $(grep -v '^[[:space:]]*#' "$ROOT_DIR/omp.cfg" | grep -v '^[[:space:]]*$' | xargs)
fi
echo "=== CPU / OpenMP info ==="
echo "System cores (nproc): $(nproc)"
echo "OMP_NUM_THREADS=${OMP_NUM_THREADS:-"(not set)"}"
echo "========================="
"$BUILD_DIR/bin/$TARGET"