הגדרת דפדפן
ישנן שתי דרכים עיקריות להשיג את TensorFlow.js בפרויקטים מבוססי הדפדפן שלך:
אם אתה חדש בפיתוח אתרים, או שמעולם לא שמעתם על כלים כמו webpack או parcel, אנו ממליצים להשתמש בגישת תגי הסקריפט . אם אתה מנוסה יותר או רוצה לכתוב תוכניות גדולות יותר, אולי כדאי לחקור באמצעות כלי בנייה.
שימוש באמצעות תג סקריפט
הוסף את תג הסקריפט הבא לקובץ ה-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: (לינוקס בלבד) אם למערכת שלך יש 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 קפדנית או שתיתקל בשגיאות במהלך ההידור.