Simple neural networks tested with xor and back propagation Arrays of data shifting all the neural network data to large blocks of arrays accessed using indexes Scalable solution define the network dimensions e g layers 2 3 4 Be warned adding lots of extra layers really slows down the training Left the debug check code in e g manual unrolling of arrays loops for the fixed case test 2 3 1 xor Some asserts scattered around to check basic array size aligment data const VARIANCE_W 0 5 const randomUniform min max return 0 123 Math random max min min const randomUniform min max Math random max min min const ru return randomUniform VARIANCE_W VARIANCE_W const xordataset inputs 0 0 outputs 0 inputs 0 1 outputs 1 inputs 1 0 outputs 1 inputs 1 1 outputs 0 const layers 2 3 1 console assert xordataset 0 outputs length layers layers length 1 const weights for let i 0 i layers length 1 i weights i Array layers i fill 0 for let k 0 k layers i k weights i k Array layers i 1 fill 0 for let b 0 b layers i 1 b weights i k b ru const biases const outputs const errors for let i 0 i layers length i biases i Array layers i fill 0 outputs i Array layers i fill 0 errors i Array layers i fill 0 const weights ru ru ru 3x2 ru ru ru ru 3x1 ru ru const biases 0 0 0 0 0 0 let outputs 0 0 input 0 0 0 hidden layer 0 output let errors 0 0 0 0 0 0 const sigmoid x 1 0 1 0 Math exp x const sigmoidPrime x x 1 x const activate iin console assert iin length outputs 0 length console assert weights 0 length layers 0 console assert weights 0 0 length layers 1 console assert weights 1 length layers 1 console assert weights 1 0 length layers 2 for let i 0 i layers length i if i 0 for let k 0 k iin length k outputs 0 k iin k else for let k 0 k layers i k var sum 0 0 for let b 0 b layers i 1 b sum outputs i 1 b weights i 1 b k outputs i k sigmoid sum biases i k outputs 0 0 iin 0 outputs 0 1 iin 1 outputs 1 0 sigmoid outputs 0 0 weights 0 0 0 outputs 0 1 weights 0 1 0 biases 1 0 outputs 1 1 sigmoid outputs 0 0 weights 0 0 1 outputs 0 1 weights 0 1 1 biases 1 1 outputs 1 2 sigmoid outputs 0 0 weights 0 0 2 outputs 0 1 weights 0 1 2 biases 1 2 outputs 2 0 sigmoid outputs 1 0 weights 1 0 0 outputs 1 1 weights 1 1 0 outputs 1 2 weights 1 2 0 biases 2 0 return outputs outputs length 1 const propagate iin target alpha 0 2 errors 2 0 target 0 outputs 2 0 sigmoidPrime outputs 2 0 errors 1 0 errors 2 0 weights 1 0 0 sigmoidPrime outputs 1 0 errors 1 1 errors 2 0 weights 1 1 0 sigmoidPrime outputs 1 1 errors 1 2 errors 2 0 weights 1 2 0 sigmoidPrime outputs 1 2 for let i layers length 1 i 0 i for let k 0 k layers i k if i layers length 1 errors i k target k outputs i k sigmoidPrime outputs i k else errors i k 0 0 for let g 0 g layers i 1 g errors i k errors i 1 g weights i k g sigmoidPrime outputs i k weights 1 0 0 alpha outputs 1 0 errors 2 0 weights 1 1 0 alpha outputs 1 1 errors 2 0 weights 1 2 0 alpha outputs 1 2 errors 2 0 biases 2 0 alpha errors 2 0 weights 0 0 0 alpha outputs 0 0 errors 1 0 weights 0 1 0 alpha outputs 0 1 errors 1 0 biases 1 0 alpha errors 1 0 weights 0 0 1 alpha outputs 0 0 errors 1 1 weights 0 1 1 alpha outputs 0 1 errors 1 1 biases 1 1 alpha errors 1 1 weights 0 0 2 alpha outputs 0 0 errors 1 2 weights 0 1 2 alpha outputs 0 1 errors 1 2 biases 1 2 alpha errors 1 2 for let i 0 i layers length i for let k 0 k layers i k if i layers length 1 for let g 0 g layers i 1 g weights i k g alpha outputs i k errors i 1 g biases i k alpha errors i k test the neural network using iteration loop xor dataset console log new Date for let epoch 0 epoch 10000 epoch let indexes Array from Array xordataset length keys indexes sort Math random 0 5 for let j of indexes activate xordataset j inputs propagate xordataset j inputs xordataset j outputs 0 2 if epoch 1000 0 let cost 0 for let j 0 j xordataset length j let o activate xordataset j inputs for let b 0 b xordataset j outputs length b cost Math pow xordataset j outputs b o b 2 cost 4 console log epoch epoch mean squared error cost for let i 0 i xordataset length i const result activate xordataset i inputs console log for input xordataset i inputs expected xordataset i outputs predicted result 0 toFixed 4 which is Math round result 0 xordataset i outputs 0 correct incorrect
ses 1 1 outputs 1 2 sigmoid outputs 0 0 weights 0 0 2 outputs 0 1 weights 0 1 2 biases 1 2 outputs 2 0 sigmoid outputs 1 0 weights 1 0 0 outputs 1 1 weights 1 1 0 outputs 1 2 weights 1 2 0 biases 2 0 return outputs outputs length 1 const propagate iin target alpha 0 2 errors 2 0 target 0 outputs 2 0 sigmoidPrime outputs 2 0 errors 1 0 errors 2 0 weights 1 0 0 sigmoidPrime outputs 1 0 errors 1 1 errors 2 0 weights 1 1 0 sigmoidPrime outputs 1 1 errors 1 2 errors 2 0 weights 1 2 0 sigmoidPrime outputs 1 2 for let i layers length 1 i 0 i for let k 0 k layers i k if i layers length 1 errors i k target k outputs i k sigmoidPrime outputs i k else errors i k 0 0 for let g 0 g layers i 1 g errors i k errors i 1 g weights i k g sigmoidPrime outputs i k weights 1 0 0 alpha outputs 1 0 errors 2 0 weights 1 1 0 alpha outputs 1 1 errors 2 0 weights 1 2 0 alpha outputs 1 2 errors 2 0 biases 2 0 alpha errors 2 0 weights 0 0 0 alpha outputs 0 0 errors 1 0 weights 0 1 0 alpha outputs 0 1 errors 1 0 biases 1 0 alpha errors 1 0 weights 0 0 1 alpha outputs 0 0 errors 1 1 weights 0 1 1 alpha outputs 0 1 errors 1 1 biases 1 1 alpha errors 1 1 weights 0 0 2 alpha outputs 0 0 errors 1 2 weights 0 1 2 alpha outputs 0 1 errors 1 2 biases 1 2 alpha errors 1 2 for let i 0 i layers length i for let k 0 k layers i k if i layers length 1 for let g 0 g layers i 1 g weights i k g alpha outputs i k errors i 1 g biases i k alpha errors i k test the neural network using iteration loop xor dataset console log new Date for let epoch 0 epoch 10000 epoch let indexes Array from Array xordataset length keys indexes sort Math random 0 5 for let j of indexes activate xordataset j inputs propagate xordataset j inputs xordataset j outputs 0 2 if epoch 1000 0 let cost 0 for let j 0 j xordataset length j let o activate xordataset j inputs for let b 0 b xordataset j outputs length b cost Math pow xordataset j outputs b o b 2 cost 4 console log epoch epoch mean squared error cost for let i 0 i xordataset length i const result activate xordataset i inputs console log for input xordataset i inputs expected xordataset i outputs predicted result 0 toFixed 4 which is Math round result 0 xordataset i outputs 0 correct incorrect