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

17 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 ActivityRegularization layer.""" 

16 

17 

18from keras.src import regularizers 

19from keras.src.engine.base_layer import Layer 

20 

21# isort: off 

22from tensorflow.python.util.tf_export import keras_export 

23 

24 

25@keras_export("keras.layers.ActivityRegularization") 

26class ActivityRegularization(Layer): 

27 """Layer that applies an update to the cost function based input activity. 

28 

29 Args: 

30 l1: L1 regularization factor (positive float). 

31 l2: L2 regularization factor (positive float). 

32 

33 Input shape: 

34 Arbitrary. Use the keyword argument `input_shape` 

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

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

37 

38 Output shape: 

39 Same shape as input. 

40 """ 

41 

42 def __init__(self, l1=0.0, l2=0.0, **kwargs): 

43 super().__init__( 

44 activity_regularizer=regularizers.L1L2(l1=l1, l2=l2), **kwargs 

45 ) 

46 self.supports_masking = True 

47 self.l1 = l1 

48 self.l2 = l2 

49 

50 def compute_output_shape(self, input_shape): 

51 return input_shape 

52 

53 def get_config(self): 

54 config = {"l1": self.l1, "l2": self.l2} 

55 base_config = super().get_config() 

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

57