This commit is contained in:
Gustav Louw 2018-03-28 22:04:47 -07:00
parent 3a5346e72a
commit eec394e038
3 changed files with 93 additions and 81 deletions

115
Tinn.c
View File

@ -3,44 +3,44 @@
#include <stdlib.h>
#include <math.h>
static double error(Tinn t, double* T)
static double error(Tinn t, double* tg)
{
double error = 0.0;
int i;
for(i = 0; i < t.output; i++)
error += 0.5 * pow(T[i] - t.O[i], 2.0);
for(i = 0; i < t.nops; i++)
error += 0.5 * pow(tg[i] - t.o[i], 2.0);
return error;
}
static void backpass(Tinn t, double* I, double* T, double rate)
static void backwards(Tinn t, double* in, double* tg, double rate)
{
int i, j, k;
double* X = t.W + t.hidden * t.inputs;
for(i = 0; i < t.inputs; i++)
double* X = t.w + t.nhid * t.nips;
for(i = 0; i < t.nips; i++)
{
double sum = 0.0;
for(k = 0; k < t.output; k++)
for(k = 0; k < t.nops; k++)
{
double a = t.O[k] - T[k];
double b = t.O[k] * (1 - t.O[k]);
double c = X[k * t.output + i];
double a = t.o[k] - tg[k];
double b = t.o[k] * (1 - t.o[k]);
double c = X[k * t.nops + i];
sum += a * b * c;
}
for(j = 0; j < t.hidden; j++)
for(j = 0; j < t.nhid; j++)
{
double a = sum;
double b = t.H[i] * (1 - t.H[i]);
double c = I[j];
t.W[i * t.hidden + j] -= rate * a * b * c;
double b = t.h[i] * (1 - t.h[i]);
double c = in[j];
t.w[i * t.nhid + j] -= rate * a * b * c;
}
}
for(i = 0; i < t.output; i++)
for(j = 0; j < t.hidden; j++)
for(i = 0; i < t.nops; i++)
for(j = 0; j < t.nhid; j++)
{
double a = t.O[i] - T[i];
double b = t.O[i] * (1 - t.O[i]);
double c = t.H[j];
X[t.hidden * i + j] -= rate * a * b * c;
double a = t.o[i] - tg[i];
double b = t.o[i] * (1 - t.o[i]);
double c = t.h[j];
X[t.nhid * i + j] -= rate * a * b * c;
}
}
@ -49,65 +49,70 @@ static double act(double net)
return 1.0 / (1.0 + exp(-net));
}
static void forepass(Tinn t, double* I)
static void forewards(Tinn t, double* in)
{
int i, j;
const double B[] = { 0.35, 0.60 };
double* X = t.W + t.hidden * t.inputs;
for(i = 0; i < t.hidden; i++)
const double bias[] = { 0.35, 0.60 };
double* X = t.w + t.nhid * t.nips;
for(i = 0; i < t.nhid; i++)
{
double sum = 0.0;
for(j = 0; j < t.inputs; j++)
for(j = 0; j < t.nips; j++)
{
double a = I[j];
double b = t.W[i * t.inputs + j];
double a = in[j];
double b = t.w[i * t.nips + j];
sum += a * b;
}
t.H[i] = act(sum + B[0]);
t.h[i] = act(sum + bias[0]);
}
for(i = 0; i < t.output; i++)
for(i = 0; i < t.nops; i++)
{
double sum = 0.0;
for(j = 0; j < t.hidden; j++)
for(j = 0; j < t.nhid; j++)
{
double a = t.H[j];
double b = X[i * t.hidden + j];
double a = t.h[j];
double b = X[i * t.nhid + j];
sum += a * b;
}
t.O[i] = act(sum + B[1]);
t.o[i] = act(sum + bias[1]);
}
}
double ttrain(Tinn t, double* I, double* T, double rate)
static void twrand(Tinn t)
{
forepass(t, I);
backpass(t, I, T, rate);
return error(t, T);
t.w[0] = 0.15;
t.w[1] = 0.20;
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;
}
Tinn tnew(int inputs, int output, int hidden)
double ttrain(Tinn t, double* in, double* tg, double rate)
{
forewards(t, in);
backwards(t, in, tg, rate);
return error(t, tg);
}
Tinn tbuild(int nips, int nops, int nhid)
{
Tinn t;
t.inputs = inputs;
t.output = output;
t.hidden = hidden;
t.H = (double*) calloc(hidden, sizeof(*t.H));
t.O = (double*) calloc(output, sizeof(*t.O));
t.W = (double*) calloc(hidden * (inputs + output), sizeof(*t.W));
t.W[0] = 0.15;
t.W[1] = 0.20;
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;
t.o = (double*) calloc(nops, sizeof(*t.o));
t.h = (double*) calloc(nhid, sizeof(*t.h));
t.w = (double*) calloc(nhid * (nips + nops), sizeof(*t.w));
t.nops = nops;
t.nhid = nhid;
t.nips = nips;
twrand(t);
return t;
}
void tfree(Tinn t)
{
free(t.W);
free(t.H);
free(t.O);
free(t.w);
free(t.h);
free(t.o);
}

16
Tinn.h
View File

@ -8,18 +8,18 @@
typedef struct
{
double* O;
double* H;
double* W;
int output;
int hidden;
int inputs;
double* o;
double* h;
double* w;
int nops;
int nhid;
int nips;
}
Tinn;
double ttrain(Tinn, double* I, double* T, double rate);
double ttrain(Tinn, double* in, double* tg, double rate);
Tinn tnew(int inputs, int output, int hidden);
Tinn tbuild(int inputs, int output, int hidden);
void tfree(Tinn);

43
main.c
View File

@ -3,28 +3,35 @@
#include <stdio.h>
#include <stdlib.h>
static double* inload(int nips)
{
double* in = (double*) calloc(nips, sizeof(*in));
in[0] = 0.05;
in[1] = 0.10;
return in;
}
static double* tgload(int nops)
{
double* tg = (double*) calloc(nops, sizeof(*tg));
tg[0] = 0.01;
tg[1] = 0.99;
return tg;
}
int main()
{
int nips = 2;
int nops = 2;
int nhid = 2;
double* in = inload(nips);
double* tg = tgload(nops);
Tinn tinn = tbuild(nips, nops, nhid);
int i;
int inputs = 2;
int output = 2;
int hidden = 2;
double* I = (double*) calloc(inputs, sizeof(*I));
double* T = (double*) calloc(output, sizeof(*T));
Tinn tinn = tnew(inputs, output, hidden);
/* Input. */
I[0] = 0.05;
I[1] = 0.10;
/* Target. */
T[0] = 0.01;
T[1] = 0.99;
for(i = 0; i < 10000; i++)
{
double error = ttrain(tinn, I, T, 0.5);
printf("error: %0.13f\n", error);
}
printf("%.18f\n", ttrain(tinn, in, tg, 0.5));
tfree(tinn);
free(I);
free(T);
free(in);
free(tg);
return 0;
}