Tensorflow JS About Simple tensorflowjs example Linear regression map input to output supervised ref https medium com lmoroney_google getting started with tensorflow js 9f778d4d0a1 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 button onclick learnLinear Start Learning button div id output_field div script async function learnLinear const model tf sequential 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 100 model add tf layers dense units 100 model add tf layers dense units 100 output layer model add tf layers dense units 1 const model tf sequential layers tf layers dense inputShape 784 units 32 activation relu tf layers dense units 10 activation softmax https blog tensorflow org 2018 04 a gentle introduction to tensorflowjs html model add tf layers dense units 8 inputShape 2 activation tanh model add tf layers dense units 1 activation sigmoid model compile optimizer sgd loss binaryCrossentropy lr 0 1 model compile loss meanSquaredError optimizer sgd const ii ii push 1 ii push 0 ii push 1 ii push 2 ii push 3 ii push 4 const din tf tensor2d 1 0 1 2 3 4 6 1 const dout tf tensor2d 3 1 1 3 5 7 6 1 await model fit din dout epochs 10 let val model predict tf tensor2d 10 1 1 document getElementById output_field innerText 10 val dataSync 0 n await model fit din dout epochs 10 val model predict tf tensor2d 10 1 1 console log res val dataSync 0 document getElementById output_field innerText 10 val dataSync 0 n await model fit din dout epochs 10 val model predict tf tensor2d 10 1 1 console log res val dataSync 0 document getElementById output_field innerText 10 val dataSync 0 n console log finished script
add tf layers dense units 8 inputShape 2 activation tanh model add tf layers dense units 1 activation sigmoid model compile optimizer sgd loss binaryCrossentropy lr 0 1 model compile loss meanSquaredError optimizer sgd const ii ii push 1 ii push 0 ii push 1 ii push 2 ii push 3 ii push 4 const din tf tensor2d 1 0 1 2 3 4 6 1 const dout tf tensor2d 3 1 1 3 5 7 6 1 await model fit din dout epochs 10 let val model predict tf tensor2d 10 1 1 document getElementById output_field innerText 10 val dataSync 0 n await model fit din dout epochs 10 val model predict tf tensor2d 10 1 1 console log res val dataSync 0 document getElementById output_field innerText 10 val dataSync 0 n await model fit din dout epochs 10 val model predict tf tensor2d 10 1 1 console log res val dataSync 0 document getElementById output_field innerText 10 val dataSync 0 n console log finished script