![]() |
VOOZH | about |
TensorFlow.js is the popular library of JavaScript that helps us add machine learning functions to web applications. Tensor is the datatype which is used in the TensorFlow.
Now, let us understand the TensorFlow.js and its components.
TensorFlow.js is the JavaScript library that allows us to train, and deploy machine learning models in the browser and on NodeJS. It is open-source and was developed by Google.
Output
[1, 2, 3, 4]In this example
To use TensorFlow.js in a web application, include it using a script tag:
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>For NodeJS, install the package via npm:
npm install @tensorflow/tfjsTensor is the data structure in the TensorFlow.js which represents the multi-dimensional arrays.
We can use the tf.tensor
Output
[1,2,3,4]Output
[[1, 2], [3, 4]]TensorFlow.js supports two types of models
A linear stack of layers, useful for simple neural networks.
const model = tf.sequential();
model.add(tf.layers.dense({ units: 10, inputShape: [5], activation: 'relu' }));
model.add(tf.layers.dense({ units: 1 }));Functional Models are used for the complex architectures like multi-input and multi-output models.
const input = tf.input({ shape: [10] });
const denseLayer = tf.layers.dense({ units: 5, activation: 'relu' }).apply(input);
const output = tf.layers.dense({ units: 1 }).apply(denseLayer);
const model = tf.model({ inputs: input, outputs: output });To manipulate tensors, tensorflow.js provides various mathematical operations.
Output
[5, 7, 9]Output
[6, 12, 18]Output
11