Thiết lập trình duyệt
Có hai cách chính để tải TensorFlow.js trong các dự án dựa trên trình duyệt của bạn:
Nếu bạn chưa quen với việc phát triển web hoặc chưa bao giờ nghe nói về các công cụ như webpack hoặc parcel, chúng tôi khuyên bạn nên sử dụng phương pháp tiếp cận thẻ script . Nếu bạn có nhiều kinh nghiệm hơn hoặc muốn viết các chương trình lớn hơn, bạn nên khám phá bằng cách sử dụng các công cụ xây dựng.
Sử dụng qua Thẻ tập lệnh
Thêm thẻ script sau vào tệp HTML chính của bạn.
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@2.0.0/dist/tf.min.js"></script>
Xem mẫu mã để thiết lập thẻ tập lệnh
// Define a model for linear regression.
const model = tf.sequential();
model.add(tf.layers.dense({units: 1, inputShape: [1]}));
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, {epochs: 10}).then(() => {
// Use the model to do inference on a data point the model hasn't seen before:
model.predict(tf.tensor2d([5], [1, 1])).print();
// Open the browser devtools to see the output
});
Cài đặt từ NPM
Bạn có thể sử dụng công cụ npm cli hoặc sợi để cài đặt TensorFlow.js.
yarn add @tensorflow/tfjs
hoặc
npm install @tensorflow/tfjs
Xem mã mẫu để cài đặt qua NPM
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]}));
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, {epochs: 10}).then(() => {
// Use the model to do inference on a data point the model hasn't seen before:
model.predict(tf.tensor2d([5], [1, 1])).print();
// Open the browser devtools to see the output
});
Thiết lập Node.js
Bạn có thể sử dụng công cụ npm cli hoặc sợi để cài đặt TensorFlow.js.
Tùy chọn 1: Cài đặt TensorFlow.js với các liên kết C ++ gốc.
yarn add @tensorflow/tfjs-node
hoặc
npm install @tensorflow/tfjs-node
Tùy chọn 2: (Chỉ dành cho Linux) Nếu hệ thống của bạn có GPU NVIDIA® với hỗ trợ CUDA , hãy sử dụng gói GPU ngay cả để có hiệu suất cao hơn.
yarn add @tensorflow/tfjs-node-gpu
hoặc
npm install @tensorflow/tfjs-node-gpu
Tùy chọn 3: Cài đặt phiên bản JavaScript thuần túy. Đây là tùy chọn hiệu suất chậm nhất khôn ngoan.
yarn add @tensorflow/tfjs
hoặc
npm install @tensorflow/tfjs
Xem mã mẫu để sử dụng Node.js
const tf = require('@tensorflow/tfjs');
// Optional Load the binding:
// Use '@tensorflow/tfjs-node-gpu' if running with GPU.
require('@tensorflow/tfjs-node');
// Train a simple model:
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: (epoch, log) => console.log(`Epoch ${epoch}: loss = ${log.loss}`)
}
});
TypeScript
Khi sử dụng TypeScript, bạn có thể cần đặt skipLibCheck: true
trong tệp tsconfig.json
của mình nếu dự án của bạn sử dụng kiểm tra null nghiêm ngặt hoặc bạn sẽ gặp lỗi trong quá trình biên dịch.