tf.nn.experimental.general_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.general_dropout(
x, rate, uniform_sampler, noise_shape=None, name=None
)
Please see tf.nn.experimental.stateless_dropout
for an overview
of dropout.
Unlike tf.nn.experimental.stateless_dropout
, here you can supply a
custom sampler function uniform_sampler
that (given a shape and a
dtype) generates a random, Uniform[0, 1)
-distributed tensor (of
that shape and dtype). uniform_sampler
can be
e.g. tf.random.stateless_random_uniform
or
tf.random.Generator.uniform
.
For example, if you are using tf.random.Generator
to generate
random numbers, you can use this code to do dropouts:
g = tf.random.Generator.from_seed(7)
sampler = g.uniform
x = tf.constant([1.1, 2.2, 3.3, 4.4, 5.5])
rate = 0.5
tf.nn.experimental.general_dropout(x, rate, sampler)
<tf.Tensor: shape=(5,), ..., numpy=array([ 0. , 4.4, 6.6, 8.8, 11. ], ...)>
tf.nn.experimental.general_dropout(x, rate, sampler)
<tf.Tensor: shape=(5,), ..., numpy=array([2.2, 0. , 0. , 8.8, 0. ], ...)>
It has better performance than using
tf.nn.experimental.stateless_dropout
and
tf.random.Generator.make_seeds
:
g = tf.random.Generator.from_seed(7)
x = tf.constant([1.1, 2.2, 3.3, 4.4, 5.5])
rate = 0.5
tf.nn.experimental.stateless_dropout(x, rate, g.make_seeds(1)[:, 0])
<tf.Tensor: shape=(5,), ..., numpy=array([ 2.2, 4.4, 6.6, 0. , 11. ], ...)>
tf.nn.experimental.stateless_dropout(x, rate, g.make_seeds(1)[:, 0])
<tf.Tensor: shape=(5,), ..., numpy=array([2.2, 0. , 6.6, 8.8, 0. ], ...>
because generating and consuming seeds cost extra
computation. tf.nn.experimental.general_dropout
can let you avoid
them.
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.
|
uniform_sampler
|
a callable of signature (shape, dtype) ->
Tensor[shape, dtype] , used to generate a tensor of uniformly-distributed
random numbers in the range [0, 1) , of the given shape and dtype.
|
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.general_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#L5664-L5732) |\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.general_dropout`](https://www.tensorflow.org/api_docs/python/tf/nn/experimental/general_dropout)\n\n\u003cbr /\u003e\n\n tf.nn.experimental.general_dropout(\n x, rate, uniform_sampler, noise_shape=None, name=None\n )\n\nPlease see [`tf.nn.experimental.stateless_dropout`](../../../tf/nn/experimental/stateless_dropout) for an overview\nof dropout.\n\nUnlike [`tf.nn.experimental.stateless_dropout`](../../../tf/nn/experimental/stateless_dropout), here you can supply a\ncustom sampler function `uniform_sampler` that (given a shape and a\ndtype) generates a random, `Uniform[0, 1)`-distributed tensor (of\nthat shape and dtype). `uniform_sampler` can be\ne.g. `tf.random.stateless_random_uniform` or\n[`tf.random.Generator.uniform`](../../../tf/random/Generator#uniform).\n\nFor example, if you are using [`tf.random.Generator`](../../../tf/random/Generator) to generate\nrandom numbers, you can use this code to do dropouts: \n\n g = tf.random.Generator.from_seed(7)\n sampler = g.uniform\n x = tf.constant([1.1, 2.2, 3.3, 4.4, 5.5])\n rate = 0.5\n tf.nn.experimental.general_dropout(x, rate, sampler)\n \u003ctf.Tensor: shape=(5,), ..., numpy=array([ 0. , 4.4, 6.6, 8.8, 11. ], ...)\u003e\n tf.nn.experimental.general_dropout(x, rate, sampler)\n \u003ctf.Tensor: shape=(5,), ..., numpy=array([2.2, 0. , 0. , 8.8, 0. ], ...)\u003e\n\nIt has better performance than using\n[`tf.nn.experimental.stateless_dropout`](../../../tf/nn/experimental/stateless_dropout) and\n[`tf.random.Generator.make_seeds`](../../../tf/random/Generator#make_seeds): \n\n g = tf.random.Generator.from_seed(7)\n x = tf.constant([1.1, 2.2, 3.3, 4.4, 5.5])\n rate = 0.5\n tf.nn.experimental.stateless_dropout(x, rate, g.make_seeds(1)[:, 0])\n \u003ctf.Tensor: shape=(5,), ..., numpy=array([ 2.2, 4.4, 6.6, 0. , 11. ], ...)\u003e\n tf.nn.experimental.stateless_dropout(x, rate, g.make_seeds(1)[:, 0])\n \u003ctf.Tensor: shape=(5,), ..., numpy=array([2.2, 0. , 6.6, 8.8, 0. ], ...\u003e\n\nbecause generating and consuming seeds cost extra\ncomputation. [`tf.nn.experimental.general_dropout`](../../../tf/nn/experimental/general_dropout) can let you avoid\nthem.\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| `uniform_sampler` | a callable of signature `(shape, dtype) -\u003e Tensor[shape, dtype]`, used to generate a tensor of uniformly-distributed random numbers in the range `[0, 1)`, of the given shape and dtype. |\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"]]