Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[译]TensorFlow.js文档 - Getting Started #13

Open
rico-c opened this issue Dec 11, 2018 · 0 comments
Open

[译]TensorFlow.js文档 - Getting Started #13

rico-c opened this issue Dec 11, 2018 · 0 comments
Labels

Comments

@rico-c
Copy link
Owner

rico-c commented Dec 11, 2018

原文地址:https://js.tensorflow.org/#getting-started

翻译:RicardoCao

起步

有两种在JavaScript项目中使用TensorFlow.js的途径:使用script标签引入或在Webpack、Parcel、Roolup项目中使用NPM包引入。

通过script标签

在HTML文件中加入如下代码:

<html>
  <head>
    <!-- 加载TensorFlow.js -->
    <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@0.14.1/dist/tf.min.js"> </script>

    <!-- 将你的代码写在这里 -->
    <script>
      // 定义一个线性回归模型.
      const model = tf.sequential();
      model.add(tf.layers.dense({units: 1, inputShape: [1]}));

      // 准备训练模型: 指定loss和optimizer.
      model.compile({loss: 'meanSquaredError', optimizer: 'sgd'});

      // 造一些假数据用于训练
      const xs = tf.tensor2d([1, 2, 3, 4], [4, 1]);
      const ys = tf.tensor2d([1, 3, 5, 7], [4, 1]);

      // 使用数据训练模型
      model.fit(xs, ys, {epochs: 10}).then(() => {
        // 使用模型去推测一个之前没有出现过的数据的结果:
        // 打开浏览器devtools以查看输出
        model.predict(tf.tensor2d([5], [1, 1])).print();
      });
    </script>
  </head>

  <body>
  </body>
</html>

将上面的HTML在浏览器中打开,以便查看结果。

通过NPM引入

可以通过使用NPM或YARN将TensorFlow.js添加至你的项目中。注意:本例使用ES2017语法(例如import),这里我们假定你已经在项目中使用打包工具。

yarn add @tensorflow/tfjs

npm install @tensorflow/tfjs

main.js中如下配置:

import * as tf from '@tensorflow/tfjs';

// 定义一个线性回归模型.
const model = tf.sequential();
model.add(tf.layers.dense({units: 1, inputShape: [1]}));

// 准备训练模型: 指定loss和optimizer.
model.compile({loss: 'meanSquaredError', optimizer: 'sgd'});

// 造一些假数据用于训练
const xs = tf.tensor2d([1, 2, 3, 4], [4, 1]);
const ys = tf.tensor2d([1, 3, 5, 7], [4, 1]);

// 使用数据训练模型
model.fit(xs, ys, {epochs: 10}).then(() => {
  // 使用模型去推测一个之前没有出现过的数据的结果:
  model.predict(tf.tensor2d([5], [1, 1])).print();
});

在Node.js中运行

安装Node.js绑定以实现TensorFlow的强大能量。

yarn add @tensorflow/tfjs-node

npm install @tensorflow/tfjs-node

如果你的系统使用了带有CUDA compute support的英伟达GPU,可以使用GPU库来提升更高的性能(仅限Linux系统):

yarn add @tensorflow/tfjs-node-gpu

npm install @tensorflow/tfjs-node-gpu

Node.js应用例子:

const tf = require('@tensorflow/tfjs');

// 加载 binding:
require('@tensorflow/tfjs-node');  // Use '@tensorflow/tfjs-node-gpu' if running with GPU.

// 训练模型:
const model = tf.sequential();
model.add(tf.layers.dense({units: 100, activation: 'relu', inputShape: [10]}));
model.add(tf.layers.dense({units: 1, activation: 'linear'}));
model.compile({optimizer: 'sgd', loss: 'meanSquaredError'});

const xs = tf.randomNormal([100, 10]);
const ys = tf.randomNormal([100, 1]);

model.fit(xs, ys, {
  epochs: 100,
  callbacks: {
    onEpochEnd: async (epoch, log) => {
      console.log(`Epoch ${epoch}: loss = ${log.loss}`);
    }
  }
});
@rico-c rico-c added the 翻译 label Dec 11, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant