1

例如,当我使用时,我有 2 个张量 A 和 B 都有维度(无,HWC)

tf.matmul(tf.transpose(A),B)

结果维度将是(HWC,HWC),这是正确的,但我想保留无维度,以便它可以是(无,HWC,HWC)。有没有办法做到这一点?

4

1 回答 1

0

也许尝试这样的事情:

import tensorflow as tf

input1 = tf.keras.layers.Input(((32, 32, 3)))
input2 = tf.keras.layers.Input(((32, 32, 3)))
a = tf.keras.layers.Conv2D(64, (1, 1))(input1)
b = tf.keras.layers.Conv2D(64, (1, 1))(input2)
z = tf.matmul(a, b, transpose_a=True)
model = tf.keras.Model([input1, input2], z)
print(model.summary())
Model: "model_1"
__________________________________________________________________________________________________
 Layer (type)                   Output Shape         Param #     Connected to                     
==================================================================================================
 input_11 (InputLayer)          [(None, 32, 32, 3)]  0           []                               
                                                                                                  
 input_12 (InputLayer)          [(None, 32, 32, 3)]  0           []                               
                                                                                                  
 conv2d_17 (Conv2D)             (None, 32, 32, 64)   256         ['input_11[0][0]']               
                                                                                                  
 conv2d_18 (Conv2D)             (None, 32, 32, 64)   256         ['input_12[0][0]']               
                                                                                                  
 tf.linalg.matmul_4 (TFOpLambda  (None, 32, 64, 64)  0           ['conv2d_17[0][0]',              
 )                                                                'conv2d_18[0][0]']              
                                                                                                  
==================================================================================================
Total params: 512
Trainable params: 512
Non-trainable params: 0
__________________________________________________________________________________________________
None
于 2021-11-28T14:37:18.440 回答