Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/tensorflow_addons/layers/gelu.py: 58%

19 statements  

« prev     ^ index     » next       coverage.py v7.4.0, created at 2024-01-03 07:57 +0000

1# Copyright 2019 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"""Implements GELU activation.""" 

16 

17import tensorflow as tf 

18from tensorflow_addons.activations import gelu 

19from typeguard import typechecked 

20 

21 

22@tf.keras.utils.register_keras_serializable(package="Addons") 

23class GELU(tf.keras.layers.Layer): 

24 """Gaussian Error Linear Unit. 

25 

26 A smoother version of ReLU generally used 

27 in the BERT or BERT architecture based models. 

28 Original paper: https://arxiv.org/abs/1606.08415 

29 

30 Input shape: 

31 Arbitrary. Use the keyword argument `input_shape` 

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

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

34 

35 Output shape: 

36 Same shape as the input. 

37 """ 

38 

39 @typechecked 

40 def __init__(self, approximate: bool = True, **kwargs): 

41 super().__init__(**kwargs) 

42 self.approximate = approximate 

43 self.supports_masking = True 

44 

45 def call(self, inputs): 

46 return gelu(inputs, approximate=self.approximate) 

47 

48 def get_config(self): 

49 config = {"approximate": self.approximate} 

50 base_config = super().get_config() 

51 return {**base_config, **config} 

52 

53 def compute_output_shape(self, input_shape): 

54 return input_shape