Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/keras/src/layers/merging/subtract.py: 61%
18 statements
« prev ^ index » next coverage.py v7.4.0, created at 2024-01-03 07:57 +0000
« 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"""Layer that subtracts two inputs."""
18from keras.src.layers.merging.base_merge import _Merge
19from keras.src.utils import tf_utils
21# isort: off
22from tensorflow.python.util.tf_export import keras_export
25@keras_export("keras.layers.Subtract")
26class Subtract(_Merge):
27 """Layer that subtracts two inputs.
29 It takes as input a list of tensors of size 2, both of the same shape, and
30 returns a single tensor, (inputs[0] - inputs[1]), also of the same shape.
32 Examples:
34 ```python
35 import keras.src as keras
37 input1 = keras.layers.Input(shape=(16,))
38 x1 = keras.layers.Dense(8, activation='relu')(input1)
39 input2 = keras.layers.Input(shape=(32,))
40 x2 = keras.layers.Dense(8, activation='relu')(input2)
41 # Equivalent to subtracted = keras.layers.subtract([x1, x2])
42 subtracted = keras.layers.Subtract()([x1, x2])
44 out = keras.layers.Dense(4)(subtracted)
45 model = keras.models.Model(inputs=[input1, input2], outputs=out)
46 ```
47 """
49 @tf_utils.shape_type_conversion
50 def build(self, input_shape):
51 super().build(input_shape)
52 if len(input_shape) != 2:
53 raise ValueError(
54 "A `Subtract` layer should be called on exactly 2 inputs. "
55 f"Received: input_shape={input_shape}"
56 )
58 def _merge_function(self, inputs):
59 if len(inputs) != 2:
60 raise ValueError(
61 "A `Subtract` layer should be called on exactly 2 inputs. "
62 f"Received: inputs={inputs}"
63 )
64 return inputs[0] - inputs[1]
67@keras_export("keras.layers.subtract")
68def subtract(inputs, **kwargs):
69 """Functional interface to the `Subtract` layer.
71 Args:
72 inputs: A list of input tensors (exactly 2).
73 **kwargs: Standard layer keyword arguments.
75 Returns:
76 A tensor, the difference of the inputs.
78 Examples:
80 ```python
81 import keras.src as keras
83 input1 = keras.layers.Input(shape=(16,))
84 x1 = keras.layers.Dense(8, activation='relu')(input1)
85 input2 = keras.layers.Input(shape=(32,))
86 x2 = keras.layers.Dense(8, activation='relu')(input2)
87 subtracted = keras.layers.subtract([x1, x2])
89 out = keras.layers.Dense(4)(subtracted)
90 model = keras.models.Model(inputs=[input1, input2], outputs=out)
91 ```
92 """
93 return Subtract(**kwargs)(inputs)