【webAI】使用Parcel构建Tensorflow.js代码

环境准备

  • win10
  • node8.9.1
  • 安装 yarn

初始化项目

  • yarn init -y
  • yarn add @tensorflow/tfjs
  • yarn global add parcel-bundler
  • 创建 app.js
import * as tf from '@tensorflow/tfjs';

// Define a model for linear regression.
const model = tf.sequential();
model.add(tf.layers.dense({units: 1, inputShape: [1]}));

// Prepare the model for training: Specify the loss and the optimizer.
model.compile({loss: 'meanSquaredError', optimizer: 'sgd'});

// Generate some synthetic data for training.
const xs = tf.tensor2d([1, 2, 3, 4], [4, 1]);
const ys = tf.tensor2d([1, 3, 5, 7], [4, 1]);

// Train the model using the data.
model.fit(xs, ys).then(() => {
  model.predict(tf.tensor2d([5], [1, 1])).print();
});

使用Parcel构建

  • parcel build app.js -d dist

测试

  • node dist/app.js
Tensor
     [[8.2074938],]

猜你喜欢

转载自blog.csdn.net/ns2250225/article/details/80096398