إعداد المستعرض
هناك طريقتان رئيسيتان للحصول على 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 أو الغزل لتثبيت 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 ، قد تحتاج إلى تعيين skipLibCheck: true
في ملف tsconfig.json
إذا كان مشروعك يستخدم التحقق الصارم من القيم الفارغة أو ستواجه أخطاء أثناء التجميع.