fixed to compile
This commit is contained in:
parent
963eef1d33
commit
30317791e4
2 changed files with 45 additions and 44 deletions
|
|
@ -3,5 +3,5 @@ project(c_net C)
|
||||||
|
|
||||||
set(CMAKE_C_STANDARD 11)
|
set(CMAKE_C_STANDARD 11)
|
||||||
|
|
||||||
add_executable(c_net main.c matrix.c image.c)
|
add_executable(c_net main.c matrix.c image.c neuronal_network.c)
|
||||||
target_link_libraries(c_net m)
|
target_link_libraries(c_net m)
|
||||||
|
|
|
||||||
|
|
@ -4,20 +4,20 @@
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
|
||||||
Neural_Network* new_network(int input_size, int hidden_size, int output_size, double learning_rate){
|
Neural_Network* new_network(int input_size, int hidden_size, int output_size, double learning_rate){
|
||||||
Neural_Network network = malloc(sizeof(Neural_Network));
|
Neural_Network *network = malloc(sizeof(Neural_Network));
|
||||||
// initialize networks variables
|
// initialize networks variables
|
||||||
network.input_size = input_size;
|
network->hidden_size = hidden_size;
|
||||||
network.hidden_size = hidden_size;
|
network->input_size = input_size;
|
||||||
network.output_size = output_size;
|
network->output_size = output_size;
|
||||||
network.learning_rate = learning_rate;
|
network->learning_rate = learning_rate;
|
||||||
|
|
||||||
network.weights_1 = matrix_randomize(matrix_create(hidden_size, input_size));
|
network->weights_1 = matrix_create(hidden_size, input_size);
|
||||||
network.weights_2 = matrix_randomize(matrix_create(hidden_size, hidden_size));
|
network->weights_2 = matrix_create(hidden_size, hidden_size);
|
||||||
network.weights_3 = matrix_randomize(matrix_create(hidden_size, hidden_size));
|
network->weights_3 = matrix_create(hidden_size, hidden_size);
|
||||||
network.weights_output = matrix_randomize(matrix_create(output_size, hidden_size));
|
network->weights_output = matrix_create(output_size, hidden_size);
|
||||||
network.bias_1 = matrix_randomize(matrix_create(hidden_size, 1));
|
network->bias_1 = matrix_create(hidden_size, 1);
|
||||||
network.bias_2 = matrix_randomize(matrix_create(hidden_size, 1));
|
network->bias_2 = matrix_create(hidden_size, 1);
|
||||||
network.bias_3 = matrix_randomize(matrix_create(hidden_size, 1));
|
network->bias_3 = matrix_create(hidden_size, 1);
|
||||||
//network.bias_output = matrix_create(output_size, 1); // do we need it?
|
//network.bias_output = matrix_create(output_size, 1); // do we need it?
|
||||||
|
|
||||||
return network;
|
return network;
|
||||||
|
|
@ -60,7 +60,7 @@ void save_network(Neural_Network* network) {
|
||||||
fprintf(save_file, "%d\n", network->output_size);
|
fprintf(save_file, "%d\n", network->output_size);
|
||||||
|
|
||||||
// close the file
|
// close the file
|
||||||
fclose(file_name);
|
fclose(save_file);
|
||||||
|
|
||||||
// save first layer
|
// save first layer
|
||||||
matrix_save(network->bias_1, file_name);
|
matrix_save(network->bias_1, file_name);
|
||||||
|
|
@ -81,40 +81,41 @@ void save_network(Neural_Network* network) {
|
||||||
}
|
}
|
||||||
|
|
||||||
Neural_Network* load_network(char* file) {
|
Neural_Network* load_network(char* file) {
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
double predict_images(Neural_Network* network, Image** images, int amount) {
|
//double predict_images(Neural_Network* network, Image** images, int amount) {
|
||||||
int num_correct = 0;
|
// int num_correct = 0;
|
||||||
for (int i = 0; i < amount; i++) {
|
// for (int i = 0; i < amount; i++) {
|
||||||
Matrix* prediction = predict_image(network, images[i]);
|
// Matrix* prediction = predict_image(network, images[i]);
|
||||||
if (matrix_argmax(prediction) == images[i]->image_label) {
|
// if (matrix_argmax(prediction) == images[i]->label) {
|
||||||
num_correct++;
|
// num_correct++;
|
||||||
}
|
// }
|
||||||
matrix_free(prediction);
|
// matrix_free(prediction);
|
||||||
}
|
// }
|
||||||
return 1.0 * num_correct / amount;
|
// return 1.0 * num_correct / amount;
|
||||||
}
|
//}
|
||||||
Matrix* predict_image(Neural_Network* network, Image*);
|
|
||||||
|
|
||||||
Matrix* predict(Neural_Network* network, Matrix* image_data) {
|
//Matrix* predict_image(Neural_Network* network, Image*);
|
||||||
Matrix* hidden1_outputs = apply(relu, add(dot(network->weights_1, image_data), network->bias_1));
|
|
||||||
|
|
||||||
Matrix* hidden2_outputs = apply(relu, add(dot(network->weights_2, hidden1_outputs), network->bias_2));
|
//Matrix* predict(Neural_Network* network, Matrix* image_data) {
|
||||||
|
// Matrix* hidden1_outputs = apply(relu, add(dot(network->weights_1, image_data), network->bias_1));
|
||||||
Matrix* hidden3_outputs = apply(relu, add(dot(network->weights_3, hidden2_outputs), network->bias_3));
|
//
|
||||||
|
// Matrix* hidden2_outputs = apply(relu, add(dot(network->weights_2, hidden1_outputs), network->bias_2));
|
||||||
Matrix* final_outputs = apply(relu, dot(network->weights_output, hidden3_outputs));
|
//
|
||||||
|
// Matrix* hidden3_outputs = apply(relu, add(dot(network->weights_3, hidden2_outputs), network->bias_3));
|
||||||
Matrix* result = softmax(final_outputs);
|
//
|
||||||
|
// Matrix* final_outputs = apply(relu, dot(network->weights_output, hidden3_outputs));
|
||||||
matrix_free(hidden1_outputs);
|
//
|
||||||
matrix_free(hidden2_outputs);
|
// Matrix* result = softmax(final_outputs);
|
||||||
matrix_free(hidden3_outputs);
|
//
|
||||||
matrix_free(final_outputs);
|
// matrix_free(hidden1_outputs);
|
||||||
|
// matrix_free(hidden2_outputs);
|
||||||
return result;
|
// matrix_free(hidden3_outputs);
|
||||||
}
|
// matrix_free(final_outputs);
|
||||||
|
//
|
||||||
|
// return result;
|
||||||
|
//}
|
||||||
|
|
||||||
void train_network(Neural_Network* network, Matrix* input, Matrix* output);
|
void train_network(Neural_Network* network, Matrix* input, Matrix* output);
|
||||||
void batch_train_network(Neural_Network* network, Image** images, int size);
|
void batch_train_network(Neural_Network* network, Image** images, int size);
|
||||||
Reference in a new issue