2016-12-01 77 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