Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/keras/src/layers/core/activation.py: 56%

18 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 Activation layer.""" 

16 

17 

18from keras.src import activations 

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.Activation") 

26class Activation(Layer): 

27 """Applies an activation function to an output. 

28 

29 Args: 

30 activation: Activation function, such as `tf.nn.relu`, or string name of 

31 built-in activation function, such as "relu". 

32 

33 Usage: 

34 

35 >>> layer = tf.keras.layers.Activation('relu') 

36 >>> output = layer([-3.0, -1.0, 0.0, 2.0]) 

37 >>> list(output.numpy()) 

38 [0.0, 0.0, 0.0, 2.0] 

39 >>> layer = tf.keras.layers.Activation(tf.nn.relu) 

40 >>> output = layer([-3.0, -1.0, 0.0, 2.0]) 

41 >>> list(output.numpy()) 

42 [0.0, 0.0, 0.0, 2.0] 

43 

44 Input shape: 

45 Arbitrary. Use the keyword argument `input_shape` 

46 (tuple of integers, does not include the batch axis) 

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

48 

49 Output shape: 

50 Same shape as input. 

51 """ 

52 

53 def __init__(self, activation, **kwargs): 

54 super().__init__(**kwargs) 

55 self.supports_masking = True 

56 self.activation = activations.get(activation) 

57 

58 def call(self, inputs): 

59 return self.activation(inputs) 

60 

61 def compute_output_shape(self, input_shape): 

62 return input_shape 

63 

64 def get_config(self): 

65 config = {"activation": activations.serialize(self.activation)} 

66 base_config = super().get_config() 

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

68