Skip to content
Related Articles

Related Articles

Tensorflow.js tf.model() Function

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 16 Aug, 2021

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 4
var 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 activation
var dLayer2 = tf.layers.dense({units:7, activation: 'softmax'});
 
var output = dLayer2.apply(dLayer1.apply(input));
 
// Model function
var model = tf.model({inputs:input, outputs:output});
 
// Prediction
model.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.summery() function also use apply() and concatenate() functions.

Javascript




// Input 1
var inp1 = tf.input({shape:[12]});
 
// Input 2
var inp2 = tf.input({shape:[24]});
 
// Apply input one to the first dense layer
// using apply() function
var denseLayer1 = tf.layers.dense({units: 4}).apply(inp1);
 
// Apply input two to second dense layer
var denseLayer2 = tf.layers.dense({units: 8}).apply(inp2);
 
// Concatenate both dense layer using concatenate() function
var concatAll = tf.layers.concatenate()
        .apply([denseLayer1,denseLayer2]);
 
var output =tf.layers.dense({units: 8,
        activation: 'softmax'}).apply(concatAll);
      
// Create model
var model = tf.model({inputs:[inp1, inp2], outputs:output});
 
// Generate summery for model
model.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
 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!