Merge branch 'master' into patch-2

This commit is contained in:
Brandon Newton 2018-04-02 18:22:11 -04:00 committed by GitHub
commit 8c2a1c6f8a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 99 additions and 71 deletions

1
.gitignore vendored
View File

@ -1,4 +1,5 @@
*.dat*
*.txt
*.o
*.d
tinn

View File

@ -10,13 +10,13 @@ Tinn can be compiled with any C++ compiler as well.
int main()
{
double in[] = { 0.05, 0.10 };
double tg[] = { 0.01, 0.99 };
float in[] = { 0.05, 0.10 };
float tg[] = { 0.01, 0.99 };
/* Two hidden neurons */
const Tinn tinn = xtbuild(len(in), 2, len(tg));
for(int i = 0; i < 1000; i++)
{
double error = xttrain(tinn, in, tg, 0.5);
float error = xttrain(tinn, in, tg, 0.5);
printf("%.12f\n", error);
}
xtfree(tinn);

99
Tinn.c
View File

@ -1,60 +1,60 @@
#include "Tinn.h"
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
// Error function.
static double err(double a, double b)
static float err(float a, float b)
{
return 0.5 * pow(a - b, 2.0);
return 0.5f * powf(a - b, 2.0f);
}
// Partial derivative of error function.
static double pderr(double a, double b)
static float pderr(float a, float b)
{
return a - b;
}
// Total error.
static double terr(const double* tg, const double* o, int size)
static float terr(const float* tg, const float* o, int size)
{
double sum = 0.0;
float sum = 0.0f;
for(int i = 0; i < size; i++)
sum += err(tg[i], o[i]);
return sum;
}
// Activation function.
static double act(double a)
static float act(float a)
{
return 1.0 / (1.0 + exp(-a));
return 1.0f / (1.0f + expf(-a));
}
// Partial derivative of activation function.
static double pdact(double a)
static float pdact(float a)
{
return a * (1.0 - a);
return a * (1.0f - a);
}
// Floating point random from 0.0 - 1.0.
static double frand()
static float frand()
{
return rand() / (double) RAND_MAX;
return rand() / (float) RAND_MAX;
}
// Back propagation.
static void backwards(const Tinn t, const double* in, const double* tg, double rate)
static void backwards(const Tinn t, const float* in, const float* tg, float rate)
{
for(int i = 0; i < t.nhid; i++)
{
double sum = 0.0;
float sum = 0.0f;
// Calculate total error change with respect to output.
for(int j = 0; j < t.nops; j++)
{
double a = pderr(t.o[j], tg[j]);
double b = pdact(t.o[j]);
const float a = pderr(t.o[j], tg[j]);
const float b = pdact(t.o[j]);
sum += a * b * t.x[j * t.nhid + i];
// Correct weights in hidden to output layer.
t.x[j * t.nhid + i] -= rate * a * b * t.h[i];
@ -71,7 +71,7 @@ static void forwards(const Tinn t, const double* in)
// Calculate hidden layer neuron values.
for(int i = 0; i < t.nhid; i++)
{
double sum = 0.0;
float sum = 0.0f;
for(int j = 0; j < t.nips; j++)
sum += in[j] * t.w[i * t.nips + j];
t.h[i] = act(sum + t.b[0]);
@ -79,7 +79,7 @@ static void forwards(const Tinn t, const double* in)
// Calculate output layer neuron values.
for(int i = 0; i < t.nops; i++)
{
double sum = 0.0;
float sum = 0.0f;
for(int j = 0; j < t.nhid; j++)
sum += t.h[j] * t.x[i * t.nhid + j];
t.o[i] = act(sum + t.b[1]);
@ -89,19 +89,47 @@ static void forwards(const Tinn t, const double* in)
// Randomizes weights and biases.
static void twrand(const Tinn t)
{
for(int i = 0; i < t.nw; i++) t.w[i] = frand() - 0.5;
for(int i = 0; i < t.nb; i++) t.b[i] = frand() - 0.5;
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;
}
double* xpredict(const Tinn t, const double* in)
// Prints a message and exits.
static void bomb(const char* const message, ...)
{
forewards(t, in);
va_list args;
va_start(args, message);
vprintf(message, args);
va_end(args);
exit(1);
}
// Fail safe file opening.
static FILE* efopen(const char* const pathname, const char* const mode)
{
FILE* const file = fopen(pathname, mode);
if(file == NULL)
bomb("failure: fopen(\"%s\", \"%s\")\n", pathname, mode);
return file;
}
// Fail safe clear allocation.
static void* ecalloc(const size_t nmemb, const size_t size)
{
void* const mem = calloc(nmemb, size);
if(mem == NULL)
bomb("failure: calloc(%d, %d)\n", nmemb, size);
return mem;
}
float* xpredict(const Tinn t, const float* in)
{
forwards(t, in);
return t.o;
}
double xttrain(const Tinn t, const double* in, const double* tg, double rate)
float xttrain(const Tinn t, const float* in, const float* tg, float rate)
{
forewards(t, in);
forwards(t, in);
backwards(t, in, tg, rate);
return terr(tg, t.o, t.nops);
}
@ -112,43 +140,42 @@ Tinn xtbuild(int nips, int nhid, int nops)
// Tinn only supports one hidden layer so there are two biases.
t.nb = 2;
t.nw = nhid * (nips + nops);
t.w = (double*) calloc(t.nw, sizeof(*t.w));
t.w = (float*) ecalloc(t.nw, sizeof(*t.w));
t.x = t.w + nhid * nips;
t.b = (double*) calloc(t.nb, sizeof(*t.b));
t.h = (double*) calloc(nhid, sizeof(*t.h));
t.o = (double*) calloc(nops, sizeof(*t.o));
t.b = (float*) ecalloc(t.nb, sizeof(*t.b));
t.h = (float*) ecalloc(nhid, sizeof(*t.h));
t.o = (float*) ecalloc(nops, sizeof(*t.o));
t.nips = nips;
t.nhid = nhid;
t.nops = nops;
srand(time(0));
twrand(t);
return t;
}
void xtsave(const Tinn t, const char* path)
{
FILE* file = fopen(path, "w");
FILE* const file = efopen(path, "w");
// Header.
fprintf(file, "%d %d %d\n", t.nips, t.nhid, t.nops);
// Biases and weights.
for(int i = 0; i < t.nb; i++) fprintf(file, "%lf\n", t.b[i]);
for(int i = 0; i < t.nw; i++) fprintf(file, "%lf\n", t.w[i]);
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]);
fclose(file);
}
Tinn xtload(const char* path)
{
FILE* file = fopen(path, "r");
FILE* const file = efopen(path, "r");
int nips = 0;
int nhid = 0;
int nops = 0;
// Header.
fscanf(file, "%d %d %d\n", &nips, &nhid, &nops);
// A new tinn is returned.
Tinn t = xtbuild(nips, nhid, nips);
const Tinn t = xtbuild(nips, nhid, nips);
// Biases and weights.
for(int i = 0; i < t.nb; i++) fscanf(file, "%lf\n", &t.b[i]);
for(int i = 0; i < t.nw; i++) fscanf(file, "%lf\n", &t.w[i]);
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]);
fclose(file);
return t;
}

21
Tinn.h
View File

@ -2,17 +2,14 @@
typedef struct
{
double* w; // All the weights.
double* x; // Hidden to output layer weights.
double* b; // Biases.
double* h; // Hidden layer.
double* o; // Output layer.
float* w; // All the weights.
float* x; // Hidden to output layer weights.
float* b; // Biases.
float* h; // Hidden layer.
float* o; // Output layer.
// Number of biases - always two - Tinn only supports a single hidden layer.
int nb;
// Number of weights.
int nw;
int nb; // Number of biases - always two - Tinn only supports a single hidden layer.
int nw; // Number of weights.
int nips; // Number of inputs.
int nhid; // Number of hidden neurons.
@ -22,7 +19,7 @@ Tinn;
// Trains a tinn with an input and target output with a learning rate.
// Returns error rate of the neural network.
double xttrain(const Tinn, const double* in, const double* tg, double rate);
float xttrain(const Tinn, const float* in, const float* tg, float rate);
// Builds a new tinn object given number of inputs (nips),
// number of hidden neurons for the hidden layer (nhid),
@ -30,7 +27,7 @@ double xttrain(const Tinn, const double* in, const double* tg, double rate);
Tinn xtbuild(int nips, int nhid, int nops);
// Returns an output prediction given an input.
double* xpredict(const Tinn, const double* in);
float* xpredict(const Tinn, const float* in);
// Saves the tinn to disk.
void xtsave(const Tinn, const char* path);

43
test.c
View File

@ -1,12 +1,13 @@
#include "Tinn.h"
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
typedef struct
{
double** in;
double** tg;
float** in;
float** tg;
int nips;
int nops;
int rows;
@ -46,11 +47,11 @@ static char* readln(FILE* const file)
return line;
}
static double** new2d(const int rows, const int cols)
static float** new2d(const int rows, const int cols)
{
double** row = (double**) malloc((rows) * sizeof(double*));
float** row = (float**) malloc((rows) * sizeof(float*));
for(int r = 0; r < rows; r++)
row[r] = (double*) malloc((cols) * sizeof(double));
row[r] = (float*) malloc((cols) * sizeof(float));
return row;
}
@ -67,7 +68,7 @@ static void parse(const Data data, char* line, const int row)
const int cols = data.nips + data.nops;
for(int col = 0; col < cols; col++)
{
const double val = atof(strtok(col == 0 ? line : NULL, " "));
const float val = atof(strtok(col == 0 ? line : NULL, " "));
if(col < data.nips)
data.in[row][col] = val;
else
@ -91,8 +92,8 @@ static void shuffle(const Data d)
for(int a = 0; a < d.rows; a++)
{
const int b = rand() % d.rows;
double* ot = d.tg[a];
double* it = d.in[a];
float* ot = d.tg[a];
float* it = d.in[a];
// Swap output.
d.tg[a] = d.tg[b];
d.tg[b] = ot;
@ -126,6 +127,8 @@ static Data build(const char* path, const int nips, const int nops)
int main()
{
// Tinn does not seed the random number generator.
srand(time(0));
// Input and output size is harded coded here as machine learning
// repositories usually don't include the input and output size in the data itself.
const int nips = 256;
@ -134,9 +137,9 @@ int main()
// Learning rate is annealed and thus not constant.
// It can be fine tuned along with the number of hidden layers.
// Feel free to modify the anneal rate as well.
const int nhid = 30;
double rate = 1.0;
const double anneal = 0.99;
const int nhid = 28;
float rate = 1.0f;
const float anneal = 0.99f;
// Load the training set.
const Data data = build("semeion.data", nips, nops);
// Train, baby, train.
@ -144,14 +147,14 @@ int main()
for(int i = 0; i < 100; i++)
{
shuffle(data);
double error = 0.0;
float error = 0.0f;
for(int j = 0; j < data.rows; j++)
{
const double* const in = data.in[j];
const double* const tg = data.tg[j];
const float* const in = data.in[j];
const float* const tg = data.tg[j];
error += xttrain(tinn, in, tg, rate);
}
printf("error %.12f :: rate %f\n", error / data.rows, rate);
printf("error %.12f :: rate %f\n", (double) error / data.rows, (double) rate);
rate *= anneal;
}
// This is how you save the neural network to disk.
@ -162,11 +165,11 @@ int main()
// Now we do a prediction with the neural network we loaded from disk.
// Ideally, we would also load a testing set to make the prediction with,
// but for the sake of brevity here we just reuse the training set from earlier.
const double* const in = data.in[0];
const double* const tg = data.tg[0];
const double* const pd = xpredict(loaded, in);
for(int i = 0; i < data.nops; i++) { printf("%f ", tg[i]); } printf("\n");
for(int i = 0; i < data.nops; i++) { printf("%f ", pd[i]); } printf("\n");
const float* const in = data.in[0];
const float* const tg = data.tg[0];
const float* const pd = xpredict(loaded, in);
for(int i = 0; i < data.nops; i++) { printf("%f ", (double) tg[i]); } printf("\n");
for(int i = 0; i < data.nops; i++) { printf("%f ", (double) pd[i]); } printf("\n");
// All done. Let's clean up.
xtfree(loaded);
dfree(data);