diff --git a/image.h b/image.h index 78dff98..4054d04 100644 --- a/image.h +++ b/image.h @@ -6,10 +6,6 @@ typedef struct { char label; } Image; -typedef struct { - const Image* image; - const size_t size; -} Image_Container; static const int MAGIC_NUMBER_LABEL = 2049; diff --git a/main.c b/main.c index da6ef80..2d787c0 100644 --- a/main.c +++ b/main.c @@ -29,9 +29,7 @@ int main() { // // int pause; int imported_count = 0; - Image** images = import_images("../data/train-images.idx3-ubyte", "../data/train-labels.idx1-ubyte", &imported_count, 60000); - matrix_save(images[0]->pixel_values, "image1.txt"); - matrix_save(images[1]->pixel_values, "images2.txt"); + Image** images = import_images("../data/train-images.idx3-ubyte", "../data/train-labels.idx1-ubyte", &imported_count, 10000); Neural_Network * net = create_network(3, 28*28, 30, 10); train_network_with_batches(net, images, imported_count, 1, 10, 3); } \ No newline at end of file diff --git a/neural_net.c b/neural_net.c index 5368f96..15b4399 100644 --- a/neural_net.c +++ b/neural_net.c @@ -7,6 +7,13 @@ #include #include "image.h" +typedef struct{ + Neural_Network * network; + Matrix ** weights_delta; + Matrix ** biases_delta +}; + + void evaluate(Neural_Network * network, Image** images, int imageCount){ int numCorrect = 0; for(int i = 0; i <= imageCount; i++){ @@ -23,7 +30,7 @@ void evaluate(Neural_Network * network, Image** images, int imageCount){ } double sigmoid(double input) { - return 1.0 / (1 + exp(-1 * input)); + return 1.0 / (1 + exp(-input)); } double sigmoid_prime(double input){ @@ -62,13 +69,15 @@ void back_prop(Neural_Network * network, Image* training_sample, Matrix ** weigh //calculate delta for last layer; //bias Matrix * subtraction_result = subtract(layer_activations[network->layer_count-1], desired_result); - Matrix * delta = apply(sigmoid_prime, subtraction_result); + Matrix * s_prime = apply(sigmoid_prime, layer_activations_wo_sigmoid[network->layer_count-2]); + Matrix * delta = multiply(subtraction_result, s_prime); + matrix_free(s_prime); matrix_free(subtraction_result); - biases_delta[network->layer_count-1] = delta; + biases_delta[network->layer_count-2] = delta; //weights Matrix * transposed = transpose(layer_activations[network->layer_count-2]); - weights_delta[network->layer_count-1] = dot(delta, transposed); + weights_delta[network->layer_count-2] = dot(delta, transposed); matrix_free(transposed); transposed = NULL; @@ -146,7 +155,12 @@ void update_batch(Neural_Network * network, Image** training_data, int batch_sta matrix_free(network->biases[i]); network->biases[i] = new_biases; } - //TODO: update mini batch + free(sum_weights_delta); + free(sum_biases_delta); + for(int i = 0; i < network->layer_count - 1; i++){ + matrix_free(weights_delta[i]); + matrix_free(biases_delta[i]); + } }