-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add nn_layer_init_weights_gaussian and nn_layer_init_biases_zeros (#19)
- Loading branch information
Showing
10 changed files
with
132 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#include "nn_activation.h" | ||
#include "nn_dot_product.h" | ||
#include "nn_layer.h" | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <time.h> | ||
|
||
int main() { | ||
srand((unsigned int)time(NULL)); | ||
|
||
// Init vars | ||
NNLayer layer; | ||
NNError error; | ||
const int input_size = 4; | ||
const int output_size = 3; | ||
const int batch_size = 2; | ||
|
||
// Initialize a layer with the given input and output sizes, ReLU activation function, and dot product function | ||
if (!nn_layer_init(&layer, input_size, output_size, nn_activation_func_relu, nn_dot_product, &error)) { | ||
fprintf(stderr, "error: %s\n", error.message); | ||
return 1; | ||
} | ||
|
||
// Initialize the weights of the layer with Gaussian random values scaled by 0.01 | ||
if (!nn_layer_init_weights_gaussian(&layer, 0.01f, &error)) { | ||
fprintf(stderr, "error: %s\n", error.message); | ||
return 1; | ||
} | ||
|
||
// Initialize the biases of the layer to zero | ||
if (!nn_layer_init_biases_zeros(&layer, &error)) { | ||
fprintf(stderr, "error: %s\n", error.message); | ||
return 1; | ||
} | ||
|
||
// Generate random inputs | ||
float inputs[NN_LAYER_MAX_BATCH_SIZE][NN_LAYER_MAX_INPUT_SIZE]; | ||
for (size_t i = 0; i < batch_size; ++i) { | ||
for (size_t j = 0; j < input_size; ++j) { | ||
inputs[i][j] = (float)rand() / (float)RAND_MAX; | ||
} | ||
} | ||
|
||
// Compute the layer with the given inputs | ||
float outputs[NN_LAYER_MAX_BATCH_SIZE][NN_LAYER_MAX_OUTPUT_SIZE]; | ||
if (!nn_layer_forward(&layer, inputs, outputs, 2, &error)) { | ||
fprintf(stderr, "error: %s\n", error.message); | ||
return 1; | ||
} | ||
|
||
// Print the inputs | ||
for (size_t i = 0; i < batch_size; ++i) { | ||
for (size_t j = 0; j < input_size; ++j) { | ||
printf("inputs[%zu][%zu] = %f\n", i, j, inputs[i][j]); | ||
} | ||
} | ||
|
||
// Print the outputs | ||
for (size_t i = 0; i < batch_size; ++i) { | ||
for (size_t j = 0; j < output_size; ++j) { | ||
printf("outputs[%zu][%zu] = %f\n", i, j, outputs[i][j]); | ||
} | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters