Tiny Neural Network Library in 200 Lines of Code

2018年4月9日 | By News | Filed in: News.

https://ift.tt/2GNhzh3

Neural networks have gone mainstream with a lot of heavy-duty — and heavy-weight — tools and libraries. What if you want to fit a network into a little computer? There’s tinn — the tiny neural network. If you can compile 200 lines of standard C code with a C or C++ compiler, you are in business. There are no dependencies on other code.

On the other hand, there’s not much documentation, either. However, between the header file and two examples, you should be able to figure it out. After all, it isn’t much code. The example in the repository directs you to download a handwriting number recognition dataset from the Internet. Once it trains that data, it shows you the expected output from the first item in the data set and then processes the first item and shows you the result.

For simplicity, the test program just uses the first training item. However, keep in mind that the program shuffles the data during training, so you won’t always get the same result. Here’s an example output:

0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 1.000000 
0.000294 0.000581 0.000000 0.000045 0.000101 0.002943 0.000000 0.000000 0.000408 0.998187

The top row is the expected result. All the numbers are zero except the last one because this is a number “9” input. The bottom row shows the results of the network. Most of the values are not zero, but are close to it. The last value is not quite one, but it is close.

You might prefer looking at the much simpler program in the README file:

#include "Tinn.h"
#include <stdio.h>

#define len(a) ((int) (sizeof(a) / sizeof(*a)))

int main()
{
   float in[] = { 0.05, 0.10 };
   float tg[] = { 0.01, 0.99 };
 /* Two hidden neurons */
   const Tinn tinn = xtbuild(len(in), 2, len(tg));
   for(int i = 0; i < 1000; i++)
     {
     float error = xttrain(tinn, in, tg, 0.5);
     printf("%.12f\n", error);
     }
   xtfree(tinn);
   return 0;
}

The first array is the expected inputs and the second array are the expected outputs. This simple program doesn’t actually use the network it trains, but the xtpredict function would be easy to add.

If you want some more reading on neural networks, we can help you. There’s also a rundown of other tools and techniques available.

IT.数码

via Hack a Day https://hackaday.com

April 9, 2018 at 02:00PM


发表评论

电子邮件地址不会被公开。 必填项已用*标注