Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/keras/src/layers/merging/multiply.py: 62%

13 statements  

« 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 multiplies (element-wise) several inputs.""" 

16 

17 

18from keras.src.layers.merging.base_merge import _Merge 

19 

20# isort: off 

21from tensorflow.python.util.tf_export import keras_export 

22 

23 

24@keras_export("keras.layers.Multiply") 

25class Multiply(_Merge): 

26 """Layer that multiplies (element-wise) a list of inputs. 

27 

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). 

30 

31 >>> tf.keras.layers.Multiply()([np.arange(5).reshape(5, 1), 

32 ... np.arange(5, 10).reshape(5, 1)]) 

33 <tf.Tensor: shape=(5, 1), dtype=int64, numpy= 

34 array([[ 0], 

35 [ 6], 

36 [14], 

37 [24], 

38 [36]])> 

39 

40 >>> x1 = tf.keras.layers.Dense(8)(np.arange(10).reshape(5, 2)) 

41 >>> x2 = tf.keras.layers.Dense(8)(np.arange(10, 20).reshape(5, 2)) 

42 >>> multiplied = tf.keras.layers.Multiply()([x1, x2]) 

43 >>> multiplied.shape 

44 TensorShape([5, 8]) 

45 """ 

46 

47 def _merge_function(self, inputs): 

48 output = inputs[0] 

49 for i in range(1, len(inputs)): 

50 output = output * inputs[i] 

51 return output 

52 

53 

54@keras_export("keras.layers.multiply") 

55def multiply(inputs, **kwargs): 

56 """Functional interface to the `Multiply` layer. 

57 

58 Example: 

59 

60 >>> x1 = np.arange(3.0) 

61 >>> x2 = np.arange(3.0) 

62 >>> tf.keras.layers.multiply([x1, x2]) 

63 <tf.Tensor: shape=(3,), dtype=float32, numpy=array([0., 1., 4.], ...)> 

64 

65 Usage in a functional model: 

66 

67 >>> input1 = tf.keras.layers.Input(shape=(16,)) 

68 >>> x1 = tf.keras.layers.Dense( 

69 ... 8, activation='relu')(input1) #shape=(None, 8) 

70 >>> input2 = tf.keras.layers.Input(shape=(32,)) 

71 >>> x2 = tf.keras.layers.Dense( 

72 ... 8, activation='relu')(input2) #shape=(None, 8) 

73 >>> out = tf.keras.layers.multiply([x1,x2]) #shape=(None, 8) 

74 >>> out = tf.keras.layers.Dense(4)(out) 

75 >>> model = tf.keras.models.Model(inputs=[input1, input2], outputs=out) 

76 

77 Args: 

78 inputs: A list of input tensors. 

79 **kwargs: Standard layer keyword arguments. 

80 

81 Returns: 

82 A tensor, the element-wise product of the inputs. 

83 """ 

84 return Multiply(**kwargs)(inputs) 

85