Tensorflow JS About Iterative updates train NN to emulate a sine wave function Instead of letting the training algorithm go off for long periods of time we iteratively update and view the result so you can watch it converge on the solution ref https stackoverflow com questions 13897316 approximating the sine function with a neural network script src https cdn jsdelivr net npm tensorflow tfjs 0 11 2 script script src https cdnjs cloudflare com ajax libs plotly js 1 20 0 plotly min js script script A console assert which actually stop the exectution default console assert is a plain display such as console log or console error It doesnt stop the execution like assert is meant to do This is a little code to workaround this limitation Note you can fix asserts to cause a stop for Chrome set options but this is a general solution workaround in code console assert function cond text dontThrow if cond return if dontThrow debugger else throw new Error text Assertion failed script br br Number of training iterations br div id counter Iteration div Error between ideal and trained version div id error Error div br br div id plot1 style width 90 height 250px div script const noSamples 100 let iteration 0 function plotData plot trained data let x let y let y2 for let tt 0 tt noSamples tt 1 let t 2 Math PI tt noSamples console assert t 0 t 1 let out model predict tf tensor2d tt noSamples 1 1 let amp out dataSync let val amp 0 2 0 1 0 x push tt noSamples y push val y2 push Math sin t let plot1 document getElementById plot1 plot nn data Plotly newPlot plot1 x y margin t 0 showSendToCloud true plot ideal data Plotly addTraces plot1 x y y2 iteration starts once the data has been loaded async function iterate iteration var din var dout var ii var oo for let tt 0 tt noSamples tt 1 let t 2 Math PI tt noSamples console assert t 0 t 1 let amp Math sin t 1 to 1 let ampNormalized amp 1 0 0 5 0 to 1 console assert ampNormalized 0 console assert ampNormalized 1 ii push tt noSamples oo push ampNormalized din tf tensor2d ii ii length 1 dout tf tensor2d oo oo length 1 await model fit dataset take 100 epochs 1 minimum workign example mwe ref https github com tensorflow tfjs issues 448 await model fit din dout batchSize 100 epochs 10 await model fit din dout epochs 10 shuffle true param refs https js tensorflow org api 0 11 2 await model fit din dout epochs 10 shuffle true let elem1 document getElementById counter elem1 innerHTML Iteration iteration let elem2 document getElementById error elem2 innerHTML Error totalError document title it iteration if iteration 10 0 only update graph every 5 iterations plotData requestAnimationFrame iterate const model tf sequential input layer model add tf layers dense units 1 inputShape 1 hidden layers model add tf layers dense units 10 activation selu model add tf layers dense units 100 activation softmax model add tf layers dense units 10 activation selu output layer model add tf layers dense units 1 activation elu hardSigmoid linear relu relu6 selu sigmoid softmax softplus softsign tanh string Name of the const LEARNING_RATE 0 01 const optimizer tf train adam LEARNING_RATE model compile optimizer optimizer optimizer sgd loss categoricalCrossentropy ref https js tensorflow org api latest Training Losses loss meanSquaredError metrics accuracy iterate script
ync function iterate iteration var din var dout var ii var oo for let tt 0 tt noSamples tt 1 let t 2 Math PI tt noSamples console assert t 0 t 1 let amp Math sin t 1 to 1 let ampNormalized amp 1 0 0 5 0 to 1 console assert ampNormalized 0 console assert ampNormalized 1 ii push tt noSamples oo push ampNormalized din tf tensor2d ii ii length 1 dout tf tensor2d oo oo length 1 await model fit dataset take 100 epochs 1 minimum workign example mwe ref https github com tensorflow tfjs issues 448 await model fit din dout batchSize 100 epochs 10 await model fit din dout epochs 10 shuffle true param refs https js tensorflow org api 0 11 2 await model fit din dout epochs 10 shuffle true let elem1 document getElementById counter elem1 innerHTML Iteration iteration let elem2 document getElementById error elem2 innerHTML Error totalError document title it iteration if iteration 10 0 only update graph every 5 iterations plotData requestAnimationFrame iterate const model tf sequential input layer model add tf layers dense units 1 inputShape 1 hidden layers model add tf layers dense units 10 activation selu model add tf layers dense units 100 activation softmax model add tf layers dense units 10 activation selu output layer model add tf layers dense units 1 activation elu hardSigmoid linear relu relu6 selu sigmoid softmax softplus softsign tanh string Name of the const LEARNING_RATE 0 01 const optimizer tf train adam LEARNING_RATE model compile optimizer optimizer optimizer sgd loss categoricalCrossentropy ref https js tensorflow org api latest Training Losses loss meanSquaredError metrics accuracy iterate script