Skip to content

Commit

Permalink
Started implementation of Neural Networks
Browse files Browse the repository at this point in the history
  • Loading branch information
iWas-Coder committed Nov 7, 2024
1 parent 61f105b commit 629cd77
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
22 changes: 22 additions & 0 deletions carbon.h
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,27 @@ CARBON_API void carbon_strlist_push(CBN_StrList *sl, const char *s);
CARBON_API void carbon_strlist_pop(CBN_StrList *sl, const char *s);
CARBON_API u8 carbon_strlist_contains(CBN_StrList *sl, const char *s);

/*
** $$=======================$$
** || NeuralNet ||
** $$=======================$$
*/
#define CARBON_NN_IN(nn) (CARBON_ASSERT((nn).arch_count > 0), (nn).as[0])
#define CARBON_NN_OUT(nn) (CARBON_ASSERT((nn).arch_count > 0), (nn).as[(nn).arch_count - 1])

typedef struct {
usz *arch;
usz arch_count;
CBN_Matrix *ws;
CBN_Row *bs;
CBN_Row *as;
} CBN_NeuralNet;

CARBON_API CBN_NeuralNet carbon_nn_create(usz *arch, usz arch_count);
CARBON_API void carbon_nn_fill(CBN_NeuralNet nn, f32 x);
CARBON_API void carbon_nn_zero(CBN_NeuralNet nn);
CARBON_API void carbon_nn_rand(CBN_NeuralNet nn, f32 min, f32 max);

/*
** $$=========================$$
** || TestManager ||
Expand Down Expand Up @@ -585,6 +606,7 @@ CARBON_API void carbon_junit_output(CBN_JUnitTestsuite *junit_ts, CBN_JUnitTestc
#include "src/carbon_hashmap.c"
#include "src/carbon_string.c"
#include "src/carbon_strlist.c"
#include "src/carbon_nn.c"
#include "src/carbon_test_manager.c"
#include "src/carbon_junit.c"
#endif // CARBON_IMPLEMENTATION
Expand Down
42 changes: 42 additions & 0 deletions src/carbon_nn.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (C) Wasym A. Alonso. All Rights Reserved.

#ifndef CARBON_IMPLEMENTATION
#include <carbon.h>
#endif // CARBON_IMPLEMENTATION

CBN_NeuralNet carbon_nn_create(usz *arch, usz arch_count) {
CARBON_ASSERT(arch_count > 0);
CBN_NeuralNet nn = {
.arch = arch,
.arch_count = arch_count,
.ws = (CBN_Matrix *) CARBON_MALLOC((arch_count - 1) * sizeof(CBN_Matrix)),
.bs = (CBN_Row *) CARBON_MALLOC((arch_count - 1) * sizeof(CBN_Row)),
.as = (CBN_Row *) CARBON_MALLOC(arch_count * sizeof(CBN_Row))
};
if (!nn.ws || !nn.bs || !nn.as) {
CARBON_ERROR("failed to allocate memory");
memset(&nn, 0, sizeof(CBN_NeuralNet));
}
return nn;
}

CARBON_API void carbon_nn_fill(CBN_NeuralNet nn, f32 x) {
for (usz i = 0; i < nn.arch_count - 1; ++i) {
carbon_math_mat_fill(nn.ws[i], x);
carbon_math_row_fill(nn.bs[i], x);
carbon_math_row_fill(nn.as[i], x);
}
carbon_math_row_fill(nn.as[nn.arch_count - 1], x);
}

CARBON_API void carbon_nn_zero(CBN_NeuralNet nn) {
carbon_nn_fill(nn, 0);
}

CARBON_API void carbon_nn_rand(CBN_NeuralNet nn, f32 min, f32 max) {
for (usz i = 0; i < nn.arch_count - 1; ++i) {
carbon_math_mat_rand(nn.ws[i], min, max);
carbon_math_row_rand(nn.bs[i], min, max);
}
}

0 comments on commit 629cd77

Please sign in to comment.