Skip to content

Commit

Permalink
Added print functions for debugging purposes
Browse files Browse the repository at this point in the history
  • Loading branch information
iWas-Coder committed Nov 9, 2024
1 parent 0834195 commit 3e76536
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
4 changes: 4 additions & 0 deletions carbon.h
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ CARBON_API void carbon_assert_abort(const char *expr, const char *file, u32 line
#define CARBON_MAT_AT(m, i, j) (m).items[(i) * (m).cols + (j)]
#define CARBON_ROW_AT(i, j) (i).items[(j)]
#define CARBON_MAT_PRINT(m) carbon_math_mat_print(m, #m)
#define CARBON_ROW_PRINT(r) carbon_math_row_print(r, #r)

typedef union {
f32 items[2];
Expand Down Expand Up @@ -286,6 +287,7 @@ CARBON_API CBN_Row carbon_math_row_slice(CBN_Row r, usz i, usz cols);
CARBON_API void carbon_math_row_fill(CBN_Row r, f32 x);
CARBON_API void carbon_math_row_rand(CBN_Row r, f32 min, f32 max);
CARBON_API void carbon_math_row_copy(CBN_Row dst, CBN_Row src);
CARBON_API void carbon_math_row_print(CBN_Row r, const char *name);

#ifdef __cplusplus
CBN_Vec2 operator+(const CBN_Vec2 &u, const CBN_Vec2 &v);
Expand Down Expand Up @@ -511,6 +513,7 @@ CARBON_API u8 carbon_strlist_contains(CBN_StrList *sl, const char *s);
#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])
#define CARBON_NN_SIGMOID_DX(x) ((x) * (1 - (x)))
#define CARBON_NN_PRINT(nn) carbon_nn_print(nn, #nn)

typedef struct {
usz *arch;
Expand All @@ -529,6 +532,7 @@ CARBON_API void carbon_nn_forward(CBN_NeuralNet nn);
CARBON_API f32 carbon_nn_cost(CBN_NeuralNet nn, CBN_Matrix m);
CARBON_API CBN_NeuralNet carbon_nn_backprop(CBN_NeuralNet nn, CBN_Matrix m);
CARBON_API void carbon_nn_learn(CBN_NeuralNet nn, CBN_NeuralNet g, f32 lr);
CARBON_API void carbon_nn_print(CBN_NeuralNet nn, const char *name);

/*
** $$=========================$$
Expand Down
4 changes: 4 additions & 0 deletions src/carbon_math.c
Original file line number Diff line number Diff line change
Expand Up @@ -271,3 +271,7 @@ void carbon_math_row_rand(CBN_Row r, f32 min, f32 max) {
void carbon_math_row_copy(CBN_Row dst, CBN_Row src) {
carbon_math_mat_copy(carbon_math_row_to_mat(dst), carbon_math_row_to_mat(src));
}

void carbon_math_row_print(CBN_Row r, const char *name) {
carbon_math_mat_print(carbon_math_row_to_mat(r), name);
}
9 changes: 9 additions & 0 deletions src/carbon_nn.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,12 @@ void carbon_nn_learn(CBN_NeuralNet nn, CBN_NeuralNet g, f32 lr) {
}
}
}

void carbon_nn_print(CBN_NeuralNet nn, const char *name) {
CARBON_INFO("%s = [", name);
for (usz i = 0; i < nn.arch_count - 1; ++i) {
carbon_math_mat_print(nn.ws[i], carbon_string_fmt("ws%zu", i));
carbon_math_row_print(nn.bs[i], carbon_string_fmt("bs%zu", i));
}
CARBON_INFO("];");
}

0 comments on commit 3e76536

Please sign in to comment.