TensorFlow.js est une bibliothèque JavaScript pour l'entraînement et l'exécution de modèles de machine learning dans le navigateur ou dans Node.js.
tf.tensor()
const t = tf.tensor([1, 2, 3]);
t.print();
tf.tensor2d()
const t = tf.tensor2d([[1, 2], [3, 4]]);
t.print();
tf.zeros()
, tf.ones()
, tf.fill()
tf.zeros([2, 2]).print();
tf.ones([2, 2]).print();
tf.fill([2, 2], 9).print();
tf.reshape()
const t = tf.tensor([1, 2, 3, 4]);
t.reshape([2, 2]).print();
tf.transpose()
const t = tf.tensor2d([[1, 2], [3, 4]]);
t.transpose().print();
tf.concat()
, tf.stack()
const a = tf.tensor([1, 2]);
const b = tf.tensor([3, 4]);
tf.concat([a, b]).print();
tf.stack([a, b]).print();
tf.add(a, b)
const a = tf.tensor([1, 2]);
const b = tf.tensor([3, 4]);
tf.add(a, b).print(); // [4, 6]
tf.sub(a, b)
const a = tf.tensor([5, 6]);
const b = tf.tensor([2, 4]);
tf.sub(a, b).print(); // [3, 2]
tf.mul(a, b)
const a = tf.tensor([2, 3]);
const b = tf.tensor([4, 5]);
tf.mul(a, b).print(); // [8, 15]
tf.div(a, b)
const a = tf.tensor([10, 20]);
const b = tf.tensor([2, 4]);
tf.div(a, b).print(); // [5, 5]
tf.pow(a, exp)
const a = tf.tensor([2, 3]);
tf.pow(a, 2).print(); // [4, 9]
tf.sqrt(a)
const a = tf.tensor([4, 9]);
tf.sqrt(a).print(); // [2, 3]
tf.exp(a)
const a = tf.tensor([1, 2]);
tf.exp(a).print();
tf.log(a)
const a = tf.tensor([1, 10]);
tf.log(a).print();
tf.abs(a)
const a = tf.tensor([-1, 2]);
tf.abs(a).print(); // [1, 2]
tf.neg(a)
const a = tf.tensor([5, -7]);
tf.neg(a).print(); // [-5, 7]
tf.sequential()
tf.sequential()
const model = tf.sequential();
model.add(tf.layers.dense({ units: 1, inputShape: [1] }));
model.add(layer)
const model = tf.sequential();
model.add(tf.layers.dense({ units: 10, activation: 'relu', inputShape: [2] }));
model.add(tf.layers.dense({ units: 1 }));
model.compile()
model.compile({
optimizer: 'sgd',
loss: 'meanSquaredError',
metrics: ['mse']
});
model.fit(x, y, options)
x
et y
.const xs = tf.tensor2d([[1], [2], [3], [4]]);
const ys = tf.tensor2d([[2], [4], [6], [8]]);
await model.fit(xs, ys, {
epochs: 100,
callbacks: {
onEpochEnd: (epoch, logs) => console.log('Epoch ' + epoch, logs.loss)
}
});
model.predict()
model.predict(x)
const prediction = model.predict(tf.tensor2d([[5]]));
prediction.print(); // devrait donner ~10
model.save('localstorage://nom')
await model.save('localstorage://mon-modele');
tf.loadLayersModel()
const model = await tf.loadLayersModel('localstorage://mon-modele');
tfjs-vis
tfvis.show.modelSummary()
tfvis.show.modelSummary({ name: 'Résumé du modèle' }, model);
tfvis.show.history()
const history = await model.fit(xs, ys, { epochs: 50 });
tfvis.show.history(
{ name: 'Historique de l\'apprentissage' },
history,
['loss']
);
tfvis.render.scatterplot()
tfvis.render.scatterplot(
{ name: 'Données' },
{ values: [{ x: 1, y: 2 }, { x: 2, y: 4 }] },
{ xLabel: 'x', yLabel: 'y' }
);
tf.data.csv()
const data = tf.data.csv('data.csv');
data.forEachAsync(row => console.log(row));
normalisation
const normalized = data.sub(data.min()).div(data.max().sub(data.min()));
Fonction de perte personnalisée
const customLoss = (yTrue, yPred) => tf.losses.meanSquaredError(yTrue, yPred);
model.compile({ loss: customLoss });
onEpochEnd
await model.fit(xs, ys, {
epochs: 100,
callbacks: {
onEpochEnd: (epoch, logs) => {
console.log(`Epoch ${epoch + 1} - Loss: ${logs.loss}`);
}
}
});
@tensorflow-models/mobilenet
import * as mobilenet from '@tensorflow-models/mobilenet';
const model = await mobilenet.load();
const prediction = await model.classify(imgElement);
tf.browser.fromPixels()
const img = tf.browser.fromPixels(videoElement).resizeBilinear([224, 224]).expandDims(0);
const prediction = model.predict(img);
tensorflowjs_converter
tensorflowjs_converter --input_format=tf_saved_model ./mon_modele ./modele_tfjs
tf.memory()
, .dispose()
console.log(tf.memory());
tensor.dispose();