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

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

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

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

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

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

หากต้องการใช้งาน TensorFlow.js โดยใช้แท็ก <script> ให้เพิ่มโค้ดต่อไปนี้ลงในไฟล์ 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 เท่านั้น) หากระบบของคุณมี GPU ของ NVIDIA® ที่ รองรับ 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}`)
  }
});

ไทป์สคริปต์

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