aboutsummaryrefslogtreecommitdiff
path: root/src/nn.c
diff options
context:
space:
mode:
authorjvech <jmvalenciae@unal.edu.co>2023-08-02 15:11:27 -0500
committerjvech <jmvalenciae@unal.edu.co>2023-08-02 15:11:27 -0500
commit2d84b6e5a34fd8fbc62a96c4842665701ab4e9bd (patch)
tree37e03c0aeb45ca1e6f5f6b52f8497e6ec29549c5 /src/nn.c
parent525f8398c58cc2ca7f92c416df880068c62abbd5 (diff)
add: delta calculation for hidden and output layer done
Diffstat (limited to 'src/nn.c')
-rw-r--r--src/nn.c35
1 files changed, 35 insertions, 0 deletions
diff --git a/src/nn.c b/src/nn.c
index 14120a5..6aa7213 100644
--- a/src/nn.c
+++ b/src/nn.c
@@ -2,6 +2,41 @@
static void fill_random_weights(double *weights, double *bias, size_t rows, size_t cols);
+void nn_layer_hidden_delta(
+ double *delta, size_t delta_cols,
+ double *delta_next, size_t delta_next_cols,
+ double *weigths_next, size_t weigths_shape[2],
+ double *zout, size_t zout_cols,
+ double (*activation_derivative)(double))
+{
+ assert(delta_cols == zout_cols);
+ assert(delta_cols == weigths_shape[0]);
+ assert(delta_next_cols == weigths_shape[1]);
+
+ for (size_t j = 0; j < delta_cols; j++) {
+ double sum = 0;
+ for (size_t k = 0; k < delta_next_cols; k++) {
+ size_t index = j * delta_cols + k;
+ sum += delta_next[k] * weigths_next[index];
+ }
+ delta[j] = sum * activation_derivative(zout[j]);
+ }
+}
+
+void nn_layer_out_delta(
+ double *delta, size_t delta_cols,
+ double *error, size_t error_cols,
+ double *zout, size_t zout_cols,
+ double (*activation_derivative)(double))
+{
+ assert(delta_cols == error_cols);
+ assert(zout_cols == error_cols);
+
+ for (size_t i = 0; i < delta_cols; i++) {
+ delta[i] = error[i] * activation_derivative(zout[i]);
+ }
+}
+
void nn_forward(
double **out, double **zout,
double *X, size_t X_shape[2],
Feel free to download, copy and edit any repo