如何处理具有维度无的张量乘法
Posted
技术标签:
【中文标题】如何处理具有维度无的张量乘法【英文标题】: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)。有没有办法做到这一点?
【问题讨论】:
回答有用吗? 【参考方案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
【讨论】:
以上是关于如何处理具有维度无的张量乘法的主要内容,如果未能解决你的问题,请参考以下文章