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:
@@ -1,13 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
#include "./core/omp_config.h"
|
||||
#include "core/omp_config.h"
|
||||
|
||||
#include "./utils/vector.h"
|
||||
#include "./utils/matrix.h"
|
||||
#include "utils/vector.h"
|
||||
#include "utils/matrix.h"
|
||||
|
||||
#include "numerics/vecmean.h"
|
||||
#include "numerics/matabs.h"
|
||||
#include "numerics/matmean.h"
|
||||
#include "numerics/mean.h"
|
||||
#include "numerics/abs.h"
|
||||
|
||||
namespace neural_networks{
|
||||
|
||||
@@ -28,7 +27,7 @@ namespace neural_networks{
|
||||
sample_losses = forward(output, y);
|
||||
|
||||
// Calculate mean loss
|
||||
data_loss = numerics::vecmean(sample_losses);
|
||||
data_loss = numerics::mean(sample_losses);
|
||||
|
||||
return data_loss;
|
||||
|
||||
@@ -42,12 +41,12 @@ namespace neural_networks{
|
||||
// L1 regularization - weights
|
||||
// calculate only when factor greater than 0
|
||||
if (layer.weight_regularizer_l1){
|
||||
regularization_losss += layer.weight_regularizer_l1 * numerics::matsum_coeff(numerics::matabs(layer.weights));
|
||||
regularization_losss += layer.weight_regularizer_l1 * numerics::sum(numerics::abs(layer.weights));
|
||||
}
|
||||
|
||||
// L2 regularization - weights
|
||||
if (layer.weight_regularizer_l2){
|
||||
regularization_losss += layer.weight_regularizer_l2 * numerics::matsum_coeff(numerics::matmul(layer.weights,layer.weights)); // elementwise!
|
||||
regularization_losss += layer.weight_regularizer_l2 * numerics::sum(numerics::mul(layer.weights,layer.weights)); // elementwise!
|
||||
}
|
||||
|
||||
// L1 regularization - biases
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
#pragma once
|
||||
|
||||
#include "./core/omp_config.h"
|
||||
#include "core/omp_config.h"
|
||||
|
||||
#include "./utils/vector.h"
|
||||
#include "./utils/matrix.h"
|
||||
#include "./utils/matcast.h"
|
||||
#include "utils/vector.h"
|
||||
#include "utils/matrix.h"
|
||||
#include "utils/matcast.h"
|
||||
|
||||
#include "./numerics/matclip.h"
|
||||
#include "./numerics/veclog.h"
|
||||
#include "numerics/clip.h"
|
||||
#include "numerics/log.h"
|
||||
|
||||
#include "./Loss.h"
|
||||
#include "Loss.h"
|
||||
|
||||
|
||||
namespace neural_networks{
|
||||
@@ -30,7 +30,7 @@ namespace neural_networks{
|
||||
|
||||
// Clip data to prevent dividning by 0
|
||||
// Clip both sides to not drag mean towards any value
|
||||
utils::Matrix<Td> y_pred_clipped = numerics::matclip(y_pred, Td{1e-7}, Td{1.0} - Td{1e-7});
|
||||
utils::Matrix<Td> y_pred_clipped = numerics::clip(y_pred, Td{1e-7}, Td{1.0} - Td{1e-7});
|
||||
|
||||
// Probabilities for taget values
|
||||
// only if categorical labes
|
||||
@@ -40,7 +40,8 @@ namespace neural_networks{
|
||||
correct_confidences[i] = y_pred_clipped(i, idx);
|
||||
}
|
||||
}else{ // Mask values - only for one-hot encoded labels
|
||||
correct_confidences = numerics::matdot_row(y_pred_clipped, cast_y_true);
|
||||
correct_confidences = numerics::sum_rowwise(numerics::mul(y_pred_clipped, cast_y_true));
|
||||
//correct_confidences = numerics::matdot_row(y_pred_clipped, cast_y_true);
|
||||
|
||||
}
|
||||
// Losses
|
||||
@@ -72,9 +73,12 @@ namespace neural_networks{
|
||||
|
||||
|
||||
// Calculate the gradient
|
||||
numerics::inplace_matscalar(y_temp,Ti{-1});
|
||||
dinputs = numerics::matdiv(utils::matcast<Td, Ti>(y_temp), dvalues);
|
||||
numerics::inplace_matdiv(dinputs, samples);
|
||||
//numerics::inplace_matscalar(y_temp,Ti{-1});
|
||||
y_temp = numerics::neg(y_temp);
|
||||
//dinputs = numerics::matdiv(utils::matcast<Td, Ti>(y_temp), dvalues);
|
||||
dinputs = numerics::div(utils::matcast<Td, Ti>(y_temp), dvalues);
|
||||
//numerics::inplace_matdiv(dinputs, samples);
|
||||
dinputs = numerics::div(dinputs, samples);
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user