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
var input = tf.input({shape:[4]});
var dLayer1 = tf.layers.dense({units:12,activation: 'relu' });
var dLayer2 = tf.layers.dense({units:7, activation: 'softmax' });
var output = dLayer2.apply(dLayer1.apply(input));
var model = tf.model({inputs:input, outputs:output});
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.summary() function also use apply() and concatenate() functions.
Javascript
var inp1 = tf.input({shape:[12]});
var inp2 = tf.input({shape:[24]});
var denseLayer1 = tf.layers.dense({units: 4}).apply(inp1);
var denseLayer2 = tf.layers.dense({units: 8}).apply(inp2);
var concatAll = tf.layers.concatenate()
.apply([denseLayer1,denseLayer2]);
var output =tf.layers.dense({units: 8,
activation: 'softmax' }).apply(concatAll);
var model = tf.model({inputs:[inp1, inp2], outputs:output});
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
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
03 Apr, 2023
Like Article
Save Article