https://github.com/sinclairzx81/neuron
Neural network implemented in JavaScript
https://github.com/sinclairzx81/neuron
machine-learning neural-network
Last synced: about 1 year ago
JSON representation
Neural network implemented in JavaScript
- Host: GitHub
- URL: https://github.com/sinclairzx81/neuron
- Owner: sinclairzx81
- License: other
- Created: 2016-12-07T00:00:52.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-06-04T11:16:00.000Z (about 9 years ago)
- Last Synced: 2025-04-22T20:12:17.785Z (about 1 year ago)
- Topics: machine-learning, neural-network
- Language: TypeScript
- Homepage:
- Size: 26.4 KB
- Stars: 9
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: readme.md
- License: license
Awesome Lists containing this project
README
### neuron
neural network implemented in javascript.
### overview
neuron is a javascript implementation of a multi layer perceptron network. The library allows one to quickly create fully connected feed forward networks and iteratively train them to approximate some desired output.
neuron was written to allow for fast, interactive training in the browser for small networks. The library is offered as is for anyone who finds it useful, educational or just interesting.
### building the project
```
npm install typescript -g
npm install typescript-bundle -g
npm run build-test
node bin/test
```
### approximate xor
the following code sets up a network and iteratively trains it to approximate xor. This network is using tanh activation per each layer, with each layer given an additional bias neuron with a value of 1.0.
```typescript
//------------------------------------------------------
// network topology
//
// 0 0 <--- input layer
// / /|\ \
// 0 0 0 0 0 <--- hidden layer 0
// \ \|/ /
// 0 0 0 <--- hidden layer 1
// \|/
// 0 <--- output layer
//
//------------------------------------------------------
const network = new neuron.Trainer(new neuron.Network([
new neuron.Tensor(2),
new neuron.Tensor(5, "tanh"),
new neuron.Tensor(3, "tanh"),
new neuron.Tensor(1, "tanh"),
]))
//------------------------------------------------------
// train the network and report every 10,000 iterations.
//------------------------------------------------------
let iteration = 0
while(iteration < 100000000) { // 100,000,000 iterations.
// train network against xor truth table. store mean error.
const error = (network.backward([0, 0], [0]) +
network.backward([0, 1], [1]) +
network.backward([1, 0], [1]) +
network.backward([1, 1], [0])) / 4
// view approximation.
if(iteration % 10000 === 0) {
console.log("-", iteration, error)
console.log(0, network.forward([0, 0]))
console.log(1, network.forward([0, 1]))
console.log(1, network.forward([1, 0]))
console.log(0, network.forward([1, 1]))
}
iteration++
}
```