Open In App

Tensorflow.js tf.randomUniform() Function

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.

The tf.randomUniform() function is used to create a tf.Tensor with values sampled from a uniform distribution.



Syntax:

tf.randomUniform (shape, minval, maxval, dtype, seed)

Parameter: This function accepts five parameters which are illustrated below:



Return: It returns tf.Tensor.

Example 1:




// Importting the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Creating the tensor with values sampled
// from a uniform distribution
const x=tf.randomUniform([5]);
  
// Printing the tensor
x.print();

Output:

Tensor
    [0.0008758, 0.3491586, 0.3466536, 0.9614096, 0.7892056]

Example 2:




// Importting the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Creating the tensor with values sampled
// from a normal distribution
const x=tf.randomUniform([2, 2]);
  
// Printing the tensor
x.print();

Output:

Tensor
    [[0.7312108, 0.5003704],
    [0.8552292, 0.082417 ]]

Example 3:




// Importting the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Creating the tensor with values sampled
// from a normal distribution
const x=tf.randomUniform([5], 10, 15, 'int32', 0);
  
// Printing the tensor
x.print();

Output:

Tensor
    [12, 14, 10, 13, 12]

Reference: https://js.tensorflow.org/api/latest/#randomUniform


Article Tags :