项目作者: fsossai

项目描述 :
Homemade Fully-connected Multilayer Perceptron
高级语言: C
项目地址: git://github.com/fsossai/nnet.git
创建时间: 2020-12-02T16:53:12Z
项目社区:https://github.com/fsossai/nnet

开源协议:

下载


Neural Network

This is a basic experimental implementation of a Fully-connected Multilayer Perceptron that
can be trained from scratch.
I created this library to have a feeling of how well a naive C implementation perform, so this is not
meant to be a complete package.

Structure

The model is not created specifying the linear transformations but rather specifying
the number of neurons for each layer. Once the layout is known, all the weights
are stored contiguously in memory.

All the data relative to the values in the network is stored in a structure with 3 main arrays:

  • stimulus contains the input of each neuron in a forward pass
  • neurons contains the value of the stimulus after the application of the activation function
  • sensitivity is proportional to the gradient and is propagated backward when training the net.

Each of these vectors is contiguous in memory, and is accessed by computing an offset w.r.t. a given layer.

Data

The library provides functions only to process datasets in a plain-text format:
every row must contain all features and class labels separated by an ascii character.
Both regression and classification tasks can be carried out but for the latter
the label class must be encoded in one-hot format.

Model creation example

This snippet creates a multilayer perceptron with 784, 100 and 10 neurons per layer
respectively, with sig_frac, i.e. x/(1+|x|), as activation function.

  1. Dataset ds_train = {
  2. .xdim = 784 /* number of input features */,
  3. .ydim = 10 /* number of classes */
  4. };
  5. import_dataset(&ds_train,
  6. "data/train.dat" /* plain dataset file */,
  7. " " /* features separator */,
  8. 42000 /* number of elements to read */
  9. );
  10. NeuralNet net;
  11. int layout[] = { 784, 100, 10 }; /* number of neurons per layer (excluding bias) */
  12. init_net(&net, layout,
  13. 3 /* total number of layers (including input and output layers) */,
  14. &sig_frac /* activation function */,
  15. &sig_frac_der /* derivative of activation function */,
  16. &argmax /* threshold function for the output layer */,
  17. 1e-2 /* learning rate */
  18. );
  19. print_net(&net);
  20. /* random initial weights with uniform distribution in [-0.7; 0.7] */
  21. set_rand_weights(&net, -0.7, +0.7);

Execution example

The following is the output of training.c, that trains an NN for 3 epochs on
the partial MNIST dataset provided in the
Kaggle Digit Recognizer competition.

  1. * IMPORT DATASET
  2. Importing ...
  3. Imported 42000 examples
  4. * NEURAL NET
  5. Structure : Fully-connected Multilayer Perceptron
  6. Number of layers : 3
  7. Number of weights : 79510
  8. Number of neurons : 894
  9. Neurons per layer : { 784,100,10 } + biases
  10. * VALIDATION: Hold out
  11. Dataset : 42000 elements
  12. holdout set : 4229 ( 10.07% )
  13. training set : 37771 ( 89.93% )
  14. * LEARNING
  15. Learning rate: 0.010000
  16. Epoch 1/3:
  17. Training: 100% [▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄] 6.6 sec
  18. Validating: 100% [▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄] 0.7 sec, loss: 21.33
  19. Epoch 2/3:
  20. Training: 100% [▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄] 7.0 sec
  21. Validating: 100% [▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄] 0.8 sec, loss: 16.22
  22. Epoch 3/3:
  23. Training: 100% [▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄] 6.9 sec
  24. Validating: 100% [▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄] 0.8 sec, loss: 14.26
  25. Best loss: 14.26%