ตั้งค่าโปรเจ็กต์ TensorFlow.js

เอกสารนี้จะแสดงวิธีติดตั้งและใช้ TensorFlow.js ในสภาพแวดล้อมของเบราว์เซอร์และใน Node.js

การตั้งค่าเบราว์เซอร์

มีสองวิธีที่แนะนำในการใช้ TensorFlow.js ในโปรเจ็กต์ที่ใช้เบราว์เซอร์:

หากคุณยังใหม่ต่อการพัฒนาเว็บ หรือไม่เคยใช้เครื่องมือสร้าง JavaScript มาก่อน คุณอาจต้องการลองใช้วิธีแท็กสคริปต์ก่อน หากคุณมักจะรวมกลุ่มหรือประมวลผลเนื้อหาเว็บของคุณ หรือคุณวางแผนที่จะเขียนแอปพลิเคชันขนาดใหญ่ คุณควรพิจารณาใช้เครื่องมือสร้าง

ใช้แท็กสคริปต์

หากต้องการรับ TensorFlow.js โดยใช้แท็กสคริปต์ ให้เพิ่มสิ่งต่อไปนี้ลงในไฟล์ HTML หลักของคุณ:

<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@latest/dist/tf.min.js"></script>

ตัวอย่างต่อไปนี้แสดงวิธีการกำหนดและฝึกโมเดลในเบราว์เซอร์:

<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8" />
    <title>TensorFlow.js browser example</title>

    <!-- Load TensorFlow.js from a script tag -->
    <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@latest/dist/tf.min.js"></script>
  </head>
  <body>
    <h1>TensorFlow.js example</h1>
    <h2>Open the console to see the results.</h2>
    <script>
    // Define a model for linear regression. The script tag makes `tf` available
    // as a global variable.
    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
    });
    </script>
  </body>
</html>

หากต้องการรันตัวอย่าง ให้ทำตามขั้นตอนเหล่านี้:

  1. บันทึกเอกสารตัวอย่างในไฟล์ชื่อ index.html
  2. ดับเบิลคลิก index.html เพื่อเปิดในเบราว์เซอร์เริ่มต้นของคุณ

    หรือคุณสามารถให้บริการ index.html ได้โดยการรัน npx http-server ในไดเร็กทอรีเดียวกันกับ index.html (หากคุณได้รับแจ้งให้ติดตั้ง http-server ให้ป้อน y ) จากนั้นไปที่ http://localhost:8080 ในเบราว์เซอร์ของคุณ

  3. เปิดคอนโซลของเบราว์เซอร์เพื่อดูผลลัพธ์ของสคริปต์

  4. รีเฟรชหน้าเพื่อดูการคาดการณ์ใหม่ (และอาจแตกต่างออกไปมาก)

ติดตั้งจาก NPM

หากต้องการติดตั้ง TensorFlow.js จาก NPM ให้ใช้ npm CLI หรือ Yarn

เอ็นพีเอ็ม

npm install @tensorflow/tfjs

เส้นด้าย

yarn add @tensorflow/tfjs

ตัวอย่างต่อไปนี้แสดงวิธีการนำเข้า TensorFlow.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]}));

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

หากต้องการใช้ TensorFlow.js ใน Node.js ให้ใช้ npm CLI หรือ Yarn เพื่อเลือกตัวเลือกการติดตั้งตัวใดตัวหนึ่งด้านล่าง

หากต้องการเรียนรู้เพิ่มเติมเกี่ยวกับการใช้ TensorFlow.js ใน Node.js โปรดดู คำแนะนำ Node.js สำหรับข้อมูลการติดตั้งเพิ่มเติม โปรดดูที่ เก็บ TensorFlow.js สำหรับ Node.js

ตัวเลือกที่ 1: ติดตั้ง TensorFlow.js ด้วยการเชื่อมโยง C++ แบบเนทีฟ

โมดูล tfjs-node ให้การดำเนินการ TensorFlow แบบเนทีฟในแอปพลิเคชัน JavaScript ภายใต้รันไทม์ Node.js ซึ่งเร่งความเร็วโดยไบนารี TensorFlow C

ติดตั้ง tfjs-node :

เอ็นพีเอ็ม

npm install @tensorflow/tfjs-node

เส้นด้าย

yarn add @tensorflow/tfjs-node

ตัวอย่างต่อไปนี้แสดงวิธีการนำเข้า tfjs-node กำหนดโมเดล และฝึกฝนโมเดล

// Use `tfjs-node`. Note that `tfjs` is imported indirectly by `tfjs-node`.
const tf = require('@tensorflow/tfjs-node');

// Define 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]);

// Train the model.
model.fit(xs, ys, {
  epochs: 100,
  callbacks: {
    onEpochEnd: (epoch, log) => console.log(`Epoch ${epoch}: loss = ${log.loss}`)
  }
});

ตัวเลือกที่ 2: ติดตั้ง TensorFlow.js สำหรับ GPU

(Linux เท่านั้น) หากระบบของคุณมี NVIDIA® GPU พร้อม รองรับ CUDA คุณสามารถใช้แพ็คเกจ GPU เพื่อประสิทธิภาพที่ดีขึ้น

ติดตั้ง tfjs-node-gpu :

เอ็นพีเอ็ม

npm install @tensorflow/tfjs-node-gpu

เส้นด้าย

yarn add @tensorflow/tfjs-node-gpu

ตัวอย่างต่อไปนี้แสดงวิธีการนำเข้า tfjs-node-gpu กำหนดโมเดล และฝึกฝนโมเดล

// Use `tfjs-node-gpu`. Note that `tfjs` is imported indirectly by `tfjs-node-gpu`.
const tf = require('@tensorflow/tfjs-node-gpu');

// Define 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]);

// Train the model.
model.fit(xs, ys, {
  epochs: 100,
  callbacks: {
    onEpochEnd: (epoch, log) => console.log(`Epoch ${epoch}: loss = ${log.loss}`)
  }
});

ตัวเลือกที่ 3: ติดตั้งเวอร์ชัน JavaScript ล้วนๆ

โมดูล tfjs เป็นแพ็คเกจเดียวกับที่คุณใช้ในเบราว์เซอร์ เป็นตัวเลือก Node.js ที่ช้าที่สุดในแง่ของประสิทธิภาพ

ติดตั้ง tfjs :

เอ็นพีเอ็ม

npm install @tensorflow/tfjs

เส้นด้าย

yarn add @tensorflow/tfjs

ตัวอย่างต่อไปนี้แสดงวิธีการนำเข้า tfjs กำหนดโมเดล และฝึกฝนโมเดล

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

// Define 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]);

// Train the model.
model.fit(xs, ys, {
  epochs: 100,
  callbacks: {
    onEpochEnd: (epoch, log) => console.log(`Epoch ${epoch}: loss = ${log.loss}`)
  }
});

TypeScript

หากคุณใช้ TensorFlow.js ในโปรเจ็กต์ TypeScript และเปิดใช้งานการตรวจสอบค่า Null อย่างเข้มงวด คุณอาจต้องตั้ง skipLibCheck: true ใน tsconfig.json เพื่อหลีกเลี่ยงข้อผิดพลาดระหว่างการคอมไพล์