Tensorflow.js is an open-source library that is developed by Google for running machine learning models as well as deep learning neural networks in the browser or node environment.
The tf.model() function is used to create a model which contains layers and layers that are provided in form of input and output parameters.
Syntax:
tf.model( args )
Here args are,
- Inputs: Inputs of the model. It can object or a list of objects.
- Outputs: Outputs of the model.
- Name: Name of the model.
Example 1: In this example, we are going to create a model with the help of tf.model() function with the input of size 4 followed by 2 dense layers with activation function relu and softmax and making a prediction with the model.predict() function.
Javascript
// Create input of size 4var input = tf.input({shape:[4]});Â
// Dense layer 1 with relu activation var dLayer1 = tf.layers.dense({units:12,activation: 'relu'});Â
// Dense layer 1 with softmax activationvar dLayer2 = tf.layers.dense({units:7, activation: 'softmax'});Â
var output = dLayer2.apply(dLayer1.apply(input));Â
// Model functionvar model = tf.model({inputs:input, outputs:output});Â
// Predictionmodel.predict(tf.ones([2,4])).print(); |
Output:
Tensor [[0.309215, 0.0659644, 0.122767, 0.1150663, 0.1592857, 0.1232278, 0.1044738], [0.309215, 0.0659644, 0.122767, 0.1150663, 0.1592857, 0.1232278, 0.1044738]]Â
Example 2: In this example, we are going to create a model with an input array of size 2 and generating summary using model.summary() function also use apply() and concatenate() functions.
Javascript
// Input 1var inp1 = tf.input({shape:[12]});Â
// Input 2var inp2 = tf.input({shape:[24]});Â
// Apply input one to the first dense layer// using apply() functionvar denseLayer1 = tf.layers.dense({units: 4}).apply(inp1);Â
// Apply input two to second dense layervar denseLayer2 = tf.layers.dense({units: 8}).apply(inp2);Â
// Concatenate both dense layer using concatenate() functionvar concatAll = tf.layers.concatenate()Â Â Â Â Â Â Â Â .apply([denseLayer1,denseLayer2]);Â
var output =tf.layers.dense({units: 8, Â Â Â Â Â Â Â Â activation: 'softmax'}).apply(concatAll);Â Â Â Â Â Â // Create modelvar model = tf.model({inputs:[inp1, inp2], outputs:output});Â
// Generate summary for modelmodel.summary(); |
Output:
__________________________________________________________________________________________________
Layer (type) Output shape Param # Receives inputs
==================================================================================================
input7 (InputLayer) [null,12] 0
__________________________________________________________________________________________________
input8 (InputLayer) [null,24] 0
__________________________________________________________________________________________________
dense_Dense10 (Dense) [null,4] 52 input7[0][0]
__________________________________________________________________________________________________
dense_Dense11 (Dense) [null,8] 200 input8[0][0]
__________________________________________________________________________________________________
concatenate_Concatenate4 (Conca [null,12] 0 dense_Dense10[0][0]
dense_Dense11[0][0]
__________________________________________________________________________________________________
dense_Dense12 (Dense) [null,8] 104 concatenate_Concatenate4[0][0]
==================================================================================================
Total params: 356
Trainable params: 356
Non-trainable params: 0
__________________________________________________________________________________________________
Reference: https://js.tensorflow.org/api/latest/#model
