neural network js

Solutions on MaxInterview for neural network js by the best coders in the world

showing results for - "neural network js"
Laura
14 Oct 2019
1// npm i @death_raider/neural-network
2
3const NeuralNetwork = require('@death_raider/neural-network').NeuralNetwork
4//creates ANN with 2 input nodes, 1 hidden layers with 2 hidden nodes and 1 output node
5let network = new NeuralNetwork({
6  input_nodes : 2,
7  layer_count : [2],
8  output_nodes :1,
9  weight_bias_initilization_range : [-1,1]
10});
11//format for activation function = [ function ,  derivative of function ]
12network.Activation.hidden = [(x)=>1/(1+Math.exp(-x)),(x)=>x*(1-x)] //sets activation for hidden layers as sigmoid function
13function xor(){
14  let inp = [Math.floor(Math.random()*2),Math.floor(Math.random()*2)]; //random inputs 0 or 1 per cell
15  let out = (inp.reduce((a,b)=>a+b)%2 == 0)?[0]:[1]; //if even number of 1's in input then 0 else 1 as output
16  return [inp,out]; //train or validation functions should have [input,output] format
17}
18network.train({
19  TotalTrain : 1e+6, //total data for training (not epochs)
20  batch_train : 1, //batch size for training
21  trainFunc : xor, //training function to get data
22  TotalVal : 1000, //total data for validation (not epochs)
23  batch_val : 1, //batch size for validation
24  validationFunc : xor, //validation function to get data
25  learning_rate : 0.1 //learning rate (default = 0.0000001)
26});
27console.log("Average Validation Loss ->",network.Loss.Validation_Loss.reduce((a,b)=>a+b)/network.Loss.Validation_Loss.length);
28// Result after running it a few times
29// Average Validation Loss -> 0.00004760326022482792
30// Average Validation Loss -> 0.000024864418333478723
31// Average Validation Loss -> 0.000026908106414283446