tinn/Tinn.c

176 lines
4.5 KiB
C
Raw Normal View History

2018-03-29 06:41:08 +03:00
#include "Tinn.h"
#include <stdarg.h>
2018-03-31 01:42:20 +03:00
#include <stdio.h>
2018-03-29 06:41:08 +03:00
#include <stdlib.h>
#include <math.h>
2018-05-26 21:50:25 +03:00
// Computes error.
2018-04-03 23:08:27 +03:00
static float err(const float a, const float b)
2018-03-29 06:41:08 +03:00
{
2018-04-11 23:32:00 +03:00
return 0.5f * (a - b) * (a - b);
2018-03-29 06:41:08 +03:00
}
2018-04-13 07:57:49 +03:00
// Returns partial derivative of error function.
2018-04-03 23:08:27 +03:00
static float pderr(const float a, const float b)
2018-03-29 06:41:08 +03:00
{
2018-03-30 23:04:37 +03:00
return a - b;
2018-03-29 06:41:08 +03:00
}
2018-04-13 07:57:49 +03:00
// Computes total error of target to output.
2018-04-04 01:12:36 +03:00
static float toterr(const float* const tg, const float* const o, const int size)
2018-03-29 06:41:08 +03:00
{
2018-03-31 14:29:03 +03:00
float sum = 0.0f;
2018-03-30 23:04:37 +03:00
for(int i = 0; i < size; i++)
sum += err(tg[i], o[i]);
return sum;
2018-03-29 06:41:08 +03:00
}
2018-03-30 23:04:37 +03:00
// Activation function.
2018-04-03 23:08:27 +03:00
static float act(const float a)
2018-03-30 23:04:37 +03:00
{
2018-03-31 14:29:03 +03:00
return 1.0f / (1.0f + expf(-a));
2018-03-30 23:04:37 +03:00
}
2018-04-13 07:57:49 +03:00
// Returns partial derivative of activation function.
2018-04-03 23:08:27 +03:00
static float pdact(const float a)
2018-03-30 23:04:37 +03:00
{
2018-03-31 14:29:03 +03:00
return a * (1.0f - a);
2018-03-30 23:04:37 +03:00
}
2018-04-13 07:57:49 +03:00
// Returns floating point random from 0.0 - 1.0.
2018-03-31 14:29:03 +03:00
static float frand()
2018-03-30 00:32:11 +03:00
{
2018-03-31 14:29:03 +03:00
return rand() / (float) RAND_MAX;
2018-03-30 00:32:11 +03:00
}
2018-04-13 07:57:49 +03:00
// Performs back propagation.
2018-04-04 01:12:36 +03:00
static void bprop(const Tinn t, const float* const in, const float* const tg, float rate)
2018-03-29 06:41:08 +03:00
{
2018-03-30 23:04:37 +03:00
for(int i = 0; i < t.nhid; i++)
2018-03-29 06:41:08 +03:00
{
2018-03-31 14:29:03 +03:00
float sum = 0.0f;
2018-03-30 23:04:37 +03:00
// Calculate total error change with respect to output.
for(int j = 0; j < t.nops; j++)
2018-03-29 06:41:08 +03:00
{
2018-04-02 03:01:42 +03:00
const float a = pderr(t.o[j], tg[j]);
const float b = pdact(t.o[j]);
2018-03-30 21:01:49 +03:00
sum += a * b * t.x[j * t.nhid + i];
2018-03-30 23:04:37 +03:00
// Correct weights in hidden to output layer.
2018-03-30 21:01:49 +03:00
t.x[j * t.nhid + i] -= rate * a * b * t.h[i];
2018-03-29 06:41:08 +03:00
}
2018-03-30 23:04:37 +03:00
// Correct weights in input to hidden layer.
for(int j = 0; j < t.nips; j++)
t.w[i * t.nips + j] -= rate * sum * pdact(t.h[i]) * in[j];
}
}
2018-04-13 07:57:49 +03:00
// Performs forward propagation.
2018-04-04 01:12:36 +03:00
static void fprop(const Tinn t, const float* const in)
2018-03-30 23:04:37 +03:00
{
// Calculate hidden layer neuron values.
for(int i = 0; i < t.nhid; i++)
{
2018-03-31 14:29:03 +03:00
float sum = 0.0f;
2018-03-30 23:04:37 +03:00
for(int j = 0; j < t.nips; j++)
sum += in[j] * t.w[i * t.nips + j];
2018-03-30 00:32:11 +03:00
t.h[i] = act(sum + t.b[0]);
2018-03-29 06:41:08 +03:00
}
2018-03-30 23:04:37 +03:00
// Calculate output layer neuron values.
for(int i = 0; i < t.nops; i++)
2018-03-29 06:41:08 +03:00
{
2018-03-31 14:29:03 +03:00
float sum = 0.0f;
2018-03-30 23:04:37 +03:00
for(int j = 0; j < t.nhid; j++)
2018-03-30 21:01:49 +03:00
sum += t.h[j] * t.x[i * t.nhid + j];
2018-03-30 00:32:11 +03:00
t.o[i] = act(sum + t.b[1]);
2018-03-29 06:41:08 +03:00
}
}
2018-04-13 07:57:49 +03:00
// Randomizes tinn weights and biases.
2018-05-20 02:26:44 +03:00
static void wbrand(const Tinn t)
2018-03-29 06:41:08 +03:00
{
2018-03-31 14:29:03 +03:00
for(int i = 0; i < t.nw; i++) t.w[i] = frand() - 0.5f;
for(int i = 0; i < t.nb; i++) t.b[i] = frand() - 0.5f;
2018-03-30 23:04:37 +03:00
}
2018-04-11 05:12:35 +03:00
// Returns an output prediction given an input.
2018-04-04 01:12:36 +03:00
float* xtpredict(const Tinn t, const float* const in)
2018-03-30 23:04:37 +03:00
{
2018-04-04 01:12:36 +03:00
fprop(t, in);
2018-03-30 23:04:37 +03:00
return t.o;
2018-03-29 06:41:08 +03:00
}
2018-04-13 07:57:49 +03:00
// Trains a tinn with an input and target output with a learning rate. Returns target to output error.
2018-04-03 23:08:27 +03:00
float xttrain(const Tinn t, const float* const in, const float* const tg, float rate)
2018-03-29 08:04:47 +03:00
{
2018-04-04 01:12:36 +03:00
fprop(t, in);
bprop(t, in, tg, rate);
return toterr(tg, t.o, t.nops);
2018-03-29 08:04:47 +03:00
}
2018-04-13 07:57:49 +03:00
// Constructs a tinn with number of inputs, number of hidden neurons, and number of outputs
2018-04-03 23:08:27 +03:00
Tinn xtbuild(const int nips, const int nhid, const int nops)
2018-03-29 06:41:08 +03:00
{
Tinn t;
2018-03-30 23:04:37 +03:00
// Tinn only supports one hidden layer so there are two biases.
2018-03-30 00:32:11 +03:00
t.nb = 2;
2018-03-31 01:42:20 +03:00
t.nw = nhid * (nips + nops);
2018-04-13 07:47:42 +03:00
t.w = (float*) calloc(t.nw, sizeof(*t.w));
2018-03-30 21:01:49 +03:00
t.x = t.w + nhid * nips;
2018-04-13 07:47:42 +03:00
t.b = (float*) calloc(t.nb, sizeof(*t.b));
t.h = (float*) calloc(nhid, sizeof(*t.h));
t.o = (float*) calloc(nops, sizeof(*t.o));
2018-03-29 08:04:47 +03:00
t.nips = nips;
2018-03-30 00:32:11 +03:00
t.nhid = nhid;
t.nops = nops;
2018-05-20 02:26:44 +03:00
wbrand(t);
2018-03-29 06:41:08 +03:00
return t;
}
2018-04-13 07:57:49 +03:00
// Saves a tinn to disk.
2018-04-03 23:08:27 +03:00
void xtsave(const Tinn t, const char* const path)
2018-03-31 01:42:20 +03:00
{
2018-04-13 07:47:42 +03:00
FILE* const file = fopen(path, "w");
2018-04-13 07:57:49 +03:00
// Save header.
2018-03-31 01:42:20 +03:00
fprintf(file, "%d %d %d\n", t.nips, t.nhid, t.nops);
2018-04-13 07:57:49 +03:00
// Save biases and weights.
for(int i = 0; i < t.nb; i++) fprintf(file, "%f\n", (double) t.b[i]);
for(int i = 0; i < t.nw; i++) fprintf(file, "%f\n", (double) t.w[i]);
2018-03-31 01:42:20 +03:00
fclose(file);
}
2018-04-13 07:57:49 +03:00
// Loads a tinn from disk.
2018-04-03 23:08:27 +03:00
Tinn xtload(const char* const path)
2018-03-31 01:42:20 +03:00
{
2018-04-13 07:47:42 +03:00
FILE* const file = fopen(path, "r");
2018-03-31 01:42:20 +03:00
int nips = 0;
int nhid = 0;
int nops = 0;
2018-04-13 07:57:49 +03:00
// Load header.
2018-03-31 01:42:20 +03:00
fscanf(file, "%d %d %d\n", &nips, &nhid, &nops);
2018-04-13 07:57:49 +03:00
// Build a new tinn.
2018-04-03 22:46:57 +03:00
const Tinn t = xtbuild(nips, nhid, nops);
// Load bias and weights.
for(int i = 0; i < t.nb; i++) fscanf(file, "%f\n", &t.b[i]);
for(int i = 0; i < t.nw; i++) fscanf(file, "%f\n", &t.w[i]);
2018-03-31 01:42:20 +03:00
fclose(file);
return t;
}
2018-04-13 07:57:49 +03:00
// Frees object from heap.
2018-03-30 23:04:37 +03:00
void xtfree(const Tinn t)
2018-03-29 06:41:08 +03:00
{
2018-03-29 08:04:47 +03:00
free(t.w);
2018-03-31 01:42:20 +03:00
free(t.b);
2018-03-29 08:04:47 +03:00
free(t.h);
free(t.o);
2018-03-29 06:41:08 +03:00
}
2018-04-11 19:34:56 +03:00
2018-04-11 19:38:24 +03:00
// Prints an array of floats. Useful for printing predictions.
2018-04-11 19:34:56 +03:00
void xtprint(const float* arr, const int size)
{
for(int i = 0; i < size; i++)
printf("%f ", (double) arr[i]);
printf("\n");
}