diff options
-rw-r--r-- | .gitignore | 3 | ||||
-rw-r--r-- | Makefile | 31 | ||||
-rw-r--r-- | README.md | 2 | ||||
-rw-r--r-- | src/json.c | 21 | ||||
-rw-r--r-- | src/json.h | 16 | ||||
-rw-r--r-- | src/main.c | 80 | ||||
-rw-r--r-- | src/neural.h | 12 |
7 files changed, 165 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..100c496 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +data/ +tags +ml diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..cd16efd --- /dev/null +++ b/Makefile @@ -0,0 +1,31 @@ +CC = gcc +CFLAGS = -std=c11 -Wall -g +BIN = ml +OBJDIR = objs +SRC = $(wildcard src/*.c) +HEADERS = $(wildcard src/*.h) +OBJS = $(SRC:src/%.c=${OBJDIR}/%.o) +DLIBS = -ljson-c +.PHONY: clean all run + +all: build + +$(OBJS): | $(OBJDIR) + +$(OBJDIR): + mkdir ${OBJDIR} + +$(OBJDIR)/%.o: src/%.c $(HEADERS) + ${CC} -c -o $@ $< ${CFLAGS} + +build: $(OBJS) + ${CC} ${DLIBS} -o ${BIN} ${OBJS} + +run: build + ./${BIN} + +debug: $(BIN) + gdb $< --tui + +clean: + @rm $(OBJS) $(OBJDIR) -rv diff --git a/README.md b/README.md new file mode 100644 index 0000000..e11a0b9 --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# A neural network built from scratch + diff --git a/src/json.c b/src/json.c new file mode 100644 index 0000000..8bf3ed7 --- /dev/null +++ b/src/json.c @@ -0,0 +1,21 @@ +#include "json.h" + +static void fill_item(HouseItem *item, char *key_buffer, char *value_buffer); +static void fill_buffer(FILE *fp, char *key_buffer, char *value_buffer); + +void json_read(const char *filepath, HouseItem *out) +{ + FILE *fp; + fp = fopen(filepath, "r"); + if (fp == NULL) { + perror("json_read() Error"); + exit(1); + } + + char c, key_buffer[1024], value_buffer[1024]; + size_t out_size = 0; + while ((c = fgetc(fp)) != EOF) { + switch (c) { + } + } +} diff --git a/src/json.h b/src/json.h new file mode 100644 index 0000000..23cab9a --- /dev/null +++ b/src/json.h @@ -0,0 +1,16 @@ +#ifndef __JSON +#define __JSON +#include <stdio.h> +#include <stdlib.h> +#include <assert.h> +#include <string.h> + +typedef struct { + float price; + float area; + float latitude; + float longitude; +} HouseItem; + +void json_read(const char *filepath, HouseItem *out); +#endif diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..8c82260 --- /dev/null +++ b/src/main.c @@ -0,0 +1,80 @@ +#include <stdio.h> +#include <stdint.h> +#include <string.h> +#include <json-c/json.h> + +#include "neural.h" + +typedef struct Array { + double *data; + size_t shape[2]; +} Array; + +static Array json_read(const char *filepath); + +Array json_read(const char *filepath) +{ + Array out; + FILE *fp; + char *fp_buffer; + size_t ret; + int64_t fp_size; + + fp = fopen(filepath, "r"); + if (fp == NULL) goto json_read_error; + + ret = (size_t)fseek(fp, 0L, SEEK_END); + if ((int)ret == -1) goto json_read_error; + + fp_size = ftell(fp); + if (fp_size == -1) goto json_read_error; + rewind(fp); + + fp_buffer = calloc(sizeof(char), fp_size); + if (fp_buffer == NULL) goto json_read_error; + + ret = fread(fp_buffer, sizeof(char), (size_t)fp_size, fp); + if (ret != (size_t)fp_size) { + fprintf(stderr, "json_read() Error: fread bytes '%zd' does not match with buffer size '%zd'", ret, (size_t)fp_size); + exit(1); + } + + + json_object *json_obj; + json_obj = json_tokener_parse(fp_buffer); + size_t json_obj_length = json_object_array_length(json_obj); + + out.shape[0] = (size_t)json_obj_length; + out.shape[1] = 4; + out.data = calloc(out.shape[0] * out.shape[1], sizeof(out.data[0])); + + for (int i = 0; i < json_object_array_length(json_obj); i++) { + json_object *item = json_object_array_get_idx(json_obj, i); + out.data[4*i] = json_object_get_double(json_object_object_get(item, "area")); + out.data[4*i + 1] = json_object_get_double(json_object_object_get(item, "longitude")); + out.data[4*i + 2] = json_object_get_double(json_object_object_get(item, "latitude")); + out.data[4*i + 3] = json_object_get_double(json_object_object_get(item, "price")); + } + + json_object_put(json_obj); + fclose(fp); + return out; + +json_read_error: + perror("json_read() Error"); + exit(1); +} + +int main(void) { + Array json_data = json_read("data/housing_rent.json"); + printf("area\tlong\tlat\tprice\n"); + for (int i = 0; i < json_data.shape[0]; i++) { + printf("%3.1lf\t%3.2lf\t%3.2lf\t%lf\n", + json_data.data[4*i], + json_data.data[4*i + 1], + json_data.data[4*i + 2], + json_data.data[4*i + 3]); + } + free(json_data.data); + return 0; +} diff --git a/src/neural.h b/src/neural.h new file mode 100644 index 0000000..4456907 --- /dev/null +++ b/src/neural.h @@ -0,0 +1,12 @@ +#ifndef __NEURAL__ +#define __NEURAL__ + +#include <stdlib.h> +#include <stdint.h> + +typedef struct Layer { + double *weights; + double (*activation)(double x); + size_t neurons; +} Layer; +#endif |