https://github.com/primaryobjects/nnsorting
Neural network sorting numbers
https://github.com/primaryobjects/nnsorting
Last synced: 10 months ago
JSON representation
Neural network sorting numbers
- Host: GitHub
- URL: https://github.com/primaryobjects/nnsorting
- Owner: primaryobjects
- Created: 2015-01-16T02:50:52.000Z (about 11 years ago)
- Default Branch: master
- Last Pushed: 2018-03-30T23:29:53.000Z (almost 8 years ago)
- Last Synced: 2025-03-21T06:41:36.928Z (10 months ago)
- Language: JavaScript
- Size: 241 KB
- Stars: 29
- Watchers: 2
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
Neural Network Sorting Numbers
=========
This program trains a neural network to sort a set of numbers. Did you know that a neural network can sort? Wow.

Neural Network Setup
---
The numbers are fed into the neural network by separating each digit as an input. For numbers greater than 9, each digit is a separate input. Digits are normalized to fall between 0-1 by dividing by 9. The number of inputs are equal to the number of digits in the numbers being sorted. For two 3-digit numbers, there would be 6 inputs. The number of outputs are equal to the number of inputs. The output cooresponds to the actual digits, in sorted form.
Numbers in. Sorted numbers out.
Results
---
Two 3-digit numbers
A neural network with two hidden layers of 25 neurons each (not including the output layer), trained for 6,000 iterations at a learning rate of 0.3, produces a training/test accuracy of 78%/74%.
Four 1-digit numbers
A neural network with two hidden layers of 25 neurons each, trained for 8,000 iterations at a learning rate of 0.3, produces a training/test accuracy of 78%/75%.
Running It
---
```
node app
```
Generating Training/Test Data
---
Data can be generated by calling the generate() method. The first parameter is the number of rows to generate in the set. The second parameter is the number of numbers to sort. The third parameter is the number of digits per number.
```
console.log(NeuralNetworkManager.generate(5, 2, 3)); // Generate five examples of two 3-digit numbers.
// Example generated data.
[ { input: [ 145, 657 ], output: [ 145, 657 ] },
{ input: [ 853, 719 ], output: [ 719, 853 ] },
{ input: [ 895, 278 ], output: [ 278, 895 ] },
{ input: [ 724, 910 ], output: [ 724, 910 ] },
{ input: [ 883, 970 ], output: [ 883, 970 ] } ]
```
After generating, the data will need to be formatted for input into the neural network. You can call the format() method to do this. The numbers will be separated by digit, so that each digit may be fed as an input into the neural network.
You can combine the two calls to generate and format by simply calling:
```
console.log(NeuralNetworkManager.generateFormatted(5, 2, 3));
// Example generated data.
[ { input: [ 1, 4, 5, 6, 5, 7 ], output: [ 1, 4, 5, 6, 5, 7 ] },
{ input: [ 8, 5, 3, 7, 1, 9 ], output: [ 7, 1, 9, 8, 5, 3 ] },
{ input: [ 8, 9, 5, 2, 7, 8 ], output: [ 2, 7, 8, 8, 9, 5 ] },
{ input: [ 7, 2, 4, 9, 1, 0 ], output: [ 7, 2, 4, 9, 1, 0 ] },
{ input: [ 8, 8, 3, 9, 7, 0 ], output: [ 8, 8, 3, 9, 7, 0 ] } ]
```
License
----
MIT
Author
----
Kory Becker
http://www.primaryobjects.com/kory-becker