Saturday, November 16, 2024
Google search engine
HomeLanguagesJavascriptTensorflow.js tf.layers.softmax() Function

Tensorflow.js tf.layers.softmax() Function

Tensorflow.js is a Google-developed open-source toolkit for executing machine learning models and deep learning neural networks in the browser or on the node platform. It also enables developers to create machine learning models in JavaScript and utilize them directly in the browser or with Node.js.

The tf.layers.softmax() function is used to apply the softmax activation function on data.

Syntax:

 tf.layers.softmax(args?)

Input Shape: Arbitrary. When utilizing this layer as the initial layer in a model, use the inputShape configuration.

Output Shape: The output has the same shape as the input.

Parameters: It accepts the args object which can have the following properties:

  • axis: It is an integer, the softmax normalization is done along this axis. Its default value is -1.
  • inputShape: If this property is set, it will be utilized to construct an input layer that will be inserted before this layer.
  • batchInputShape: If this property is set, an input layer will be created and inserted before this layer.
  • batchSize: If batchInputShape isn’t supplied and inputShape is, batchSize is utilized to build the batchInputShape.
  • dtype: It is the kind of data type for this layer. float32 is the default value. This parameter applies exclusively to input layers.
  • name: This is the layer’s name and is of string type.
  • trainable: If the weights of this layer may be changed by fit. True is the default value.
  • weights: The layer’s initial weight values.

Returns: It returns an object (Softmax).

Example 1:

Javascript




import * as tf from "@tensorflow/tfjs";
  
const softmaxLayer = tf.layers.softmax({axis: 0});
const x = tf.tensor([1, 2, 1]);
  
softmaxLayer.apply(x).print();


Output:

Tensor
   [0.2119416, 0.5761169, 0.2119416]

Example 2:

Javascript




import * as tf from "@tensorflow/tfjs";
  
const softmaxLayer = tf.layers.softmax({axis: 0});
const x = tf.tensor([11, 15, 10, 12]);
  
softmaxLayer.apply(x).print();


Output:

Tensor
   [0.0170403, 0.9303705, 0.0062688, 0.0463204]

Reference: https://js.tensorflow.org/api/latest/#layers.softmax

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

Recent Comments