Friday, September 5, 2025
HomeLanguagesJavascriptTensorflow.js tf.input() Function

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.

  • 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

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen 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!
RELATED ARTICLES

Most Popular

Dominic
32265 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6634 POSTS0 COMMENTS
Nicole Veronica
11801 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11863 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6703 POSTS0 COMMENTS
Umr Jansen
6718 POSTS0 COMMENTS