Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/keras/src/layers/regularization/gaussian_noise.py: 54%

24 statements  

« 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 GaussianNoise layer.""" 

16 

17 

18import tensorflow.compat.v2 as tf 

19 

20from keras.src import backend 

21from keras.src.engine import base_layer 

22from keras.src.utils import tf_utils 

23 

24# isort: off 

25from tensorflow.python.util.tf_export import keras_export 

26 

27 

28@keras_export("keras.layers.GaussianNoise") 

29class GaussianNoise(base_layer.BaseRandomLayer): 

30 """Apply additive zero-centered Gaussian noise. 

31 

32 This is useful to mitigate overfitting 

33 (you could see it as a form of random data augmentation). 

34 Gaussian Noise (GS) is a natural choice as corruption process 

35 for real valued inputs. 

36 

37 As it is a regularization layer, it is only active at training time. 

38 

39 Args: 

40 stddev: Float, standard deviation of the noise distribution. 

41 seed: Integer, optional random seed to enable deterministic behavior. 

42 

43 Call arguments: 

44 inputs: Input tensor (of any rank). 

45 training: Python boolean indicating whether the layer should behave in 

46 training mode (adding noise) or in inference mode (doing nothing). 

47 

48 Input shape: 

49 Arbitrary. Use the keyword argument `input_shape` 

50 (tuple of integers, does not include the samples axis) 

51 when using this layer as the first layer in a model. 

52 

53 Output shape: 

54 Same shape as input. 

55 """ 

56 

57 def __init__(self, stddev, seed=None, **kwargs): 

58 super().__init__(seed=seed, **kwargs) 

59 self.supports_masking = True 

60 self.stddev = stddev 

61 self.seed = seed 

62 

63 def call(self, inputs, training=None): 

64 def noised(): 

65 return inputs + self._random_generator.random_normal( 

66 shape=tf.shape(inputs), 

67 mean=0.0, 

68 stddev=self.stddev, 

69 dtype=inputs.dtype, 

70 ) 

71 

72 return backend.in_train_phase(noised, inputs, training=training) 

73 

74 def get_config(self): 

75 config = {"stddev": self.stddev, "seed": self.seed} 

76 base_config = super().get_config() 

77 return dict(list(base_config.items()) + list(config.items())) 

78 

79 @tf_utils.shape_type_conversion 

80 def compute_output_shape(self, input_shape): 

81 return input_shape 

82