Skip to content
Merged
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
14 changes: 14 additions & 0 deletions nn/include/feedforward.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once

#include "neural_network.h"

//==============================
// Forward Pass Functions
//==============================

NeuralNetwork* create_network(int num_layers);

void free_network(NeuralNetwork* nn);

// It caches the intermediate results for the backpropagation algorithm.
Matrix* feedforward(NeuralNetwork* nn, const Matrix* input);
33 changes: 33 additions & 0 deletions nn/include/neural_network.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#pragma once

#include "activation.h"
#include "cache.h"
#include "linalg.h"

//==============================
// Neural Network Layer Struct
//==============================

typedef Matrix* (*ActivationFunc)(Matrix*);

typedef struct {
Matrix* weights;
Matrix* bias;

ActivationFunc activation;

// The leak parameter for the Leaky ReLU activation function.
double leak_parameter;
} Layer;

//==============================
// Neural Network Struct
//==============================

typedef struct {
Layer** layers;
int num_layers;

// A caching mechanism to store intermediate values from the forward pass.
Cache* cache;
} NeuralNetwork;
6 changes: 6 additions & 0 deletions nn/include/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ typedef enum {
// For safe malloc allocations
#define CHECK_MALLOC(ptr, message) ASSERT((ptr) != NULL, message);

// Macro to handle memory allocation checks.
#define CHECK_ALLOC(ptr) \
if (ptr == NULL) { \
return NULL; \
}

// Function prototypes for logging
void log_message(LogLevel level, const char* format, ...);

Expand Down
96 changes: 96 additions & 0 deletions nn/src/neural_network/feedforward.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#include "feedforward.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "linalg.h"
#include "neural_network.h"
#include "utils.h"

NeuralNetwork* create_network(int num_layers) {
NeuralNetwork* nn = (NeuralNetwork*)malloc(sizeof(NeuralNetwork));
if (nn == NULL) {
LOG_ERROR("Memory allocation failed for Neural Network struct.");
return NULL;
}

nn->layers = (Layer**)malloc(sizeof(Layer*) * num_layers);
if (nn->layers == NULL) {
LOG_ERROR("Memory allocation failed for layers array.");
free(nn);
return NULL;
}

nn->num_layers = num_layers;
nn->cache = init_cache();
if (nn->cache == NULL) {
LOG_ERROR("Failed to initialize cache.");
free(nn->layers);
free(nn);
return NULL;
}
return nn;
}

void free_network(NeuralNetwork* nn) {
if (nn == NULL) {
return;
}

if (nn->layers != NULL) {
for (int i = 0; i < nn->num_layers; i++) {
if (nn->layers[i] != NULL) {
if (nn->layers[i]->weights != NULL) {
free_matrix(nn->layers[i]->weights);
}
if (nn->layers[i]->bias != NULL) {
free_matrix(nn->layers[i]->bias);
}
free(nn->layers[i]);
}
}
free(nn->layers);
}
if (nn->cache != NULL) {
clear_cache(nn->cache);
free(nn->cache);
}
free(nn);
}

Matrix* feedforward(NeuralNetwork* nn, const Matrix* input) {
ASSERT(nn != NULL, "Neural Network pointer cannot be NULL.");
ASSERT(input != NULL, "Input matrix cannot be NULL.");
ASSERT(input->rows == nn->layers[0]->weights->cols,
"Input dimensions must match network dimensions.");

Matrix* current_output = copy_matrix(input);

// Cache the input for the backpropagation algorithm.
put_matrix(nn->cache, "input", current_output);

for (int i = 0; i < nn->num_layers; i++) {
Matrix* z = dot_matrix(current_output, nn->layers[i]->weights);

add_matrix(z, nn->layers[i]->bias);

// Cache the intermediate value (z).
char z_key[32];
sprintf(z_key, "z_%d", i);
put_matrix(nn->cache, z_key, z);

Matrix* a = apply_onto_matrix(nn->layers[i]->activation, z);

// Cache the activated output (a).
char a_key[32];
sprintf(a_key, "a_%d", i);
put_matrix(nn->cache, a_key, a);

free_matrix(z);
free_matrix(current_output);
current_output = a;
}

return current_output;
}