Open In App

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:

Returns: It returns an object (Softmax).

Example 1:




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:




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

Article Tags :