# Understand ML With Simplest Code

Document created by Biyong Sun on Jul 26, 2018
Version 1Show Document
TensorFlow  Provides a very simple ML  by Java Script.
It is easy to have the environment to see it demo.
This document is to introduce it.
The formula to get the training data

We have a formula   Y = 2X – 1 to get the training data

example:  let x=-1 then  Y = 2*-1 – 1 = -2 – 1 = -3

x = { -1, 0, 1, 2, 3, 4}
y =  {-3, -1, 1, 3, 5, 7}

Build up a very simple network
This network will get training and predict the result for Y = 2X – 1
Should remind you here is the Machine do NOT know about the formula.
It cannot calculate like us.
The complete code
<html>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@0.11.2">
</script>
<body>
<div id="output_field"></div>
<div id="output_field1"></div>

</body>
<script>
async function learnLinear(){
const model = tf.sequential();
model.compile({
loss: 'meanSquaredError',
optimizer: 'sgd'
});

const xs = tf.tensor2d([-1, 0, 1, 2, 3,4], [6, 1]);
const ys = tf.tensor2d([-3, -1, 1, 3, 5,7], [6, 1]);

await model.fit(xs, ys, {epochs: 500});

document.getElementById('output_field').innerText =
model.predict(tf.tensor2d([10], [1, 1]));

}
learnLinear();
</script>
<html>

Adjust the training to see what happen
We will go to change the following code to adjust the training, then let machine tell the result for  X = 10 to see if the training result  is different or not.
The result by calculation is Y = 2X – 1 = 2X10 -1 = 19
await model.fit(xs, ys, {epochs: 10});

We will try 10, 100,   500  and 1500.

The result summary
Y = 2X – 1 = 2X10 -1 = 19

10       : 13.9085026,   10.9296398,  13.0426989,  12.0150528, 7.4879761
100      : 18.0845203, 17.7116661, 17.9885635, 17.9806786, 18.2209091
500      : 18.9848061, 18.983654, 18.9877472, 18.9812298, 18.9825478
1500     : 18.9999866, 18.9999866, 18.9999866, 18.9999866, 18.999986
With 1500 training, the machine can predict the result very closely.
But it cannot reach the correct result 19. Because the machine doesn’t know about the formula Y = 2X - 1