Fittet new functions to everying in neural networks. Still need to optimise for uint64_t vs int64_t and vec vs mat in some places.

This commit is contained in:
2026-05-16 20:37:05 +02:00
parent 412a854c65
commit d2fe8aa65c
50 changed files with 489 additions and 1482 deletions
+45 -15
View File
@@ -1,4 +1,4 @@
#include "./core/omp_config.h"
#include "core/omp_config.h"
#include "utils/utils.h"
#include "numerics/numerics.h"
@@ -31,8 +31,8 @@ int main(int argc, char const *argv[])
float loss;
float accuracy;
utils::Vector<int64_t> class_targets;
utils::Vector<int64_t> predections;
utils::Vector<uint64_t> class_targets;
utils::Vector<uint64_t> predections;
// Create dataset
@@ -40,14 +40,23 @@ int main(int argc, char const *argv[])
//neural_networks::create_vertical_data<float, int64_t>(number_of_samples, number_of_classes, X, y);
// Create Dense layer with 2 input featues and 3 output values
neural_networks::Dense_Layer<float> dense1(2, 64);
neural_networks::Dense_Layer<float> dense1(2, 16);
// Create ReLU activation (to be used with Dense layer)
neural_networks::Activation_ReLU<float> activation1;
// Create a second Dense layer with 16 inputs (as we take the vlaues from the last layer)
// and 16 output values
neural_networks::Dense_Layer<float> dense2(16, 16);
// Create Softmax activation (to be used with Dense layer)
neural_networks::Activation_Softmax<float> activation2;
// Create a second Dense layer with 3 inputs (as we take the vlaues from the last layer)
// and 3 output values
neural_networks::Dense_Layer<float> dense2(64, number_of_classes);
neural_networks::Dense_Layer<float> dense3(16, number_of_classes);
// Create a Sfotmax classifier's combined loss and activation
neural_networks::Activation_Softmax_Loss_CategoricalCrossentropy<float, int64_t> loss_activation;
@@ -74,22 +83,33 @@ int main(int argc, char const *argv[])
// takes output of activation function of the first layer as input
dense2.forward(activation1.outputs);
// Perform a forward pass thourgh activation function
// takes the output fo the first layer here
activation2.forward(dense2.outputs);
// Perform a forward pass through second Dense layer
// takes output of activation function of the first layer as input
dense3.forward(activation2.outputs);
// Perform a foard pass through the activation/loss function
// takes the output of the second dense layer here and returns loss
loss = loss_activation.forward(dense2.outputs, y);
loss_activation.loss.regularization_loss(dense1);
// Calculate accuracy from output of activation2 and targets
predections = numerics::matargmax_row<int64_t, float>(loss_activation.outputs);
//predections = numerics::matargmax_row <int64_t, float>(loss_activation.outputs);
predections = numerics::argmax_rowwise(loss_activation.outputs);
if (y.cols() < 1){
class_targets = numerics::matargmax_row<int64_t, int64_t>(y);
class_targets = numerics::argmax_rowwise(y);
}else{
class_targets = y.get_col(0);
class_targets = utils::veccast <uint64_t, int64_t> (y.get_col(0));
}
accuracy = numerics::vecmean_equal<float>(predections, class_targets);
accuracy = numerics::mean( utils::veccast<float, uint64_t> (numerics::equal_elementwise_serial(predections, class_targets)));
if (!(epoch%100)){
@@ -123,6 +143,7 @@ int main(int argc, char const *argv[])
// Perform a forward pass of our testing data through this layer
dense1.forward(X_test);
// Perform a forward pass thourgh activation function
// takes the output fo the first layer here
activation1.forward(dense1.outputs);
@@ -131,21 +152,30 @@ int main(int argc, char const *argv[])
// takes output of activation function of the first layer as input
dense2.forward(activation1.outputs);
// Perform a forward pass thourgh activation function
// takes the output fo the first layer here
activation2.forward(dense2.outputs);
// Perform a forward pass through second Dense layer
// takes output of activation function of the first layer as input
dense3.forward(activation2.outputs);
// Perform a foard pass through the activation/loss function
// takes the output of the second dense layer here and returns loss
loss = loss_activation.forward(dense2.outputs, y);
loss = loss_activation.forward(dense3.outputs, y_test);
// Calculate accuracy from output of activation2 and targets
predections = numerics::matargmax_row<int64_t, float>(loss_activation.outputs);
predections = numerics::argmax_rowwise(loss_activation.outputs);
if (y.cols() < 1){
class_targets = numerics::matargmax_row<int64_t, int64_t>(y);
if (y.cols() == 1){
class_targets = numerics::argmax_rowwise(y_test);
}else{
class_targets = y.get_col(0);
class_targets = utils::veccast <uint64_t, int64_t> (y_test.get_col(0));
}
accuracy = numerics::vecmean_equal<float>(predections, class_targets);
accuracy = numerics::mean( utils::veccast<float, uint64_t> (numerics::equal_elementwise_serial(predections, class_targets)));
std::cout << "validation, acc: " << accuracy << ", loss: " << loss << std::endl;