This commit is contained in:
Gustav Louw 2018-03-30 11:01:49 -07:00
parent dd6fc57439
commit 97c15283bd
2 changed files with 6 additions and 6 deletions

9
Tinn.c
View File

@ -47,7 +47,6 @@ static double frand()
// Back propagation. // Back propagation.
static void backwards(const Tinn t, const double* in, const double* tg, double rate) static void backwards(const Tinn t, const double* in, const double* tg, double rate)
{ {
double* x = t.w + t.nhid * t.nips;
for(int i = 0; i < t.nhid; i++) for(int i = 0; i < t.nhid; i++)
{ {
double sum = 0.0; double sum = 0.0;
@ -56,9 +55,9 @@ static void backwards(const Tinn t, const double* in, const double* tg, double r
{ {
double a = pderr(t.o[j], tg[j]); double a = pderr(t.o[j], tg[j]);
double b = pdact(t.o[j]); double b = pdact(t.o[j]);
sum += a * b * x[j * t.nhid + i]; sum += a * b * t.x[j * t.nhid + i];
// Correct weights in hidden to output layer. // Correct weights in hidden to output layer.
x[j * t.nhid + i] -= rate * a * b * t.h[i]; t.x[j * t.nhid + i] -= rate * a * b * t.h[i];
} }
// Correct weights in input to hidden layer. // Correct weights in input to hidden layer.
for(int j = 0; j < t.nips; j++) for(int j = 0; j < t.nips; j++)
@ -69,7 +68,6 @@ static void backwards(const Tinn t, const double* in, const double* tg, double r
// Forward propagation. // Forward propagation.
static void forewards(const Tinn t, const double* in) static void forewards(const Tinn t, const double* in)
{ {
double* x = t.w + t.nhid * t.nips;
// Calculate hidden layer neuron values. // Calculate hidden layer neuron values.
for(int i = 0; i < t.nhid; i++) for(int i = 0; i < t.nhid; i++)
{ {
@ -83,7 +81,7 @@ static void forewards(const Tinn t, const double* in)
{ {
double sum = 0.0; double sum = 0.0;
for(int j = 0; j < t.nhid; j++) for(int j = 0; j < t.nhid; j++)
sum += t.h[j] * x[i * t.nhid + j]; sum += t.h[j] * t.x[i * t.nhid + j];
t.o[i] = act(sum + t.b[1]); t.o[i] = act(sum + t.b[1]);
} }
} }
@ -115,6 +113,7 @@ Tinn xtbuild(int nips, int nhid, int nops)
t.nb = 2; t.nb = 2;
t.nw = nhid * (nips + nops); t.nw = nhid * (nips + nops);
t.w = (double*) calloc(t.nw, sizeof(*t.w)); t.w = (double*) calloc(t.nw, sizeof(*t.w));
t.x = t.w + nhid * nips;
t.b = (double*) calloc(t.nb, sizeof(*t.b)); t.b = (double*) calloc(t.nb, sizeof(*t.b));
t.h = (double*) calloc(nhid, sizeof(*t.h)); t.h = (double*) calloc(nhid, sizeof(*t.h));
t.o = (double*) calloc(nops, sizeof(*t.o)); t.o = (double*) calloc(nops, sizeof(*t.o));

3
Tinn.h
View File

@ -2,7 +2,8 @@
typedef struct typedef struct
{ {
double* w; // Weights. double* w; // All the weights.
double* x; // Hidden to output layer weights.
double* b; // Biases. double* b; // Biases.
double* h; // Hidden layer. double* h; // Hidden layer.
double* o; // Output layer. double* o; // Output layer.