【问题标题】:How to handle tensor multiplication with dimension None如何处理具有维度无的张量乘法
【发布时间】:2022-01-05 09:23:08
【问题描述】:

例如,当我使用时,我有 2 个张量 A 和 B,尺寸均为 (None,HWC)

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

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

【问题讨论】:

  • 回答有用吗?

标签: python tensorflow computer-vision conv-neural-network self-attention


【解决方案1】:

不妨试试这样的:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-12
    • 1970-01-01
    • 2021-04-05
    • 2020-08-26
    相关资源
    最近更新 更多