2016-12-01 78 views
1

我是初学者总到TensorFlow,现在我有一个1-d张量,其形状为[4,1],我有2矩阵 a=tf.placeholder(tf.float32,[4,2]); b=tf.placeholder(tf.float32,[2]) 当我多个他们:c=tf.mul(a,tf.expand_dims(b,1))Tensorflow如何转移的1张量d为矢量

然后我得到了[4,1]矩阵c。它是一个二维张量,但我想将其改变为一维张量,意味着它是一个矢量,它的形状是[4],而不是[4,1]。

tf.shape表明tf.shape[c]=[4 1],not [4]

谁能告诉我如何做到这一点?非常感谢。

回答

1

我想你想要tf.squeezetf.reshape

a = tf.constant(1.0, shape=[4, 2]) 
b = tf.constant(1.0, shape=[2]) 
c = tf.matmul(a, tf.expand_dims(b,1)) 
c = tf.squeeze(c) 

# This will also work: 
# c = tf.reshape(c, [4]) 

你也想tf.matmul而不是tf.mul在你的例子,如果你想要做矩阵乘法,而不是按元素相乘。

+0

非常感谢,它的工作原理。顺便说一句,我输入错误的单词。它是“tf.matmul(a,tf.expand_dims(b,1))”,而不是tf.mul(a,tf.expand_dims(b,1))。你是对的。 – zeekzhen