tf.random.uniform
Stay organized with collections Save and categorize content based on your preferences.
Outputs random values from a uniform distribution.
tf.random.uniform( shape, minval=0, maxval=None, dtype=tf.dtypes.float32
, seed=None, name=None )
Used in the notebooks
Used in the guide | Used in the tutorials |
| |
The generated values follow a uniform distribution in the range [minval, maxval)
. The lower bound minval
is included in the range, while the upper bound maxval
is excluded.
For floats, the default range is [0, 1)
. For ints, at least maxval
must be specified explicitly.
In the integer case, the random integers are slightly biased unless maxval - minval
is an exact power of two. The bias is small for values of maxval - minval
significantly smaller than the range of the output (either 2**32
or 2**64
).
Examples:
tf.random.uniform(shape=[2])
<tf.Tensor: shape=(2,), dtype=float32, numpy=array([..., ...], dtype=float32)>
tf.random.uniform(shape=[], minval=-1., maxval=0.)
<tf.Tensor: shape=(), dtype=float32, numpy=-...>
tf.random.uniform(shape=[], minval=5, maxval=10, dtype=tf.int64)
<tf.Tensor: shape=(), dtype=int64, numpy=...>
The seed
argument produces a deterministic sequence of tensors across multiple calls. To repeat that sequence, use tf.random.set_seed
:
tf.random.set_seed(5)
tf.random.uniform(shape=[], maxval=3, dtype=tf.int32, seed=10)
<tf.Tensor: shape=(), dtype=int32, numpy=2>
tf.random.uniform(shape=[], maxval=3, dtype=tf.int32, seed=10)
<tf.Tensor: shape=(), dtype=int32, numpy=0>
tf.random.set_seed(5)
tf.random.uniform(shape=[], maxval=3, dtype=tf.int32, seed=10)
<tf.Tensor: shape=(), dtype=int32, numpy=2>
tf.random.uniform(shape=[], maxval=3, dtype=tf.int32, seed=10)
<tf.Tensor: shape=(), dtype=int32, numpy=0>
Without tf.random.set_seed
but with a seed
argument is specified, small changes to function graphs or previously executed operations will change the returned value. See tf.random.set_seed
for details.
Args |
shape | A 1-D integer Tensor or Python array. The shape of the output tensor. |
minval | A Tensor or Python value of type dtype , broadcastable with shape (for integer types, broadcasting is not supported, so it needs to be a scalar). The lower bound on the range of random values to generate (inclusive). Defaults to 0. |
maxval | A Tensor or Python value of type dtype , broadcastable with shape (for integer types, broadcasting is not supported, so it needs to be a scalar). The upper bound on the range of random values to generate (exclusive). Defaults to 1 if dtype is floating point. |
dtype | The type of the output: float16 , bfloat16 , float32 , float64 , int32 , or int64 . Defaults to float32 . |
seed | A Python integer. Used in combination with tf.random.set_seed to create a reproducible sequence of tensors across multiple calls. |
name | A name for the operation (optional). |
Returns |
A tensor of the specified shape filled with random uniform values. |
Raises |
ValueError | If dtype is integral and maxval is not specified. |
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates. Some content is licensed under the numpy license.
Last updated 2024-04-26 UTC.
[null,null,["Last updated 2024-04-26 UTC."],[],[],null,["# tf.random.uniform\n\n\u003cbr /\u003e\n\n|------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/ops/random_ops.py#L211-L320) |\n\nOutputs random values from a uniform distribution.\n\n#### View aliases\n\n\n**Compat aliases for migration**\n\nSee\n[Migration guide](https://www.tensorflow.org/guide/migrate) for\nmore details.\n\n[`tf.compat.v1.random.uniform`](https://www.tensorflow.org/api_docs/python/tf/random/uniform), [`tf.compat.v1.random_uniform`](https://www.tensorflow.org/api_docs/python/tf/random/uniform)\n\n\u003cbr /\u003e\n\n tf.random.uniform(\n shape,\n minval=0,\n maxval=None,\n dtype=../../tf/dtypes#float32,\n seed=None,\n name=None\n )\n\n### Used in the notebooks\n\n| Used in the guide | Used in the tutorials |\n|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| - [Validating correctness \\& numerical equivalence](https://www.tensorflow.org/guide/migrate/validate_correctness) - [tf.data: Build TensorFlow input pipelines](https://www.tensorflow.org/guide/data) - [Introduction to graphs and tf.function](https://www.tensorflow.org/guide/intro_to_graphs) - [Logistic regression for binary classification with Core APIs](https://www.tensorflow.org/guide/core/logistic_regression_core) - [Quickstart for the TensorFlow Core APIs](https://www.tensorflow.org/guide/core/quickstart_core) | - [Customization basics: tensors and operations](https://www.tensorflow.org/tutorials/customization/basics) - [Parameter server training with ParameterServerStrategy](https://www.tensorflow.org/tutorials/distribute/parameter_server_training) - [Learned data compression](https://www.tensorflow.org/tutorials/generative/data_compression) - [DeepDream](https://www.tensorflow.org/tutorials/generative/deepdream) - [pix2pix: Image-to-image translation with a conditional GAN](https://www.tensorflow.org/tutorials/generative/pix2pix) |\n\nThe generated values follow a uniform distribution in the range\n`[minval, maxval)`. The lower bound `minval` is included in the range, while\nthe upper bound `maxval` is excluded.\n\nFor floats, the default range is `[0, 1)`. For ints, at least `maxval` must\nbe specified explicitly.\n\nIn the integer case, the random integers are slightly biased unless\n`maxval - minval` is an exact power of two. The bias is small for values of\n`maxval - minval` significantly smaller than the range of the output (either\n`2**32` or `2**64`).\n\n#### Examples:\n\n tf.random.uniform(shape=[2])\n \u003ctf.Tensor: shape=(2,), dtype=float32, numpy=array([..., ...], dtype=float32)\u003e\n tf.random.uniform(shape=[], minval=-1., maxval=0.)\n \u003ctf.Tensor: shape=(), dtype=float32, numpy=-...\u003e\n tf.random.uniform(shape=[], minval=5, maxval=10, dtype=tf.int64)\n \u003ctf.Tensor: shape=(), dtype=int64, numpy=...\u003e\n\nThe `seed` argument produces a deterministic sequence of tensors across\nmultiple calls. To repeat that sequence, use [`tf.random.set_seed`](../../tf/random/set_seed): \n\n tf.random.set_seed(5)\n tf.random.uniform(shape=[], maxval=3, dtype=tf.int32, seed=10)\n \u003ctf.Tensor: shape=(), dtype=int32, numpy=2\u003e\n tf.random.uniform(shape=[], maxval=3, dtype=tf.int32, seed=10)\n \u003ctf.Tensor: shape=(), dtype=int32, numpy=0\u003e\n tf.random.set_seed(5)\n tf.random.uniform(shape=[], maxval=3, dtype=tf.int32, seed=10)\n \u003ctf.Tensor: shape=(), dtype=int32, numpy=2\u003e\n tf.random.uniform(shape=[], maxval=3, dtype=tf.int32, seed=10)\n \u003ctf.Tensor: shape=(), dtype=int32, numpy=0\u003e\n\nWithout [`tf.random.set_seed`](../../tf/random/set_seed) but with a `seed` argument is specified, small\nchanges to function graphs or previously executed operations will change the\nreturned value. See [`tf.random.set_seed`](../../tf/random/set_seed) for details.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|----------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `shape` | A 1-D integer Tensor or Python array. The shape of the output tensor. |\n| `minval` | A Tensor or Python value of type `dtype`, broadcastable with `shape` (for integer types, broadcasting is not supported, so it needs to be a scalar). The lower bound on the range of random values to generate (inclusive). Defaults to 0. |\n| `maxval` | A Tensor or Python value of type `dtype`, broadcastable with `shape` (for integer types, broadcasting is not supported, so it needs to be a scalar). The upper bound on the range of random values to generate (exclusive). Defaults to 1 if `dtype` is floating point. |\n| `dtype` | The type of the output: `float16`, `bfloat16`, `float32`, `float64`, `int32`, or `int64`. Defaults to `float32`. |\n| `seed` | A Python integer. Used in combination with [`tf.random.set_seed`](../../tf/random/set_seed) to create a reproducible sequence of tensors across multiple calls. |\n| `name` | A name for the operation (optional). |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| A tensor of the specified shape filled with random uniform values. ||\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|--------------|-------------------------------------------------------|\n| `ValueError` | If `dtype` is integral and `maxval` is not specified. |\n\n\u003cbr /\u003e"]]