Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/keras/src/layers/merging/average.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 averages 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.Average")
25class Average(_Merge):
26 """Layer that averages a list of inputs element-wise.
28 It takes as input a list of tensors, all of the same shape, and returns
29 a single tensor (also of the same shape).
31 Example:
33 >>> x1 = np.ones((2, 2))
34 >>> x2 = np.zeros((2, 2))
35 >>> y = tf.keras.layers.Average()([x1, x2])
36 >>> y.numpy().tolist()
37 [[0.5, 0.5], [0.5, 0.5]]
39 Usage in a functional model:
41 >>> input1 = tf.keras.layers.Input(shape=(16,))
42 >>> x1 = tf.keras.layers.Dense(8, activation='relu')(input1)
43 >>> input2 = tf.keras.layers.Input(shape=(32,))
44 >>> x2 = tf.keras.layers.Dense(8, activation='relu')(input2)
45 >>> avg = tf.keras.layers.Average()([x1, x2])
46 >>> out = tf.keras.layers.Dense(4)(avg)
47 >>> model = tf.keras.models.Model(inputs=[input1, input2], outputs=out)
49 Raises:
50 ValueError: If there is a shape mismatch between the inputs and the shapes
51 cannot be broadcasted to match.
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 / len(inputs)
61@keras_export("keras.layers.average")
62def average(inputs, **kwargs):
63 """Functional interface to the `tf.keras.layers.Average` layer.
65 Example:
67 >>> x1 = np.ones((2, 2))
68 >>> x2 = np.zeros((2, 2))
69 >>> y = tf.keras.layers.Average()([x1, x2])
70 >>> y.numpy().tolist()
71 [[0.5, 0.5], [0.5, 0.5]]
73 Usage in a functional model:
75 >>> input1 = tf.keras.layers.Input(shape=(16,))
76 >>> x1 = tf.keras.layers.Dense(8, activation='relu')(input1)
77 >>> input2 = tf.keras.layers.Input(shape=(32,))
78 >>> x2 = tf.keras.layers.Dense(8, activation='relu')(input2)
79 >>> avg = tf.keras.layers.Average()([x1, x2])
80 >>> out = tf.keras.layers.Dense(4)(avg)
81 >>> model = tf.keras.models.Model(inputs=[input1, input2], outputs=out)
83 Args:
84 inputs: A list of input tensors.
85 **kwargs: Standard layer keyword arguments.
87 Returns:
88 A tensor, the average of the inputs.
90 Raises:
91 ValueError: If there is a shape mismatch between the inputs and the shapes
92 cannot be broadcasted to match.
93 """
94 return Average(**kwargs)(inputs)