zeekzhen
zeekzhen

Reputation: 159

Tensorflow how to transfer a 1-D tensor to vector

I'm a total beginner to TensorFlow, now I have a 1-D tensor, its shape is [4, 1], I have 2 matrix a=tf.placeholder(tf.float32,[4,2]); b=tf.placeholder(tf.float32,[2]) when I multiple them: c=tf.mul(a,tf.expand_dims(b,1))

then I got a [4,1] matrix c. it is a 2-D tensor, but I want to change it to a 1-D tensor , means it's a vector and its shape is [4], not [4,1].

tf.shape shows that tf.shape[c]=[4 1],not [4]

Can anyone tell me how to do this? thanks very much.

Upvotes: 2

Views: 1456

Answers (1)

kempy
kempy

Reputation: 616

I think you want tf.squeeze or tf.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])

You also want tf.matmul instead of tf.mul in your example if you want to do matrix multiplication instead of elementwise multiplication.

Upvotes: 3

Related Questions