Tensorflow JS About Simple tensorflowjs example Teach Neural Network to Play a Sound 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 button onclick getData Start button Load Training Sound hello mp3 and Start Training Neural Network br br button onclick playOriginalSound Play Original Sound button button onclick playTrainedSound Play Trained Sound button 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 div id plot2 style width 90 height 250px div script let audioCtx null let sndData null let sndTrainedData null let iteration 0 function getData if window webkitAudioContext audioCtx new window webkitAudioContext else audioCtx new window AudioContext source audioCtx createBufferSource request new XMLHttpRequest request open GET https notebook xbdev net var resources hello3 mp3 true request responseType arraybuffer request onload function let audioData request response audioCtx decodeAudioData audioData function buffer sndData buffer console log num channels buffer numberOfChannels let data sndData getChannelData 0 get data console log length of data samples data length create neural network sound buffer const bufferSize data length sndTrainedData audioCtx createBuffer 1 bufferSize audioCtx sampleRate start training the neural network iterate setInterval iterate 1 startWorker function e Error with decoding audio data e error request send function playSoundData sdata var source audioCtx createBufferSource set the buffer in the AudioBufferSourceNode source buffer sdata connect the AudioBufferSourceNode to the destination so you can hear the sound source connect audioCtx destination start the source playing source start function getAmplitude dataBuffer t let dataArray dataBuffer getChannelData 0 get data return dataArray t function toBinary num let v 0 if num 0 v num toString 2 while v length 8 v 0 v convert char to int let arr for let i 0 i v length i arr push parseFloat v i return arr function playOriginalSound playSoundData sndData function playTrainedSound preview let noSamples sndTrainedData getChannelData 0 length let bdata sndTrainedData getChannelData 0 for let tt 0 tt noSamples tt 1 let amp perceptron activate tt noSamples let amp perceptron activate toBinary tt let out model predict tf tensor2d tt noSamples 1 1 let amp out dataSync let val amp 0 2 0 1 0 console assert val 1 val 1 bdata tt val playSoundData sndTrainedData var din var dout iteration starts once the data has been loaded async function iterate iteration if din undefined let noSamples sndData getChannelData 0 length console log setup training data var ii var oo for let tt 0 tt noSamples tt 1 learning rate A traditional default value for the learning rate is 0 1 or 0 01 and this may represent a good starting point on your problem An alternative to using a fixed learning rate is to instead vary the learning rate over the training process Simplest learning rate schedule is to decrease the learning rate linearly from a large initial value to a small value let dynamicRate 1 0 100 0 1 iteration add small bit of randomness to jiggle the search let dynamicRate 1 0 10 0 1 iteration Math random 0 1 let dynamicRate 01 1 00005 iteration let dynamicRate 0 3 0 3 0 01 iteration let amp getAmplitude sndData tt 1 to 1 let ampNormalized amp 1 0 0 5 0 to 1 let val perceptron activate tt noSamples let val perceptron activate toBinary tts console assert val 0 0 console assert val 0 1 ii push tt noSamples oo push ampNormalized perceptron propagate dynamicRate ampNormalized totalError totalError Math abs val 0 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 100 await model fit din dout epochs 1 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 plotSounds requestAnimationFrame iterate function plotSounds plot trained data let noSamples sndTrainedData getChannelData 0 length let x let y for let tt 0 tt noSamples tt 1 let amp perceptron activate toBinary tt 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 y push val let plot1 document getElementById plot1 Plotly newPlot plot1 x y margin t 0 showSendToCloud true plot original data let noSamples sndData getChannelData 0 length let bdata sndData getChannelData 0 let x let y for let tt 0 tt noSamples tt 1 let val bdata tt x push tt y push val let plot2 document getElementById plot2 Plotly newPlot plot2 x y margin t 0 showSendToCloud true deep neural network 8 time time index for input 15 15 hidden layers 1 amplitude var perceptron new synaptic Architect Perceptron 8 5 5 5 5 5 5 1 const model tf sequential const model tf sequential layers tf layers dense inputShape 784 units 32 activation relu tf layers dense units 10 activation softmax ref https js tensorflow org api 0 11 6 activation elu hardSigmoid linear relu relu6 selu sigmoid softmax softplus softsign tanh string ref https www tensorflow org js guide models_and_layers first layer in the model needs an inputShape input layer model add tf layers dense units 1 inputShape 1 hidden layers model add tf layers dense units 30 activation selu model add tf layers dense units 30 activation selu model add tf layers dense units 10 activation selu model add tf layers dense units 10 activation selu output layer model add tf layers dense units 1 model compile loss meanSquaredError optimizer sgd const LEARNING_RATE 0 001 const optimizer tf train adam LEARNING_RATE const optimizer tf train adam model compile optimizer optimizer optimizer sgd loss categoricalCrossentropy ref https js tensorflow org api latest Training Losses loss meanSquaredError metrics accuracy const LEARNING_RATE 0 001 model compile optimizer tf train sgd LEARNING_RATE loss meanSquaredError ref https jsfiddle net wagenaartje 1p6q0gw2 2 ref https github com cazala synaptic wiki Neurons Test with custom TANH Neuron squash TANH_2 function x derivate if derivate return 1 Math pow Math tanh x 2 return Math tanh x var network new Architect Perceptron 2 3 1 network setOptimize false var neurons network neurons for var i 0 i neurons length i neurons i neuron squash Neuron squash TANH_2 console log ready script
tial value to a small value let dynamicRate 1 0 100 0 1 iteration add small bit of randomness to jiggle the search let dynamicRate 1 0 10 0 1 iteration Math random 0 1 let dynamicRate 01 1 00005 iteration let dynamicRate 0 3 0 3 0 01 iteration let amp getAmplitude sndData tt 1 to 1 let ampNormalized amp 1 0 0 5 0 to 1 let val perceptron activate tt noSamples let val perceptron activate toBinary tts console assert val 0 0 console assert val 0 1 ii push tt noSamples oo push ampNormalized perceptron propagate dynamicRate ampNormalized totalError totalError Math abs val 0 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 100 await model fit din dout epochs 1 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 plotSounds requestAnimationFrame iterate function plotSounds plot trained data let noSamples sndTrainedData getChannelData 0 length let x let y for let tt 0 tt noSamples tt 1 let amp perceptron activate toBinary tt 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 y push val let plot1 document getElementById plot1 Plotly newPlot plot1 x y margin t 0 showSendToCloud true plot original data let noSamples sndData getChannelData 0 length let bdata sndData getChannelData 0 let x let y for let tt 0 tt noSamples tt 1 let val bdata tt x push tt y push val let plot2 document getElementById plot2 Plotly newPlot plot2 x y margin t 0 showSendToCloud true deep neural network 8 time time index for input 15 15 hidden layers 1 amplitude var perceptron new synaptic Architect Perceptron 8 5 5 5 5 5 5 1 const model tf sequential const model tf sequential layers tf layers dense inputShape 784 units 32 activation relu tf layers dense units 10 activation softmax ref https js tensorflow org api 0 11 6 activation elu hardSigmoid linear relu relu6 selu sigmoid softmax softplus softsign tanh string ref https www tensorflow org js guide models_and_layers first layer in the model needs an inputShape input layer model add tf layers dense units 1 inputShape 1 hidden layers model add tf layers dense units 30 activation selu model add tf layers dense units 30 activation selu model add tf layers dense units 10 activation selu model add tf layers dense units 10 activation selu output layer model add tf layers dense units 1 model compile loss meanSquaredError optimizer sgd const LEARNING_RATE 0 001 const optimizer tf train adam LEARNING_RATE const optimizer tf train adam model compile optimizer optimizer optimizer sgd loss categoricalCrossentropy ref https js tensorflow org api latest Training Losses loss meanSquaredError metrics accuracy const LEARNING_RATE 0 001 model compile optimizer tf train sgd LEARNING_RATE loss meanSquaredError ref https jsfiddle net wagenaartje 1p6q0gw2 2 ref https github com cazala synaptic wiki Neurons Test with custom TANH Neuron squash TANH_2 function x derivate if derivate return 1 Math pow Math tanh x 2 return Math tanh x var network new Architect Perceptron 2 3 1 network setOptimize false var neurons network neurons for var i 0 i neurons length i neurons i neuron squash Neuron squash TANH_2 console log ready script