# How to get Tensorflow tensor dimensions (shape) as int values?

Suppose I have a Tensorflow tensor. How do I get the dimensions (shape) of the tensor as integer values? I know there are two methods, tensor.get_shape() and tf.shape(tensor), but I can't get the shape values as integer int32 values. For example, below I've created a 2-D tensor, and I need to get the number of rows and columns as int32 so that I can call reshape() to create a tensor of shape (num_rows * num_cols, 1). However, the method tensor.get_shape() returns values as Dimension type, not int32. import tensorflow as tf import numpy as np sess = tf.Session() tensor = tf.convert_to_tensor(np.array([[1001,1002,1003],[3,4,5]]), dtype=tf.float32) sess.run(tensor) # array([[ 1001., 1002., 1003.], # [ 3., 4., 5.]], dtype=float32) tensor_shape = tensor.get_shape() tensor_shape # TensorShape([Dimension(2), Dimension(3)]) print tensor_shape # (2, 3) num_rows = tensor_shape[0] # ??? num_cols = tensor_shape[1] # ??? tensor2 = tf.reshape(tensor, (num_rows*num_cols, 1)) TypeError: Expected int32, got Dimension(6) of type 'Dimension' instead. Select the correct answer from above options

Code to get the shape as a list of ints tensor.get_shape().as_list() To complete your tf.shape() function is incomplete, Add the following code to complete: tensor2 = tf.reshape(tensor, tf.TensorShape([num_rows*num_cols, 1])) Or tensor2 = tf.reshape(tensor, tf.TensorShape([-1, 1])) Hope this answer helps. If you want to learn more about TensorFlow then enroll for the Machine Learning Course.

## Related questions

I am new to TensorFlow. While I am reading the existing documentation, I found the term tensor really confusing. ... types of tensors? Select the correct answer from above options...
I am trying to decompose a 3D matrix using python library scikit-tensor. I managed to decompose my Tensor ... the decomposed matrices? Select the correct answer from above options...
I'm creating a very basic AI with Tensorflow, and am using the code from the official docs/tutorial. Here's my ... Tensorflow 1.13.1. Select the correct answer from above options...
I am training on 970 samples and validating on 243 samples. How big should batch size and number of epochs be ... on data input size? Select the correct answer from above options...
I have worked all the tutorials and searched for "load csv tensorflow" but just can't get the logic of it all. ... and test the net. Select the correct answer from above options...
What is the difference between the two? It seems that both create new columns, in which their number is equal to ... they are in. Select the correct answer from above options...
I am receiving the error: ValueError: Wrong number of items passed 3, placement implies 1, and I am struggling to ... 'sigma'] = sigma Select the correct answer from above options...
In this video from Sebastian Thrun, he says that supervised learning works with "labeled" data and unsupervised ... basic difference. Select the correct answer from above options...