Python 张量流将三维张量乘以二维矩阵
我试着用2D矩阵乘以3D张量,但是有一个未知的维度。 我检查了所有关于这方面的帖子,但是如果没有我就找不到我想要的 我有以下参数: T形(M,N) L形(?,M,M) F形(?,N) 我想用输出形状(?,M)乘以L*T*F 我试着扩大尺寸等 不幸的是,我总是迷失方向Python 张量流将三维张量乘以二维矩阵,python,tensorflow,Python,Tensorflow,我试着用2D矩阵乘以3D张量,但是有一个未知的维度。 我检查了所有关于这方面的帖子,但是如果没有我就找不到我想要的 我有以下参数: T形(M,N) L形(?,M,M) F形(?,N) 我想用输出形状(?,M)乘以L*T*F 我试着扩大尺寸等 不幸的是,我总是迷失方向 谢谢你的建议。你可以这样做 L --> [?, M, M] T --> [M, N] tensordot(L,T) axes [[2], [0]] --> [?,M, N] F --> [?, N] --&g
谢谢你的建议。你可以这样做
L --> [?, M, M]
T --> [M, N]
tensordot(L,T) axes [[2], [0]] --> [?,M, N]
F --> [?, N] --> expand axis --> [?, N, 1]
matmul [?, M, N], [?, N, 1] --> [?, M, 1] --> squeeze --> [?, M]
综合起来:
tf.squeeze(tf.matmul(tf.tensordot(L,T, axes=[[2],[0]]),F[...,None]))
作为一名学习者,我发现这个问题和答案相当神秘。所以我自己简化了
import tensorflow as tf
L = tf.placeholder(tf.float32, shape=[None, 5, 5])
T = tf.placeholder(tf.float32, shape=[ 5, 10])
F = tf.placeholder(tf.float32, shape=[ None, 10])
print ((tf.tensordot(L,T, axes=[[2],[0]])).get_shape)
# This is more cryptic. Not really sure.
#print(F[...,None].get_shape)
print( tf.expand_dims(F,2).get_shape)
finaltensor = tf.matmul(tf.tensordot(L,T, axes=[[2],[0]]),F[...,None])
print (finaltensor.get_shape)
squeezedtensor = tf.squeeze(finaltensor)
print (tf.shape(squeezedtensor))
除了最后一行外,所有打印的内容都是清晰的
<bound method Tensor.get_shape of <tf.Tensor 'Tensordot:0' shape=(?, 5, 10) dtype=float32>>
<bound method Tensor.get_shape of <tf.Tensor 'ExpandDims:0' shape=(?, 10, 1) dtype=float32>>
<bound method Tensor.get_shape of <tf.Tensor 'MatMul:0' shape=(?, 5, 1) dtype=float32>>
Tensor("Shape:0", shape=(?,), dtype=int32)
张量(“形状:0”,形状=(?,),数据类型=int32)