-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathexponent.py
More file actions
100 lines (87 loc) · 3.34 KB
/
Copy pathexponent.py
File metadata and controls
100 lines (87 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# Copyright [2024] Expedia, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import Any, Dict, List, Optional
import keras
from keras import KerasTensor, ops
import kamae
from kamae.keras.core.backend import ALL_BACKENDS
from kamae.keras.core.base import BaseLayer
from kamae.keras.core.utils.input_utils import allow_single_or_multiple_tensor_input
@keras.saving.register_keras_serializable(package=kamae.__name__)
class ExponentLayer(BaseLayer):
"""
Performs the x^exponent operation on a given input tensor.
"""
supported_backends = ALL_BACKENDS
jit_compatible = True
def __init__(
self,
name: Optional[str] = None,
input_dtype: Optional[str] = None,
output_dtype: Optional[str] = None,
exponent: Optional[float] = None,
**kwargs: Any,
) -> None:
"""
Initializes the exponent layer
:param name: Name of the layer, defaults to `None`.
:param input_dtype: The dtype to cast the input to. Defaults to `None`.
:param output_dtype: The dtype to cast the output to. Defaults to `None`.
:param exponent: The exponent to raise the input to, defaults to `None`.
"""
super().__init__(
name=name, input_dtype=input_dtype, output_dtype=output_dtype, **kwargs
)
self.exponent = exponent
@property
def compatible_dtypes(self) -> Optional[List[str]]:
"""
Returns the compatible dtypes of the layer.
:returns: The compatible dtypes of the layer.
"""
return [
"float16",
"float32",
"float64",
"complex64",
"complex128",
]
@allow_single_or_multiple_tensor_input
def _call(self, inputs: KerasTensor, **kwargs: Any) -> KerasTensor:
"""
:param inputs: Single tensor or iterable of tensors to perform the x^pow
operation on.
:returns: The tensor raised to the power of the exponent.
"""
if self.exponent is not None:
if len(inputs) > 1:
raise ValueError("If exponent is set, cannot have multiple inputs")
return ops.power(
inputs[0],
ops.cast(self.exponent, dtype=inputs[0].dtype),
)
else:
if not len(inputs) == 2:
raise ValueError("If exponent is not set, must have exactly 2 inputs")
return ops.power(inputs[0], inputs[1])
def get_config(self) -> Dict[str, Any]:
"""
Gets the configuration of the exp layer.
Used for saving and loading from a model.
Specifically adds the `exponent` to the config dictionary
:returns: Dictionary of the configuration of the layer.
"""
config = super().get_config()
config.update({"exponent": self.exponent})
return config