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:
- shape: An array of integers defining the shape of the output tensor.
- minval: It is an optional argument. The lower bound of the uniform distribution range. The default value is 0.
- maxval: It is also an optional argument. It is the upper bound of the steady distribution range. It is not included in the range. The default value is 1.
- dtype: The data type of the output. The values of datatype possible are ‘float32’ , ‘int32’, ‘ ‘bool’ , ‘complex64’ , ‘string’. It is also an optional argument. The default value is ‘float32’
- seed: It is an optional argument. The seed for the random number generator.
Return: It returns tf.Tensor.
Example 1:
Javascript
// 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:
Javascript
// 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:
Javascript
// 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