1

To say, i have a tensor matrix:

matrix=tf.convert_to_tensor([[1,1,1,1],[0,0,0,0]],dtype=tf.float32)

and i get the shape of matrix using tf.shape(matrix), the result is

<tf.Tensor 'Shape_2:0' shape=(2,) dtype=int32>

however using print(matrix), i get the result:

<tf.Tensor 'Const_257:0' shape=(2, 4) dtype=float32>.

Why they are not the same. I'm new with tensorflow, can anybody explain it?

thanks a lot.

1
  • 1
    Seems like you somehow messed up the formatting of your question. Anyway, I guess this answer will answer your question.
    – chrert
    Jul 10, 2017 at 13:30

1 Answer 1

2

The method tf.shape() returns a new tensor containing the shape of the input tensor. The returned tensor is completely different than the input tensor.

>>> import tensorflow as tf
>>> matrix = tf.convert_to_tensor([[1,1,1,1],[0,0,0,0]],dtype=tf.float32)
>>> matrix
<tf.Tensor 'Const_5:0' shape=(2, 4) dtype=float32>
>>> matrix.get_shape()
TensorShape([Dimension(2), Dimension(4)])
>>> shape_tensor = tf.shape(matrix)
>>> shape_tensor
<tf.Tensor 'Shape_3:0' shape=(2,) dtype=int32>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.