راه اندازی مرورگر
دو راه اصلی برای دریافت TensorFlow.js در پروژه های مبتنی بر مرورگر شما وجود دارد:
اگر در زمینه توسعه وب تازه کار هستید یا هرگز در مورد ابزارهایی مانند بسته وب یا بسته نشنیده اید، توصیه می کنیم از رویکرد برچسب اسکریپت استفاده کنید . اگر باتجربهتر هستید یا میخواهید برنامههای بزرگتری بنویسید، ممکن است ارزشمند باشد که با استفاده از ابزارهای ساخت کاوش کنید.
استفاده از طریق برچسب اسکریپت
تگ اسکریپت زیر را به فایل 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 یا yarn برای نصب 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 یا yarn برای نصب TensorFlow.js استفاده کنید.
گزینه 1: TensorFlow.js را با اتصالات C++ بومی نصب کنید.
yarn add @tensorflow/tfjs-node
یا
npm install @tensorflow/tfjs-node
گزینه 2: (فقط لینوکس) اگر سیستم شما دارای پردازنده گرافیکی NVIDIA® با پشتیبانی از CUDA است، از بسته گرافیکی حتی برای عملکرد بالاتر استفاده کنید.
yarn add @tensorflow/tfjs-node-gpu
یا
npm install @tensorflow/tfjs-node-gpu
گزینه 3: نسخه خالص جاوا اسکریپت را نصب کنید. این کندترین گزینه از نظر عملکرد است.
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
خود تنظیم کنید، اگر پروژه شما از بررسی دقیق تهی استفاده می کند یا در حین کامپایل با خطا مواجه می شوید.