Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/keras/src/layers/regularization/gaussian_dropout.py: 50%
28 statements
« prev ^ index » next coverage.py v7.4.0, created at 2024-01-03 07:57 +0000
« prev ^ index » next coverage.py v7.4.0, created at 2024-01-03 07:57 +0000
1# Copyright 2015 The TensorFlow Authors. All Rights Reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14# ==============================================================================
15"""Contains the GaussianDropout layer."""
18import numpy as np
19import tensorflow.compat.v2 as tf
21from keras.src import backend
22from keras.src.engine import base_layer
23from keras.src.utils import tf_utils
25# isort: off
26from tensorflow.python.util.tf_export import keras_export
29@keras_export("keras.layers.GaussianDropout")
30class GaussianDropout(base_layer.BaseRandomLayer):
31 """Apply multiplicative 1-centered Gaussian noise.
33 As it is a regularization layer, it is only active at training time.
35 Args:
36 rate: Float, drop probability (as with `Dropout`).
37 The multiplicative noise will have
38 standard deviation `sqrt(rate / (1 - rate))`.
39 seed: Integer, optional random seed to enable deterministic behavior.
41 Call arguments:
42 inputs: Input tensor (of any rank).
43 training: Python boolean indicating whether the layer should behave in
44 training mode (adding dropout) or in inference mode (doing nothing).
46 Input shape:
47 Arbitrary. Use the keyword argument `input_shape`
48 (tuple of integers, does not include the samples axis)
49 when using this layer as the first layer in a model.
51 Output shape:
52 Same shape as input.
53 """
55 def __init__(self, rate, seed=None, **kwargs):
56 super().__init__(seed=seed, **kwargs)
57 self.supports_masking = True
58 self.rate = rate
59 self.seed = seed
61 def call(self, inputs, training=None):
62 if 0 < self.rate < 1:
64 def noised():
65 stddev = np.sqrt(self.rate / (1.0 - self.rate))
66 return inputs * self._random_generator.random_normal(
67 shape=tf.shape(inputs),
68 mean=1.0,
69 stddev=stddev,
70 dtype=inputs.dtype,
71 )
73 return backend.in_train_phase(noised, inputs, training=training)
74 return inputs
76 def get_config(self):
77 config = {"rate": self.rate, "seed": self.seed}
78 base_config = super().get_config()
79 return dict(list(base_config.items()) + list(config.items()))
81 @tf_utils.shape_type_conversion
82 def compute_output_shape(self, input_shape):
83 return input_shape