Tensorflow如何将1-D张量传递到矢量

时间:2016-12-01 11:56:51

标签: tensorflow

我是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))

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

tf.shape显示tf.shape[c]=[4 1],not [4]

谁能告诉我怎么做?非常感谢。

1 个答案:

答案 0 :(得分:2)

我认为您需要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