การตั้งค่าเบราว์เซอร์
มีสองวิธีหลักในการรับ TensorFlow.js ในโครงการที่ใช้เบราว์เซอร์ของคุณ:
หากคุณยังใหม่ต่อการพัฒนาเว็บ หรือไม่เคยได้ยินเครื่องมืออย่าง webpack หรือพัสดุ เราขอแนะนำให้คุณใช้วิธีแท็กสคริปต์ หากคุณมีประสบการณ์มากกว่าหรือต้องการเขียนโปรแกรมขนาดใหญ่ขึ้น อาจคุ้มค่าที่จะสำรวจโดยใช้เครื่องมือสร้าง
การใช้งานผ่านแท็กสคริปต์
เพิ่มแท็กสคริปต์ต่อไปนี้ในไฟล์ HTML หลักของคุณ
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@2.0.0/dist/tf.min.js"></script>
ดูตัวอย่างโค้ดสำหรับการตั้งค่าแท็กสคริปต์
// 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
});
การติดตั้งจาก NPM
คุณสามารถใช้เครื่องมือ npm cli หรือ เส้นด้าย เพื่อติดตั้ง TensorFlow.js
yarn add @tensorflow/tfjs
หรือ
npm install @tensorflow/tfjs
ดูตัวอย่างโค้ดสำหรับการติดตั้งผ่าน 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
});
การตั้งค่า Node.js
คุณสามารถใช้เครื่องมือ npm cli หรือ เส้นด้าย เพื่อติดตั้ง TensorFlow.js
ตัวเลือกที่ 1: ติดตั้ง TensorFlow.js ด้วยการเชื่อมโยง C++ ดั้งเดิม
yarn add @tensorflow/tfjs-node
หรือ
npm install @tensorflow/tfjs-node
ตัวเลือกที่ 2: (Linux เท่านั้น) หากระบบของคุณมี NVIDIA® GPU ที่ รองรับ CUDA ให้ใช้แพ็คเกจ GPU เพื่อประสิทธิภาพที่สูงขึ้น
yarn add @tensorflow/tfjs-node-gpu
หรือ
npm install @tensorflow/tfjs-node-gpu
ตัวเลือก 3: ติดตั้งเวอร์ชัน JavaScript แท้ นี่คือประสิทธิภาพของตัวเลือกที่ช้าที่สุดที่ชาญฉลาด
yarn add @tensorflow/tfjs
หรือ
npm install @tensorflow/tfjs
ดูตัวอย่างโค้ดการใช้งาน 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
เมื่อใช้ TypeScript คุณอาจต้องตั้งค่า skipLibCheck: true
ในไฟล์ tsconfig.json
ของคุณ หากโปรเจ็กต์ของคุณใช้การตรวจสอบค่า null อย่างเข้มงวด ไม่เช่นนั้นคุณจะพบข้อผิดพลาดระหว่างการรวบรวม