Open In App

Tensorflow.js tf.input() Function

Last Updated : 21 Jul, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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.

  • Shape: It represents expected input will be batches of 32-dimensional vectors.
  • batchShape: It represents shape tuple including batch size.
  • name: It represents the name for the layer.
  • dtype: It is used to denote the type of input.
  • sparse: A boolean value represents the placeholder created is sparse.

Returns: It returns the tf.SymbolicTensor.

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

Javascript




// 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.

Javascript




// 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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads