Use TensorFlowJS to fit straight lines

  • Problem description fitting line y = (2x-1) + 0.1(random value from -1 to 1) given x range (0,3) a learning framework can be used to recommend the use of y = w * x + b network model
  • Code 1: Complete the model directly through operations (OPS)
<html>

<head>
  <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs">
  </script>

</head>

<body>
  <button class="btn btn-primary" onclick="fnRun0();">Began to 0</button>
  <div id="p0Id">out0</div>
  <button class="btn btn-primary" onclick="fnRun1();">Began to 1</button>
  <div id="p1Id">out1</div>
  <button class="btn btn-primary" onclick="fnRun2();">Start 2</button>
  <div id="p2Id">out2</div>
</body>

<script>
  //train(xst, yst, numIterations);
  function get_ys(xs) {
    var ys = new Array(a);for (var i = 0; i < xs.length; i++) {
      ys[i] = 2 * xs[i] - 1 + (0.1 * (2 * Math.random() - 1));
    }
    return (ys);
  }
  var xs = new Array(a);for (var i = 0; i < 200; i++) {
    xs[i] = 0.01 * i;
  }
  var ys = get_ys(xs);
  const xst = tf.tensor(xs, [xs.length, 1]);
  const yst = tf.tensor(ys, [ys.length, 1]);
  const w1 = tf.variable(tf.scalar(Math.random()));
  const b1 = tf.variable(tf.scalar(Math.random()));
  const f = x= > w1.mul(x).add(b1);
  const numIterations = 200;
  const learningRate = 1;
  const optimizer = tf.train.adam(learningRate);
  const loss = (pred, label) = > pred.sub(label).square().mean();
  for (let iter = 0; iter < numIterations; iter++) {
    optimizer.minimize(() = > {
      const loss_var = loss(f(xst), yst);
      loss_var.print();
      returnloss_var; })}</script>

</html>
Copy the code

2. Through the advanced API TF.model

<html>

<head>
  <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs">
  </script>

</head>

<body>
  <button class="btn btn-primary" onclick="fnRun0();">Began to 0</button>
  <div id="p0Id">out0</div>
  <button class="btn btn-primary" onclick="fnRun1();">Began to 1</button>
  <div id="p1Id">out1</div>
  <button class="btn btn-primary" onclick="fnRun2();">Start 2</button>
  <div id="p2Id">out2</div>
</body>

<script>

  function get_ys(xs) {
    var ys = new Array(a);for (var i = 0; i < xs.length; i++) {
      ys[i] = 2 * xs[i] - 1 + (0.1 * (2 * Math.random() - 1));
    }
    return (ys);
  }
  
  async function learnLinear() {
    var xs = new Array(a);for (var i = 0; i < 100; i++) {
      xs[i] = 0.02 * i;
    }
    var ys = get_ys(xs);
    const xst = tf.tensor(xs, [xs.length, 1]);
    const yst = tf.tensor(ys, [ys.length, 1]);
    
    const model = tf.sequential();
    model.add(tf.layers.dense({
      units: 1.inputShape: [1]})); model.compile({loss: 'meanSquaredError'.optimizer: 'sgd'
    });
    
    await model.fit(xst, yst, {
      epochs: 100
    });
    model.predict(tf.tensor2d([1.1.1.2.1.3.1.4.1.5], [5.1])).print();
  }
  
  learnLinear();
  
</script>

</html>
Copy the code
  • Result When input x is: [1.1,1.2,1.3,1.4,1.5], the output is [[1.2097658], [1.3917543], [1.5737425], [1.755731], [1.9377195]] It can be seen that the system well fits the line y = (2X-1).
"Tensor [[1.2097658], [1.3917543], [1.5737425], [1.755731], [1.9377195]]"Copy the code