前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >[TensorFlowJS只如初见]实战二·使用TensorFlowJS拟合直线

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

作者头像
小宋是呢
发布2019-06-27 14:46:25
6070
发布2019-06-27 14:46:25
举报
文章被收录于专栏:深度应用深度应用

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

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

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

</head>

<body>
  <button class="btn btn-primary" onclick="fnRun0();">开始0</button>
  <div id="p0Id">out0</div>
  <button class="btn btn-primary" onclick="fnRun1();">开始1</button>
  <div id="p1Id">out1</div>
  <button class="btn btn-primary" onclick="fnRun2();">开始2</button>
  <div id="p2Id">out2</div>
</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

代码语言:javascript
复制
<html>

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

</head>

<body>
  <button class="btn btn-primary" onclick="fnRun0();">开始0</button>
  <div id="p0Id">out0</div>
  <button class="btn btn-primary" onclick="fnRun1();">开始1</button>
  <div id="p1Id">out1</div>
  <button class="btn btn-primary" onclick="fnRun2();">开始2</button>
  <div id="p2Id">out2</div>
</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)
代码语言:javascript
复制
"Tensor
    [[1.2097658],
     [1.3917543],
     [1.5737425],
     [1.755731 ],
     [1.9377195]]"
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018年11月29日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • [TensorFlowJS只如初见]实战二·使用TensorFlowJS拟合直线
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档