mirror of
https://github.com/glouw/tinn
synced 2024-11-24 23:39:38 +03:00
doc
This commit is contained in:
parent
396bd2bcdd
commit
c0a6cbe4be
24
test.c
24
test.c
@ -4,16 +4,23 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
// Data object.
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
|
// 2D floating point array of input.
|
||||||
float** in;
|
float** in;
|
||||||
|
// 2D floating point array of target.
|
||||||
float** tg;
|
float** tg;
|
||||||
|
// Number of inputs to neural network.
|
||||||
int nips;
|
int nips;
|
||||||
|
// Number of outputs to neural network.
|
||||||
int nops;
|
int nops;
|
||||||
|
// Number of rows in file (number of sets for neural network).
|
||||||
int rows;
|
int rows;
|
||||||
}
|
}
|
||||||
Data;
|
Data;
|
||||||
|
|
||||||
|
// Returns the number of lines in a file.
|
||||||
static int lns(FILE* const file)
|
static int lns(FILE* const file)
|
||||||
{
|
{
|
||||||
int ch = EOF;
|
int ch = EOF;
|
||||||
@ -31,6 +38,7 @@ static int lns(FILE* const file)
|
|||||||
return lines;
|
return lines;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Reads a line from a file.
|
||||||
static char* readln(FILE* const file)
|
static char* readln(FILE* const file)
|
||||||
{
|
{
|
||||||
int ch = EOF;
|
int ch = EOF;
|
||||||
@ -47,6 +55,7 @@ static char* readln(FILE* const file)
|
|||||||
return line;
|
return line;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// New 2D array of floats.
|
||||||
static float** new2d(const int rows, const int cols)
|
static float** new2d(const int rows, const int cols)
|
||||||
{
|
{
|
||||||
float** row = (float**) malloc((rows) * sizeof(float*));
|
float** row = (float**) malloc((rows) * sizeof(float*));
|
||||||
@ -55,6 +64,7 @@ static float** new2d(const int rows, const int cols)
|
|||||||
return row;
|
return row;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// New data object.
|
||||||
static Data ndata(const int nips, const int nops, const int rows)
|
static Data ndata(const int nips, const int nops, const int rows)
|
||||||
{
|
{
|
||||||
const Data data = {
|
const Data data = {
|
||||||
@ -63,6 +73,7 @@ static Data ndata(const int nips, const int nops, const int rows)
|
|||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Gets one row of inputs and outputs from a string.
|
||||||
static void parse(const Data data, char* line, const int row)
|
static void parse(const Data data, char* line, const int row)
|
||||||
{
|
{
|
||||||
const int cols = data.nips + data.nops;
|
const int cols = data.nips + data.nops;
|
||||||
@ -76,6 +87,7 @@ static void parse(const Data data, char* line, const int row)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Frees a data object from the heap.
|
||||||
static void dfree(const Data d)
|
static void dfree(const Data d)
|
||||||
{
|
{
|
||||||
for(int row = 0; row < d.rows; row++)
|
for(int row = 0; row < d.rows; row++)
|
||||||
@ -87,6 +99,7 @@ static void dfree(const Data d)
|
|||||||
free(d.tg);
|
free(d.tg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Randomly shuffles a data object.
|
||||||
static void shuffle(const Data d)
|
static void shuffle(const Data d)
|
||||||
{
|
{
|
||||||
for(int a = 0; a < d.rows; a++)
|
for(int a = 0; a < d.rows; a++)
|
||||||
@ -103,6 +116,7 @@ static void shuffle(const Data d)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Parses file from path getting all inputs and outputs for the neural network. Returns data object.
|
||||||
static Data build(const char* path, const int nips, const int nops)
|
static Data build(const char* path, const int nips, const int nops)
|
||||||
{
|
{
|
||||||
FILE* file = fopen(path, "r");
|
FILE* file = fopen(path, "r");
|
||||||
@ -170,12 +184,14 @@ int main()
|
|||||||
// Now we do a prediction with the neural network we loaded from disk.
|
// 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,
|
// 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.
|
// but for the sake of brevity here we just reuse the training set from earlier.
|
||||||
// One data set is picked at random.
|
// One data set is picked at random (zero index of input and target arrays is enough
|
||||||
const int pick = rand() % data.rows;
|
// as they were both shuffled earlier).
|
||||||
const float* const in = data.in[pick];
|
const float* const in = data.in[0];
|
||||||
const float* const tg = data.tg[pick];
|
const float* const tg = data.tg[0];
|
||||||
const float* const pd = xtpredict(loaded, in);
|
const float* const pd = xtpredict(loaded, in);
|
||||||
|
// Prints target.
|
||||||
xtprint(tg, data.nops);
|
xtprint(tg, data.nops);
|
||||||
|
// Prints prediction.
|
||||||
xtprint(pd, data.nops);
|
xtprint(pd, data.nops);
|
||||||
// All done. Let's clean up.
|
// All done. Let's clean up.
|
||||||
xtfree(loaded);
|
xtfree(loaded);
|
||||||
|
Loading…
Reference in New Issue
Block a user