From 4a5b6b12040a87dd8319a735cc48f31a2d140e3a Mon Sep 17 00:00:00 2001 From: jvech Date: Thu, 18 Jul 2024 21:42:38 -0500 Subject: add: linear activation added --- src/activations.c | 10 ++++++++++ src/main.c | 2 ++ tests/architectures/sine.cfg | 20 ++++++++++++++++++++ tests/main.sh | 8 ++++---- tests/plots.gpi | 21 ++++++++++++++++----- 5 files changed, 52 insertions(+), 9 deletions(-) create mode 100644 tests/architectures/sine.cfg diff --git a/src/activations.c b/src/activations.c index 7a2d6a7..064482d 100644 --- a/src/activations.c +++ b/src/activations.c @@ -28,6 +28,8 @@ double sigmoid(double x); double dsigmoid(double x); double softplus(double x); double dsoftplus(double x); +double linear(double x); +double dlinear(double x); struct Activation NN_LEAKY_RELU = { .func = leaky_relu, @@ -49,6 +51,14 @@ struct Activation NN_SIGMOID = { .dfunc = dsigmoid }; +struct Activation NN_LINEAR = { + .func = linear, + .dfunc = dlinear, +}; + +double linear(double x) {return x;} +double dlinear(double x) {return 1.0;} + double sigmoid(double x) { return 1 / (1 + exp(-x)); } double dsigmoid(double x) { return sigmoid(x) * (1 - sigmoid(x)); } diff --git a/src/main.c b/src/main.c index 8718a66..f3e6750 100644 --- a/src/main.c +++ b/src/main.c @@ -170,6 +170,7 @@ Layer * load_network(struct Configs cfg) extern struct Activation NN_SOFTPLUS; extern struct Activation NN_SIGMOID; extern struct Activation NN_LEAKY_RELU; + extern struct Activation NN_LINEAR; Layer *network = ecalloc(cfg.network_size, sizeof(Layer)); @@ -178,6 +179,7 @@ Layer * load_network(struct Configs cfg) else if (!strcmp("sigmoid", cfg.activations[i])) network[i].activation = NN_SIGMOID; else if (!strcmp("softplus", cfg.activations[i])) network[i].activation = NN_SOFTPLUS; else if (!strcmp("leaky_relu", cfg.activations[i])) network[i].activation = NN_LEAKY_RELU; + else if (!strcmp("linear", cfg.activations[i])) network[i].activation = NN_LINEAR; else die("load_network() Error: Unknown '%s' activation", cfg.activations[i]); network[i].neurons = cfg.neurons[i]; diff --git a/tests/architectures/sine.cfg b/tests/architectures/sine.cfg new file mode 100644 index 0000000..6b0c23f --- /dev/null +++ b/tests/architectures/sine.cfg @@ -0,0 +1,20 @@ +[net] +loss = square ; options (square) +epochs = 1000 ; comment +alpha = 1e-4 +weights_path = data/sine.bin +inputs = x +labels = y + +; activation options (relu, sigmoid, softplus, leaky_relu) + +[layer] +neurons=20 +activation=leaky_relu + +[layer] +neurons=20 +activation=sigmoid + +[outlayer] +activation=linear diff --git a/tests/main.sh b/tests/main.sh index 7224f79..3cec06e 100644 --- a/tests/main.sh +++ b/tests/main.sh @@ -4,7 +4,7 @@ echo '-----------------' gnuplot tests/plots.gpi echo '' -echo 'Benchmark test' -echo '-------------------' -time ./ml train -c tests/architectures/big_nn.cfg data/gauss2d.json > /dev/null -echo '' +#echo 'Benchmark test' +#echo '-------------------' +#time ./ml train -c tests/architectures/big_nn.cfg data/gauss2d.json > /dev/null +#echo '' diff --git a/tests/plots.gpi b/tests/plots.gpi index 85fbf98..4fd11b0 100644 --- a/tests/plots.gpi +++ b/tests/plots.gpi @@ -11,10 +11,12 @@ predict_cmd = "<./ml predict %s -c %s | ".sprintf(json2tsv, "-") # -- data_gauss1d = "data/gauss1d.json" data_xor = "data/xor.json" +data_sine = "data/sine.json" # -- arch_gauss1d = "tests/architectures/gauss1d.cfg" arch_xor = "tests/architectures/xor.cfg" +arch_sine = "tests/architectures/sine.cfg" set ylabel arch_gauss1d @@ -26,12 +28,21 @@ unset ylabel plot sprintf(predict_cmd, data_gauss1d, arch_gauss1d) with lines title 'network',\ "<".sprintf(json2tsv, data_gauss1d) with lines title 'original' -set ylabel arch_xor +#set ylabel arch_xor +#set logscale x +#plot sprintf(train_cmd, data_xor, arch_xor) u 2:4 with lines title 'loss' +#unset logscale +#unset ylabel +# +#set table "/dev/stdout" +#plot "<".sprintf(json2tsv, data_xor) using 1:2:3 with table,\ +# sprintf(predict_cmd, data_xor, arch_xor) using 3 with table + +set ylabel arch_sine set logscale x -plot sprintf(train_cmd, data_xor, arch_xor) u 2:4 with lines title 'loss' +plot sprintf(train_cmd, data_sine, arch_sine) u 2:4 with lines title 'loss' unset logscale unset ylabel -set table "/dev/stdout" -plot "<".sprintf(json2tsv, data_xor) using 1:2:3 with table,\ - sprintf(predict_cmd, data_xor, arch_xor) using 3 with table +plot sprintf(predict_cmd, data_sine, arch_sine) with lines title 'network',\ + "<".sprintf(json2tsv, data_sine) with lines title 'original' -- cgit v1.2.3-70-g09d2