Open In App

Tensorflow.js tf.layers.activation() Function

Introduction: Tensorflow.js is an open-source library developed by Google for running machine learning models and deep learning neural networks in the browser or node environment. Tensorflow.js tf.layers.activation() function is used to applied to function to all the element of our input layer . we can also apply function to the input data with dense layer. 

Syntax: 



tf.layers.activation(args);    

Parameters: Below are the parameters accepted by this function:

Returns: Activation



Below are some examples for this function:

Example 1: In this example,  we will make activation layer and check the return value. 




import * as tf from "@tensorflow/tfjs"
 
// Creating config for the activation layer
const config = {
    activation: 'sigmoid',
    inputShape: 5,
    dtype: 'int32',
    name: 'activationLayer'
};
 
// Defining the activation layer
const activationLayer = tf.layers.activation(config);
 
// printing return of activation layer
console.log(activationLayer);

Output: 

{
  "_callHook": null,
  "_addedWeightNames": [],
  "_stateful": false,
  "id": 38,
  "activityRegularizer": null,
  "inputSpec": null,
  "supportsMasking": true,
  "_trainableWeights": [],
  "_nonTrainableWeights": [],
  "_losses": [],
  "_updates": [],
  "_built": false,
  "inboundNodes": [],
  "outboundNodes": [],
  "name": "ActivationLayer",
  "trainable_": true,
  "initialWeights": null,
  "_refCount": null,
  "fastWeightInitDuringBuild": false,
  "activation": {}
}

Example 2: In this example, we will create our activation layer with some configuration and train our input data with activation layer. 




import * as tf from "@tensorflow/tfjs"
 
// Configuration file for the activation layer
const geek_config = {
    activation: 'sigmoid',
    inputShape: 5,
    dtype: 'int32',
    name: 'activationLayer'
};
 
const geek_activation = tf.layers.activation(geek_config);
const geek_inputLayer = tf.layers.dense({units: 1});
 
// Our Input layer for the model
const geek_input = tf.input({shape: [7]});
 
// Making structure for the model
const geek_output = geek_inputLayer.apply(geek_input);
const geek_result = geek_activation.apply(geek_output);
 
// Making Model from structure 
const config2 = {inputs: geek_input, outputs: geek_result}
const model = tf.model(config2);
 
// Collect both outputs and print separately.
const config3 = tf.randomUniform([4, 7])
const  geek_activationResult = model.predict(confg3);
geek_activationResult.print();

Output: 

Tensor
    [[0.4178988],
     [0.2027801],
     [0.2813435],
     [0.2546847]]

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


Article Tags :