with example

This commit is contained in:
Gustav Louw 2018-03-29 14:32:11 -07:00
parent aac310890d
commit adabe8d1ca
4 changed files with 59 additions and 82 deletions

View File

@ -2,6 +2,27 @@
Tinn (Tiny Neural Network) is a dependency free ANSI-C neural network library. Tinn (Tiny Neural Network) is a dependency free ANSI-C neural network library.
#include "Tinn.h"
#include <stdio.h>
#define len(a) ((int) (sizeof(a) / sizeof(*a)))
int main(void)
{
double in[] = { 0.05, 0.10 };
double tg[] = { 0.01, 0.99 };
/* Two hidden nuerons */
const Tinn tinn = xtbuild(len(in), 2, len(tg));
int i;
for(i = 0; i < 10000; i++)
{
double error = xttrain(tinn, in, tg, 0.5);
printf("%.12f\n", error);
}
xtfree(tinn);
return 0;
}
Run the sample: Run the sample:
make; ./tinn make; ./tinn

69
Tinn.c
View File

@ -2,6 +2,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <math.h> #include <math.h>
#include <time.h>
static double error(Tinn t, double* tg) static double error(Tinn t, double* tg)
{ {
@ -49,9 +50,13 @@ static double act(double net)
return 1.0 / (1.0 + exp(-net)); return 1.0 / (1.0 + exp(-net));
} }
static double frand(void)
{
return rand() / (double) RAND_MAX;
}
static void forewards(Tinn t, double* in) static void forewards(Tinn t, double* in)
{ {
const double bias[] = { 0.35, 0.60 };
double* x = t.w + t.nhid * t.nips; double* x = t.w + t.nhid * t.nips;
int i; int i;
for(i = 0; i < t.nhid; i++) for(i = 0; i < t.nhid; i++)
@ -64,7 +69,7 @@ static void forewards(Tinn t, double* in)
double b = t.w[i * t.nips + j]; double b = t.w[i * t.nips + j];
sum += a * b; sum += a * b;
} }
t.h[i] = act(sum + bias[0]); t.h[i] = act(sum + t.b[0]);
} }
for(i = 0; i < t.nops; i++) for(i = 0; i < t.nops; i++)
{ {
@ -76,53 +81,16 @@ static void forewards(Tinn t, double* in)
double b = x[i * t.nhid + j]; double b = x[i * t.nhid + j];
sum += a * b; sum += a * b;
} }
t.o[i] = act(sum + bias[1]); t.o[i] = act(sum + t.b[1]);
} }
} }
static void twrand(Tinn t) static void twrand(Tinn t)
{ {
#if 0 const int wgts = t.nhid * (t.nips + t.nops);
/* 2 2 2 */ int i;
t.w[0] = 0.15; for(i = 0; i < wgts; i++) t.w[i] = frand();
t.w[1] = 0.20; for(i = 0; i < t.nb; i++) t.b[i] = frand();
t.w[2] = 0.25;
t.w[3] = 0.30;
t.w[4] = 0.40;
t.w[5] = 0.45;
t.w[6] = 0.50;
t.w[7] = 0.55;
#endif
#if 0
/* 2 3 2 */
t.w[0] = 0.15;
t.w[1] = 0.20;
t.w[2] = 0.25;
t.w[3] = 0.30;
t.w[4] = 0.30;
t.w[5] = 0.30;
t.w[6] = 0.40;
t.w[7] = 0.45;
t.w[8] = 0.50;
t.w[9] = 0.55;
t.w[10] = 0.55;
t.w[11] = 0.55;
#endif
/* 2 3 1 */
#if 1
t.w[0] = 0.15;
t.w[1] = 0.20;
t.w[2] = 0.25;
t.w[3] = 0.30;
t.w[4] = 0.30;
t.w[5] = 0.30;
t.w[6] = 0.40;
t.w[7] = 0.45;
t.w[8] = 0.50;
#endif
} }
double xttrain(Tinn t, double* in, double* tg, double rate) double xttrain(Tinn t, double* in, double* tg, double rate)
@ -132,15 +100,18 @@ double xttrain(Tinn t, double* in, double* tg, double rate)
return error(t, tg); return error(t, tg);
} }
Tinn xtbuild(int nips, int nops, int nhid) Tinn xtbuild(int nips, int nhid, int nops)
{ {
Tinn t; Tinn t;
t.o = (double*) calloc(nops, sizeof(*t.o)); t.nb = 2;
t.h = (double*) calloc(nhid, sizeof(*t.h));
t.w = (double*) calloc(nhid * (nips + nops), sizeof(*t.w)); t.w = (double*) calloc(nhid * (nips + nops), sizeof(*t.w));
t.nops = nops; t.b = (double*) calloc(t.nb, sizeof(*t.b));
t.nhid = nhid; t.h = (double*) calloc(nhid, sizeof(*t.h));
t.o = (double*) calloc(nops, sizeof(*t.o));
t.nips = nips; t.nips = nips;
t.nhid = nhid;
t.nops = nops;
srand(time(0));
twrand(t); twrand(t);
return t; return t;
} }

12
Tinn.h
View File

@ -3,18 +3,20 @@
typedef struct typedef struct
{ {
double* o;
double* h;
double* w; double* w;
int nops; double* b;
int nhid; double* h;
double* o;
int nb;
int nips; int nips;
int nhid;
int nops;
} }
Tinn; Tinn;
extern double xttrain(Tinn, double* in, double* tg, double rate); extern double xttrain(Tinn, double* in, double* tg, double rate);
extern Tinn xtbuild(int nips, int nops, int nhid); extern Tinn xtbuild(int nips, int nhid, int nops);
extern void xtfree(Tinn); extern void xtfree(Tinn);

39
test.c
View File

@ -1,37 +1,20 @@
#include "Tinn.h" #include "Tinn.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
static double* inload(int nips) #define len(a) ((int) (sizeof(a) / sizeof(*a)))
{
double* in = (double*) calloc(nips, sizeof(*in));
in[0] = 0.05;
in[1] = 0.10;
return in;
}
static double* tgload(int nops) int main(void)
{ {
double* tg = (double*) calloc(nops, sizeof(*tg)); double in[] = { 0.05, 0.10 };
tg[0] = 0.01; double tg[] = { 0.01, 0.99 };
/* tg[1] = 0.99; */ /* Two hidden nuerons */
return tg; const Tinn tinn = xtbuild(len(in), 2, len(tg));
}
int main()
{
int nips = 2;
int nhid = 3;
int nops = 1;
double* in = inload(nips);
double* tg = tgload(nops);
Tinn tinn = xtbuild(nips, nops, nhid);
int i; int i;
for(i = 0; i <= 10000; i++) for(i = 0; i < 10000; i++)
printf("%.18f\n", xttrain(tinn, in, tg, 0.5)); {
double error = xttrain(tinn, in, tg, 0.5);
printf("%.12f\n", error);
}
xtfree(tinn); xtfree(tinn);
free(in);
free(tg);
return 0; return 0;
} }