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 UniformNoise layer.""" 16 17 18import tensorflow.compat.v2 as tf 19 20from tensorflow.keras import backend 21 22from tensorflow.keras.layers import Layer 23 24class UniformNoise(Layer): 25 """Apply additive zero-centered uniform noise. 26 27 This is useful to mitigate overfitting 28 (you could see it as a form of random data augmentation). 29 Gaussian Noise (GS) is a natural choice as corruption process 30 for real valued inputs. 31 32 As it is a regularization layer, it is only active at training time. 33 34 Args: 35 stddev: Float, standard deviation of the noise distribution. 36 seed: Integer, optional random seed to enable deterministic behavior. 37 38 Call arguments: 39 inputs: Input tensor (of any rank). 40 training: Python boolean indicating whether the layer should behave in 41 training mode (adding noise) or in inference mode (doing nothing). 42 43 Input shape: 44 Arbitrary. Use the keyword argument `input_shape` 45 (tuple of integers, does not include the samples axis) 46 when using this layer as the first layer in a model. 47 48 Output shape: 49 Same shape as input. 50 """ 51 52 53 54 55 def __init__(self, stddev=0.5, seed=None, **kwargs): 56 super().__init__(**kwargs) 57 self.supports_masking = True 58 self.stddev = stddev 59 60 61 def call(self, inputs, training=None): 62 def noised(): 63 return inputs + backend.random_uniform( 64 shape=tf.shape(inputs), 65 minval=-self.stddev, 66 maxval=self.stddev, 67 dtype=inputs.dtype, 68 ) 69 70 return backend.in_train_phase(noised, inputs, training=training) 71 72 def get_config(self): 73 config = {"stddev": self.stddev} 74 base_config = super().get_config() 75 return dict(list(base_config.items()) + list(config.items())) 76 77 def compute_output_shape(self, input_shape): 78 return input_shape 79