mirror of
https://github.com/glouw/tinn
synced 2024-12-22 12:02:34 +03:00
double to float
This commit is contained in:
parent
97c15283bd
commit
0228fdb32e
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,4 +1,5 @@
|
||||
*.dat*
|
||||
*.txt
|
||||
*.o
|
||||
*.d
|
||||
tinn
|
||||
|
60
Tinn.c
60
Tinn.c
@ -6,55 +6,55 @@
|
||||
#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]);
|
||||
float a = pderr(t.o[j], tg[j]);
|
||||
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];
|
||||
@ -66,12 +66,12 @@ static void backwards(const Tinn t, const double* in, const double* tg, double r
|
||||
}
|
||||
|
||||
// Forward propagation.
|
||||
static void forewards(const Tinn t, const double* in)
|
||||
static void forewards(const Tinn t, const float* 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 forewards(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,17 +89,17 @@ static void forewards(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)
|
||||
float* xpredict(const Tinn t, const float* in)
|
||||
{
|
||||
forewards(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);
|
||||
backwards(t, in, tg, rate);
|
||||
@ -112,11 +112,11 @@ 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*) calloc(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*) calloc(t.nb, sizeof(*t.b));
|
||||
t.h = (float*) calloc(nhid, sizeof(*t.h));
|
||||
t.o = (float*) calloc(nops, sizeof(*t.o));
|
||||
t.nips = nips;
|
||||
t.nhid = nhid;
|
||||
t.nops = nops;
|
||||
@ -131,8 +131,8 @@ void xtsave(const Tinn t, const char* path)
|
||||
// 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);
|
||||
}
|
||||
|
||||
@ -147,8 +147,8 @@ Tinn xtload(const char* path)
|
||||
// A new tinn is returned.
|
||||
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;
|
||||
}
|
||||
|
14
Tinn.h
14
Tinn.h
@ -2,11 +2,11 @@
|
||||
|
||||
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;
|
||||
@ -22,7 +22,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 +30,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);
|
||||
|
32
test.c
32
test.c
@ -5,8 +5,8 @@
|
||||
|
||||
typedef struct
|
||||
{
|
||||
double** in;
|
||||
double** tg;
|
||||
float** in;
|
||||
float** tg;
|
||||
int nips;
|
||||
int nops;
|
||||
int rows;
|
||||
@ -46,11 +46,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 +67,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 +91,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;
|
||||
@ -135,8 +135,8 @@ int main()
|
||||
// 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;
|
||||
float rate = 1.0;
|
||||
const float anneal = 0.99;
|
||||
// Load the training set.
|
||||
const Data data = build("semeion.data", nips, nops);
|
||||
// Train, baby, train.
|
||||
@ -144,11 +144,11 @@ int main()
|
||||
for(int i = 0; i < 100; i++)
|
||||
{
|
||||
shuffle(data);
|
||||
double error = 0.0;
|
||||
float error = 0.0;
|
||||
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);
|
||||
@ -162,9 +162,9 @@ 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);
|
||||
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 ", tg[i]); } printf("\n");
|
||||
for(int i = 0; i < data.nops; i++) { printf("%f ", pd[i]); } printf("\n");
|
||||
// All done. Let's clean up.
|
||||
|
Loading…
Reference in New Issue
Block a user