如何处理维度为None的张量乘法



例如,当我使用时,我有两个张量A和B,它们都具有维度(None,HWC(

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

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

也许可以试试这样的东西:

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

相关内容

  • 没有找到相关文章

最新更新