diff --git a/README.md b/README.md index 44ceed8..62e6e25 100644 --- a/README.md +++ b/README.md @@ -84,6 +84,26 @@ This gives 10 outputs to the neural network. The test program will output the accuracy for each digit. Expect above 99% accuracy for the correct digit, and less that 1% accuracy for the other digits. +# Tips + +* Tinn will never use more than the C standard library. + +* Tinn is great for embedded systems. Train a model on your powerful desktop and load +it onto a microcontroller and use the analog to digital converter to predict real time events. + +* The Tinn source code will always be less than 200 lines. Functions externed in the Tinn header +are protected with the _xt_ namespace standing for _externed tinn_. + +* Tinn can easily be multi-threaded with a bit of ingenuity but the master branch will remain +single threaded to aid development for embedded systems. + +* Tinn does not seed the random number generator. Do not forget to do so yourself. + +* Always shuffle your input data. Shuffle again after every training iteration. + +* Get greater training accuracy by annealing your learning rate. For instance, multiply +your learning rate by 0.99 every training iteration. This will zero in on a good learning minima. + # Disclaimer Tinn is not a fully featured neural network C library like Kann, or Genann: diff --git a/Tinn.c b/Tinn.c index 3e71b41..6264c2b 100644 --- a/Tinn.c +++ b/Tinn.c @@ -121,12 +121,15 @@ static void* ecalloc(const size_t nmemb, const size_t size) return mem; } +// Returns an output prediction given an input. float* xtpredict(const Tinn t, const float* const in) { fprop(t, in); return t.o; } +// Trains a tinn with an input and target output with a learning rate. +// Returns error rate of the neural network. float xttrain(const Tinn t, const float* const in, const float* const tg, float rate) { fprop(t, in); @@ -134,6 +137,9 @@ float xttrain(const Tinn t, const float* const in, const float* const tg, float return toterr(tg, t.o, t.nops); } +// Builds a new tinn object given number of inputs (nips), +// number of hidden neurons for the hidden layer (nhid), +// and number of outputs (nops). Tinn xtbuild(const int nips, const int nhid, const int nops) { Tinn t; @@ -152,6 +158,7 @@ Tinn xtbuild(const int nips, const int nhid, const int nops) return t; } +// Saves the tinn to disk. void xtsave(const Tinn t, const char* const path) { FILE* const file = efopen(path, "w"); @@ -163,6 +170,7 @@ void xtsave(const Tinn t, const char* const path) fclose(file); } +// Loads a new tinn from disk. Tinn xtload(const char* const path) { FILE* const file = efopen(path, "r"); @@ -180,6 +188,7 @@ Tinn xtload(const char* const path) return t; } +// Frees a tinn from the heap. void xtfree(const Tinn t) { free(t.w); diff --git a/Tinn.h b/Tinn.h index ebea924..af0f927 100644 --- a/Tinn.h +++ b/Tinn.h @@ -2,38 +2,39 @@ typedef struct { - float* w; // All the weights. - float* x; // Hidden to output layer weights. - float* b; // Biases. - float* h; // Hidden layer. - float* o; // Output layer. + // All the weights. + float* w; + // Hidden to output layer weights. + float* x; + // Biases. + float* b; + // Hidden layer. + float* h; + // Output layer. + float* o; - int nb; // Number of biases - always two - Tinn only supports a single hidden layer. - int nw; // Number of weights. + // Number of biases - always two - Tinn only supports a single hidden layer. + int nb; + // Number of weights. + int nw; - int nips; // Number of inputs. - int nhid; // Number of hidden neurons. - int nops; // Number of outputs. + // Number of inputs. + int nips; + // Number of hidden neurons. + int nhid; + // Number of outputs. + int nops; } Tinn; -// Trains a tinn with an input and target output with a learning rate. -// Returns error rate of the neural network. -float xttrain(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), -// and number of outputs (nops). -Tinn xtbuild(int nips, int nhid, int nops); - -// Returns an output prediction given an input. float* xtpredict(Tinn, const float* in); -// Saves the tinn to disk. +float xttrain(Tinn, const float* in, const float* tg, float rate); + +Tinn xtbuild(int nips, int nhid, int nops); + void xtsave(Tinn, const char* path); -// Loads a new tinn from disk. Tinn xtload(const char* path); -// Frees a tinn from the heap. void xtfree(Tinn);