[TensorFlowJS只如初见]实战二·使用TensorFlowJS拟合直线

[TensorFlowJS只如初见]实战二·使用TensorFlowJS拟合直线

  • 问题描述
    拟合直线 y =(2x -1) + 0.1(-1到1的随机值)
    给定x范围(0,3)
    可以使用学习框架
    建议使用
    y = w * x + b 网络模型
  • 代码
    1、通过操作(ops)来直接完成模型
<html>

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

head>

<body>
  <button class="btn btn-primary" onclick="fnRun0();">开始0button>
  <div id="p0Id">out0div>
  <button class="btn btn-primary" onclick="fnRun1();">开始1button>
  <div id="p1Id">out1div>
  <button class="btn btn-primary" onclick="fnRun2();">开始2button>
  <div id="p2Id">out2div>
body>

<script>
  //train(xst, yst, numIterations);
  function get_ys(xs) {
    var ys = new Array();
    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();
  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();
      return loss_var;
    })
  }

script>

html>

2、通过高级API tf.model

<html>

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

head>

<body>
  <button class="btn btn-primary" onclick="fnRun0();">开始0button>
  <div id="p0Id">out0div>
  <button class="btn btn-primary" onclick="fnRun1();">开始1button>
  <div id="p1Id">out1div>
  <button class="btn btn-primary" onclick="fnRun2();">开始2button>
  <div id="p2Id">out2div>
body>

<script>

  function get_ys(xs) {
    var ys = new Array();
    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();
    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>
  • 结果
    当输入x为:[1.1,1.2,1.3,1.4,1.5],输出为
    [[1.2097658],
    [1.3917543],
    [1.5737425],
    [1.755731 ],
    [1.9377195]]
    可见系统较好的拟合了直线 y =(2x -1)
"Tensor
    [[1.2097658],
     [1.3917543],
     [1.5737425],
     [1.755731 ],
     [1.9377195]]"

你可能感兴趣的:(深度学习,TensorFlowJS,TensorFlow)