tf.nn.experimental.stateless_dropout
Stay organized with collections
Save and categorize content based on your preferences.
Computes dropout: randomly sets elements to zero to prevent overfitting.
tf.nn.experimental.stateless_dropout(
x, rate, seed, rng_alg=None, noise_shape=None, name=None
)
Dropout is useful for regularizing DNN
models. Inputs elements are randomly set to zero (and the other elements are
rescaled). This encourages each node to be independently useful, as it cannot
rely on the output of other nodes.
More precisely: With probability rate
elements of x
are set to 0
.
The remaining elements are scaled up by 1.0 / (1 - rate)
, so that the
expected value is preserved.
x = tf.ones([3,5])
tf.nn.experimental.stateless_dropout(x, rate=0.5, seed=[1, 0])
<tf.Tensor: shape=(3, 5), dtype=float32, numpy=
array([[2., 0., 2., 0., 0.],
[0., 0., 2., 0., 2.],
[0., 0., 0., 0., 2.]], dtype=float32)>
x = tf.ones([3,5])
tf.nn.experimental.stateless_dropout(x, rate=0.8, seed=[1, 0])
<tf.Tensor: shape=(3, 5), dtype=float32, numpy=
array([[5., 0., 0., 0., 0.],
[0., 0., 0., 0., 5.],
[0., 0., 0., 0., 5.]], dtype=float32)>
tf.nn.experimental.stateless_dropout(x, rate=0.0, seed=[1, 0]) == x
<tf.Tensor: shape=(3, 5), dtype=bool, numpy=
array([[ True, True, True, True, True],
[ True, True, True, True, True],
[ True, True, True, True, True]])>
This function is a stateless version of tf.nn.dropout
, in the
sense that no matter how many times you call this function, the same
seed
will lead to the same results, and different seed
will lead
to different results.
x = tf.ones([3,5])
tf.nn.experimental.stateless_dropout(x, rate=0.8, seed=[1, 0])
<tf.Tensor: shape=(3, 5), dtype=float32, numpy=
array([[5., 0., 0., 0., 0.],
[0., 0., 0., 0., 5.],
[0., 0., 0., 0., 5.]], dtype=float32)>
tf.nn.experimental.stateless_dropout(x, rate=0.8, seed=[1, 0])
<tf.Tensor: shape=(3, 5), dtype=float32, numpy=
array([[5., 0., 0., 0., 0.],
[0., 0., 0., 0., 5.],
[0., 0., 0., 0., 5.]], dtype=float32)>
tf.nn.experimental.stateless_dropout(x, rate=0.8, seed=[2, 0])
<tf.Tensor: shape=(3, 5), dtype=float32, numpy=
array([[5., 0., 0., 0., 0.],
[0., 0., 0., 5., 0.],
[0., 0., 0., 0., 0.]], dtype=float32)>
tf.nn.experimental.stateless_dropout(x, rate=0.8, seed=[2, 0])
<tf.Tensor: shape=(3, 5), dtype=float32, numpy=
array([[5., 0., 0., 0., 0.],
[0., 0., 0., 5., 0.],
[0., 0., 0., 0., 0.]], dtype=float32)>
Compare the above results to those of tf.nn.dropout
below. The
second time tf.nn.dropout
is called with the same seed, it will
give a different output.
tf.random.set_seed(0)
x = tf.ones([3,5])
tf.nn.dropout(x, rate=0.8, seed=1)
<tf.Tensor: shape=(3, 5), dtype=float32, numpy=
array([[0., 0., 0., 5., 5.],
[0., 5., 0., 5., 0.],
[5., 0., 5., 0., 5.]], dtype=float32)>
tf.nn.dropout(x, rate=0.8, seed=1)
<tf.Tensor: shape=(3, 5), dtype=float32, numpy=
array([[0., 0., 0., 0., 0.],
[0., 0., 0., 5., 0.],
[0., 0., 0., 0., 0.]], dtype=float32)>
tf.nn.dropout(x, rate=0.8, seed=2)
<tf.Tensor: shape=(3, 5), dtype=float32, numpy=
array([[0., 0., 0., 0., 0.],
[0., 5., 0., 5., 0.],
[0., 0., 0., 0., 0.]], dtype=float32)>
tf.nn.dropout(x, rate=0.8, seed=2)
<tf.Tensor: shape=(3, 5), dtype=float32, numpy=
array([[0., 0., 0., 0., 0.],
[5., 0., 5., 0., 5.],
[0., 5., 0., 0., 5.]], dtype=float32)>
The difference between this function and tf.nn.dropout
is
analogous to the difference between tf.random.stateless_uniform
and tf.random.uniform
. Please see Random number
generation guide
for a detailed description of the various RNG systems in TF. As the
guide states, legacy stateful RNG ops like tf.random.uniform
and
tf.nn.dropout
are not deprecated yet but highly discouraged,
because their states are hard to control.
By default, each element is kept or dropped independently. If noise_shape
is specified, it must be
broadcastable
to the shape of x
, and only dimensions with noise_shape[i] == shape(x)[i]
will make independent decisions. This is useful for dropping whole
channels from an image or sequence. For example:
x = tf.ones([3,10])
tf.nn.experimental.stateless_dropout(x, rate=2/3, noise_shape=[1,10],
seed=[1, 0])
<tf.Tensor: shape=(3, 10), dtype=float32, numpy=
array([[3., 0., 0., 0., 0., 0., 0., 3., 0., 3.],
[3., 0., 0., 0., 0., 0., 0., 3., 0., 3.],
[3., 0., 0., 0., 0., 0., 0., 3., 0., 3.]], dtype=float32)>
Args |
x
|
A floating point tensor.
|
rate
|
A scalar Tensor with the same type as x. The probability
that each element is dropped. For example, setting rate=0.1 would drop
10% of input elements.
|
seed
|
An integer tensor of shape [2] . The seed of the random numbers.
|
rng_alg
|
The algorithm used to generate the random numbers
(default to "auto_select" ). See the alg argument of
tf.random.stateless_uniform for the supported values.
|
noise_shape
|
A 1-D integer Tensor , representing the
shape for randomly generated keep/drop flags.
|
name
|
A name for this operation.
|
Returns |
A Tensor of the same shape and dtype of x .
|
Raises |
ValueError
|
If rate is not in [0, 1) or if x is not a floating point
tensor. rate=1 is disallowed, because the output would be all zeros,
which is likely not what was intended.
|
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.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-04-26 UTC."],[],[],null,["# tf.nn.experimental.stateless_dropout\n\n\u003cbr /\u003e\n\n|----------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/ops/nn_ops.py#L5520-L5661) |\n\nComputes dropout: randomly sets elements to zero to prevent overfitting.\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.nn.experimental.stateless_dropout`](https://www.tensorflow.org/api_docs/python/tf/nn/experimental/stateless_dropout)\n\n\u003cbr /\u003e\n\n tf.nn.experimental.stateless_dropout(\n x, rate, seed, rng_alg=None, noise_shape=None, name=None\n )\n\n[Dropout](https://arxiv.org/abs/1207.0580) is useful for regularizing DNN\nmodels. Inputs elements are randomly set to zero (and the other elements are\nrescaled). This encourages each node to be independently useful, as it cannot\nrely on the output of other nodes.\n\nMore precisely: With probability `rate` elements of `x` are set to `0`.\nThe remaining elements are scaled up by `1.0 / (1 - rate)`, so that the\nexpected value is preserved. \n\n x = tf.ones([3,5])\n tf.nn.experimental.stateless_dropout(x, rate=0.5, seed=[1, 0])\n \u003ctf.Tensor: shape=(3, 5), dtype=float32, numpy=\n array([[2., 0., 2., 0., 0.],\n [0., 0., 2., 0., 2.],\n [0., 0., 0., 0., 2.]], dtype=float32)\u003e\n\n x = tf.ones([3,5])\n tf.nn.experimental.stateless_dropout(x, rate=0.8, seed=[1, 0])\n \u003ctf.Tensor: shape=(3, 5), dtype=float32, numpy=\n array([[5., 0., 0., 0., 0.],\n [0., 0., 0., 0., 5.],\n [0., 0., 0., 0., 5.]], dtype=float32)\u003e\n\n tf.nn.experimental.stateless_dropout(x, rate=0.0, seed=[1, 0]) == x\n \u003ctf.Tensor: shape=(3, 5), dtype=bool, numpy=\n array([[ True, True, True, True, True],\n [ True, True, True, True, True],\n [ True, True, True, True, True]])\u003e\n\nThis function is a stateless version of [`tf.nn.dropout`](../../../tf/nn/dropout), in the\nsense that no matter how many times you call this function, the same\n`seed` will lead to the same results, and different `seed` will lead\nto different results. \n\n x = tf.ones([3,5])\n tf.nn.experimental.stateless_dropout(x, rate=0.8, seed=[1, 0])\n \u003ctf.Tensor: shape=(3, 5), dtype=float32, numpy=\n array([[5., 0., 0., 0., 0.],\n [0., 0., 0., 0., 5.],\n [0., 0., 0., 0., 5.]], dtype=float32)\u003e\n tf.nn.experimental.stateless_dropout(x, rate=0.8, seed=[1, 0])\n \u003ctf.Tensor: shape=(3, 5), dtype=float32, numpy=\n array([[5., 0., 0., 0., 0.],\n [0., 0., 0., 0., 5.],\n [0., 0., 0., 0., 5.]], dtype=float32)\u003e\n tf.nn.experimental.stateless_dropout(x, rate=0.8, seed=[2, 0])\n \u003ctf.Tensor: shape=(3, 5), dtype=float32, numpy=\n array([[5., 0., 0., 0., 0.],\n [0., 0., 0., 5., 0.],\n [0., 0., 0., 0., 0.]], dtype=float32)\u003e\n tf.nn.experimental.stateless_dropout(x, rate=0.8, seed=[2, 0])\n \u003ctf.Tensor: shape=(3, 5), dtype=float32, numpy=\n array([[5., 0., 0., 0., 0.],\n [0., 0., 0., 5., 0.],\n [0., 0., 0., 0., 0.]], dtype=float32)\u003e\n\nCompare the above results to those of [`tf.nn.dropout`](../../../tf/nn/dropout) below. The\nsecond time [`tf.nn.dropout`](../../../tf/nn/dropout) is called with the same seed, it will\ngive a different output. \n\n tf.random.set_seed(0)\n x = tf.ones([3,5])\n tf.nn.dropout(x, rate=0.8, seed=1)\n \u003ctf.Tensor: shape=(3, 5), dtype=float32, numpy=\n array([[0., 0., 0., 5., 5.],\n [0., 5., 0., 5., 0.],\n [5., 0., 5., 0., 5.]], dtype=float32)\u003e\n tf.nn.dropout(x, rate=0.8, seed=1)\n \u003ctf.Tensor: shape=(3, 5), dtype=float32, numpy=\n array([[0., 0., 0., 0., 0.],\n [0., 0., 0., 5., 0.],\n [0., 0., 0., 0., 0.]], dtype=float32)\u003e\n tf.nn.dropout(x, rate=0.8, seed=2)\n \u003ctf.Tensor: shape=(3, 5), dtype=float32, numpy=\n array([[0., 0., 0., 0., 0.],\n [0., 5., 0., 5., 0.],\n [0., 0., 0., 0., 0.]], dtype=float32)\u003e\n tf.nn.dropout(x, rate=0.8, seed=2)\n \u003ctf.Tensor: shape=(3, 5), dtype=float32, numpy=\n array([[0., 0., 0., 0., 0.],\n [5., 0., 5., 0., 5.],\n [0., 5., 0., 0., 5.]], dtype=float32)\u003e\n\nThe difference between this function and [`tf.nn.dropout`](../../../tf/nn/dropout) is\nanalogous to the difference between [`tf.random.stateless_uniform`](../../../tf/random/stateless_uniform)\nand [`tf.random.uniform`](../../../tf/random/uniform). Please see [Random number\ngeneration](https://www.tensorflow.org/guide/random_numbers) guide\nfor a detailed description of the various RNG systems in TF. As the\nguide states, legacy stateful RNG ops like [`tf.random.uniform`](../../../tf/random/uniform) and\n[`tf.nn.dropout`](../../../tf/nn/dropout) are not deprecated yet but highly discouraged,\nbecause their states are hard to control.\n\nBy default, each element is kept or dropped independently. If `noise_shape`\nis specified, it must be\n[broadcastable](http://docs.scipy.org/doc/numpy/user/basics.broadcasting.html)\nto the shape of `x`, and only dimensions with `noise_shape[i] == shape(x)[i]`\nwill make independent decisions. This is useful for dropping whole\nchannels from an image or sequence. For example: \n\n x = tf.ones([3,10])\n tf.nn.experimental.stateless_dropout(x, rate=2/3, noise_shape=[1,10],\n seed=[1, 0])\n \u003ctf.Tensor: shape=(3, 10), dtype=float32, numpy=\n array([[3., 0., 0., 0., 0., 0., 0., 3., 0., 3.],\n [3., 0., 0., 0., 0., 0., 0., 3., 0., 3.],\n [3., 0., 0., 0., 0., 0., 0., 3., 0., 3.]], dtype=float32)\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|---------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `x` | A floating point tensor. |\n| `rate` | A scalar `Tensor` with the same type as x. The probability that each element is dropped. For example, setting rate=0.1 would drop 10% of input elements. |\n| `seed` | An integer tensor of shape `[2]`. The seed of the random numbers. |\n| `rng_alg` | The algorithm used to generate the random numbers (default to `\"auto_select\"`). See the `alg` argument of [`tf.random.stateless_uniform`](../../../tf/random/stateless_uniform) for the supported values. |\n| `noise_shape` | A 1-D integer `Tensor`, representing the shape for randomly generated keep/drop flags. |\n| `name` | A name for this operation. |\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 same shape and dtype of `x`. ||\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|--------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `ValueError` | If `rate` is not in `[0, 1)` or if `x` is not a floating point tensor. `rate=1` is disallowed, because the output would be all zeros, which is likely not what was intended. |\n\n\u003cbr /\u003e"]]