Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/keras/src/layers/merging/maximum.py: 64%
14 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 computes the maximum (element-wise) of several inputs."""
18import tensorflow.compat.v2 as tf
20from keras.src.layers.merging.base_merge import _Merge
22# isort: off
23from tensorflow.python.util.tf_export import keras_export
26@keras_export("keras.layers.Maximum")
27class Maximum(_Merge):
28 """Layer that computes the maximum (element-wise) a list of inputs.
30 It takes as input a list of tensors, all of the same shape, and returns
31 a single tensor (also of the same shape).
33 >>> tf.keras.layers.Maximum()([np.arange(5).reshape(5, 1),
34 ... np.arange(5, 10).reshape(5, 1)])
35 <tf.Tensor: shape=(5, 1), dtype=int64, numpy=
36 array([[5],
37 [6],
38 [7],
39 [8],
40 [9]])>
42 >>> x1 = tf.keras.layers.Dense(8)(np.arange(10).reshape(5, 2))
43 >>> x2 = tf.keras.layers.Dense(8)(np.arange(10, 20).reshape(5, 2))
44 >>> maxed = tf.keras.layers.Maximum()([x1, x2])
45 >>> maxed.shape
46 TensorShape([5, 8])
47 """
49 def _merge_function(self, inputs):
50 output = inputs[0]
51 for i in range(1, len(inputs)):
52 output = tf.maximum(output, inputs[i])
53 return output
56@keras_export("keras.layers.maximum")
57def maximum(inputs, **kwargs):
58 """Functional interface to compute maximum (element-wise) list of `inputs`.
60 This is equivalent to the `tf.keras.layers.Maximum` layer.
62 For example:
64 ```python
65 input1 = tf.keras.layers.Input(shape=(16,))
66 x1 = tf.keras.layers.Dense(8, activation='relu')(input1) #shape=(None, 8)
67 input2 = tf.keras.layers.Input(shape=(32,))
68 x2 = tf.keras.layers.Dense(8, activation='relu')(input2) #shape=(None, 8)
69 max_inp=tf.keras.layers.maximum([x1,x2]) #shape=(None, 8)
70 out = tf.keras.layers.Dense(4)(max_inp)
71 model = tf.keras.models.Model(inputs=[input1, input2], outputs=out)
72 ```
74 Args:
75 inputs: A list of input tensors of same shape.
76 **kwargs: Standard layer keyword arguments.
78 Returns:
79 A tensor (of same shape as input tensor) with the element-wise
80 maximum of the inputs.
82 Raises:
83 ValueError: If input tensors are of different shape.
84 """
85 return Maximum(**kwargs)(inputs)