Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/keras/src/layers/merging/add.py: 62%
13 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 adds several inputs."""
18from keras.src.layers.merging.base_merge import _Merge
20# isort: off
21from tensorflow.python.util.tf_export import keras_export
24@keras_export("keras.layers.Add")
25class Add(_Merge):
26 """Layer that adds a list of inputs.
28 It takes as input a list of tensors,
29 all of the same shape, and returns
30 a single tensor (also of the same shape).
32 Examples:
34 >>> input_shape = (2, 3, 4)
35 >>> x1 = tf.random.normal(input_shape)
36 >>> x2 = tf.random.normal(input_shape)
37 >>> y = tf.keras.layers.Add()([x1, x2])
38 >>> print(y.shape)
39 (2, 3, 4)
41 Used in a functional model:
43 >>> input1 = tf.keras.layers.Input(shape=(16,))
44 >>> x1 = tf.keras.layers.Dense(8, activation='relu')(input1)
45 >>> input2 = tf.keras.layers.Input(shape=(32,))
46 >>> x2 = tf.keras.layers.Dense(8, activation='relu')(input2)
47 >>> # equivalent to `added = tf.keras.layers.add([x1, x2])`
48 >>> added = tf.keras.layers.Add()([x1, x2])
49 >>> out = tf.keras.layers.Dense(4)(added)
50 >>> model = tf.keras.models.Model(inputs=[input1, input2], outputs=out)
52 """
54 def _merge_function(self, inputs):
55 output = inputs[0]
56 for i in range(1, len(inputs)):
57 output += inputs[i]
58 return output
61@keras_export("keras.layers.add")
62def add(inputs, **kwargs):
63 """Functional interface to the `tf.keras.layers.Add` layer.
65 Args:
66 inputs: A list of input tensors with the same shape.
67 **kwargs: Standard layer keyword arguments.
69 Returns:
70 A tensor as the sum of the inputs. It has the same shape as the inputs.
72 Examples:
74 >>> input_shape = (2, 3, 4)
75 >>> x1 = tf.random.normal(input_shape)
76 >>> x2 = tf.random.normal(input_shape)
77 >>> y = tf.keras.layers.add([x1, x2])
78 >>> print(y.shape)
79 (2, 3, 4)
81 Used in a functional model:
83 >>> input1 = tf.keras.layers.Input(shape=(16,))
84 >>> x1 = tf.keras.layers.Dense(8, activation='relu')(input1)
85 >>> input2 = tf.keras.layers.Input(shape=(32,))
86 >>> x2 = tf.keras.layers.Dense(8, activation='relu')(input2)
87 >>> added = tf.keras.layers.add([x1, x2])
88 >>> out = tf.keras.layers.Dense(4)(added)
89 >>> model = tf.keras.models.Model(inputs=[input1, input2], outputs=out)
91 """
92 return Add(**kwargs)(inputs)