Tensorflow.js tf.layers.dense() Function (original) (raw)

Last Updated : 26 May, 2021

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:

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()

`