Open In App

Tensorflow.js tf.input() Function

The models in deep learning are collections of connected Layers which can be trained, evaluate, and can be used to predict something. To perform this operation you need to instantiate an input to the models. In this post, We are going to know about how the input factory function works.

The tf.input() function is used when model created using tf.model() function.



Syntax:

tf.input(Args) 

Parameters: The Args object contains the following props.



Returns: It returns the tf.SymbolicTensor.

Example 1: In this example, we are going to use the default parameter shape.




// Importing the tensorflow.Js library
const tf = require("@tensorflow/tfjs")
 
// Define input
const inp = tf.input({ shape: [64] });
 
// Define op
const op = tf.layers.dense({ units: 8, activation: 'softmax' }).apply(inp);
 
// Create model and pass inp and op
const model = tf.model({ inputs: inp, outputs: op });
 
// Predict something
model.predict(tf.ones([2, 64])).print();

Output:

Tensor
   [[0.0285837, 0.1409771, 0.1021329, 0.0912676, 0.2361873, 0.0262359, 
   0.2991393, 0.0754762],
    [0.0285837, 0.1409771, 0.1021329, 0.0912676, 0.2361873, 0.0262359, 
    0.2991393, 0.0754762]]

Example 2: In this example, we are going to use all parameters shape, name, type, and sparse.




// Importing the tensorflow.js library
const tf = require("@tensorflow/tfjs")
 
// Define input and pass all parameters
const inp = tf.input({ shape: [16] }, { name: 'abc' },
    { dtype: 'float32' }, { sparse: false });
 
// Define op
const op = tf.layers.dense({ units: 2, activation: 'softmax' }).apply(inp);
 
// Create model and pass inp and op
const model = tf.model({ inputs: inp, outputs: op });
 
// Predict something
model.summary();

Output:

Layer (type)                 Output shape              Param #    
=================================================================
input8 (InputLayer)          [null,16]                 0          
_________________________________________________________________
dense_Dense8 (Dense)         [null,2]                  34        
=================================================================
Total params: 34
Trainable params: 34
Non-trainable params: 0
_________________________________________________________________

References: https://js.tensorflow.org/api/latest/#input


Article Tags :