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

14 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 SpatialDropout1D layer.""" 

16 

17 

18import tensorflow.compat.v2 as tf 

19 

20from keras.src.engine.input_spec import InputSpec 

21from keras.src.layers.regularization.dropout import Dropout 

22 

23# isort: off 

24from tensorflow.python.util.tf_export import keras_export 

25 

26 

27@keras_export("keras.layers.SpatialDropout1D") 

28class SpatialDropout1D(Dropout): 

29 """Spatial 1D version of Dropout. 

30 

31 This version performs the same function as Dropout, however, it drops 

32 entire 1D feature maps instead of individual elements. If adjacent frames 

33 within feature maps are strongly correlated (as is normally the case in 

34 early convolution layers) then regular dropout will not regularize the 

35 activations and will otherwise just result in an effective learning rate 

36 decrease. In this case, SpatialDropout1D will help promote independence 

37 between feature maps and should be used instead. 

38 

39 Args: 

40 rate: Float between 0 and 1. Fraction of the input units to drop. 

41 Call arguments: 

42 inputs: A 3D tensor. 

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

44 training mode (adding dropout) or in inference mode (doing nothing). 

45 Input shape: 

46 3D tensor with shape: `(samples, timesteps, channels)` 

47 Output shape: Same as input. 

48 References: - [Efficient Object Localization Using Convolutional 

49 Networks](https://arxiv.org/abs/1411.4280) 

50 """ 

51 

52 def __init__(self, rate, **kwargs): 

53 super().__init__(rate, **kwargs) 

54 self.input_spec = InputSpec(ndim=3) 

55 

56 def _get_noise_shape(self, inputs): 

57 input_shape = tf.shape(inputs) 

58 noise_shape = (input_shape[0], 1, input_shape[2]) 

59 return noise_shape 

60