The tf.layers.dense() is an inbuilt function of Tensorflow.js library. This function is used to create fully connected layers, in which every output depends on every input.
Syntax:
tf.layers.dense(args)
Parameters: This function takes the args object as a parameter which can have the following properties:
- units: It is a positive number that defines the dimensionality of the output space.
- activation: Specifies which activation function to use.
- useBias: specifies whether to apply a bias or not.
- kernelInitializer: Specifies which initializer to use for the dense kernel weight matrix.
- biasInitializer: Specifies the bias vector for this layer.
- inputDim: Defines input shape as [inputDim].
- kernelConstraint: Specifies the constraint for the kernel.
- biasConstraint: Specifics constraint for the bias vector.
- kernelRegularizer: Specifies the regularizer function applied to the dense kernel weights matrix.
- biasRegularizer: Specifies the regularizer function applied to the bias vector.
- activityRegularizer: Specifies the regularizer function applied to the activation.
- inputShape: If this parameter is defined, it will create another input layer to insert before this layer.
- batchInputShape: If this parameter is defined, it will create another input layer to insert before this layer.
- batchSize : Used to construct batchInputShape, if not already specified.
- dtype: Specifies the data type for this layer. The default value of this parameter is ‘float32’.
- name: Specifies name for this layer.
- trainable: Specifies whether the weights of this layer are updated by fit.
- weights: Specifies the initial weight values of the layer.
- inputDType : It is used to denote the inputDType and its value can be ‘float32’ or ‘int32’ or ‘bool’ or ‘complex64’ or ‘string’.
Return value: It returns a Dense object.
Example 1:
Javascript
import * as tf from "@tensorflow/tfjs" // Create a new dense layer const denseLayer = tf.layers.dense({ units: 2, kernelInitializer: 'heNormal' , useBias: true }); const input = tf.ones([2, 3]); const output = denseLayer.apply(input); // Print the output output.print() |
Output:
Example 2:
Javascript
import * as tf from "@tensorflow/tfjs" // Create a new dense layer const denseLayer = tf.layers.dense({ units: 3, kernelInitializer: 'heNormal' , useBias: false }); const input = tf.ones([2, 3, 3]); const output = denseLayer.apply(input); // Print the output output.print() |
Output:
Example 3:
Javascript
import * as tf from "@tensorflow/tfjs" // Create a new dense layer const denseLayer = tf.layers.dense({ units: 3, kernelInitializer: 'ones' , useBias: false }); const input = tf.ones([2, 3, 3]); const output = denseLayer.apply(input); // Print the output output.print() |
Output:
Example 4:
Javascript
import * as tf from "@tensorflow/tfjs" // Create a new dense layer const denseLayer = tf.layers.dense({ units: 3, kernelInitializer: 'randomUniform' , useBias: false }); const input = tf.ones([2, 3, 3]); const output = denseLayer.apply(input); // Print the output output.print() |
Output:
Reference: https://js.tensorflow.org/api/latest/#layers.dense