diff options
-rw-r--r-- | mlp.c | 88 | ||||
-rw-r--r-- | perceptron.c | 53 |
2 files changed, 141 insertions, 0 deletions
@@ -0,0 +1,88 @@ +#include <stdio.h> +#include <stdint.h> +#include <stdlib.h> +#include <arpa/inet.h> + +float leaky_relu(float input); +float relu(float input); +void print_numbers(char *file_location); +float softmax(int num_inputs, float *inputs_list); + +struct neuron + +int main(void) +{ + char *file_location = "/home/sp1ral/Schreibtisch/MNIST/train-images.idx3-ubyte"; + print_numbers(file_location); + return 0; +} + +float leaky_relu(float input) +{ + float leak = input / 10; + float output = leak > input ? leak : input; + return output; +} + +float relu(float input) +{ + float output = input > 0 ? input : 0; + return output; +} + +void print_numbers(char *file_location) +{ + FILE *f; + f = fopen(file_location, "r"); + + int32_t magic_number, num_items; + + fread(&magic_number, 1, sizeof(magic_number), f); + fread(&num_items, 1, sizeof(num_items), f); + + magic_number = ntohl(magic_number); + num_items = ntohl(num_items); + + int32_t width_image, height_image; + + fread(&width_image, 1, sizeof(width_image), f); + fread(&height_image, 1, sizeof(height_image), f); + + width_image = ntohl(width_image); + height_image = ntohl(height_image); + + printf("Magic number -> %d\n", magic_number); + printf("Number of items -> %d\n", num_items); + printf("Width image -> %d\n", width_image); + printf("Height image -> %d\n", height_image); + printf("\n"); + + char line[28]; + char pixel; + for (int item_num = 0; item_num < num_items; item_num++) { + for (int i = 0; i < 28; i++) { + fread(&line, 1, sizeof(line), f); + for (int j = 0; j < 28; j++) { + pixel = line[j]; + if (pixel) { + printf("1"); + } else { + printf("0"); + } + } + printf("\n"); + } + printf("\n"); + } + + fclose(f); +} + +float softmax(int num_inputs, float *inputs_list) +{ + float sum; + for (int input_num = 0; input_num < num_inputs; input_num++) { + sum += inputs_list[input_num]; + } + return sum / inputs->num_inputs; +} diff --git a/perceptron.c b/perceptron.c new file mode 100644 index 0000000..0d0d35a --- /dev/null +++ b/perceptron.c @@ -0,0 +1,53 @@ +#include <stdio.h> + +// perceptron to decide if a person is friend or foe +// values range from 0 to 10 + +struct weights { + float nice; + float intelligent; + float funny; + float based; +}; + +struct person { + int nice; + int intelligent; + int funny; + int based; + char name[64]; +}; + +int perceptron(struct weights *weights, struct person *person, float activation_value); + +int main(void) +{ + struct person tim = {8, 8, 6, 10, "Tim"}; + struct person lara = {3, 3, 4, 1, "Lara"}; + struct person finja = {10, 6, 7, 8, "Finja"}; + struct person marlon = {8, 6, 6, 5, "Marlon"}; + + struct weights personal_weights = {0.7, 0.5, 0.7, 1.5}; + struct person perceptron_person = lara; + int decision; + + decision = perceptron(&personal_weights, &perceptron_person, 0.6); + if (decision) { + printf("%s is your friend\n", perceptron_person.name); + } else { + printf("%s is your foe\n", perceptron_person.name); + } +} + +int perceptron(struct weights *weights, struct person *person, float activation_value) +{ + float nice_value, intelligent_value, funny_value, based_value, general_value; + + nice_value = weights->nice / 10 * person->nice; + intelligent_value = weights->intelligent / 10 * person->intelligent; + funny_value = weights->funny / 10 * person->funny; + based_value = weights->based / 10 * person->based; + + general_value = (nice_value + intelligent_value + funny_value + based_value) / 4; + return (general_value > activation_value) ? 1 : 0; +} |