## 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)) # Traceback (most recent call last): # File "<stdin>", line 1, in <module> # File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/gen_array_ops.py", line 1750, in reshape # name=name) # File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 454, in apply_op # as_ref=input_arg.is_ref) # File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 621, in convert_to_tensor # ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref) # File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/constant_op.py", line 180, in _constant_tensor_conversion_function # return constant(v, dtype=dtype, name=name) # File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/constant_op.py", line 163, in constant # tensor_util.make_tensor_proto(value, dtype=dtype, shape=shape)) # File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/tensor_util.py", line 353, in make_tensor_proto # _AssertCompatible(values, dtype) # File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/tensor_util.py", line 290, in _AssertCompatible # (dtype.name, repr(mismatch), type(mismatch).__name__)) # TypeError: Expected int32, got Dimension(6) of type 'Dimension' instead.

To get the shape as a list of ints, do `tensor.get_shape().as_list()`

.

To complete your `tf.shape()`

call, try `tensor2 = tf.reshape(tensor, tf.TensorShape([num_rows*num_cols, 1]))`

. Or you can directly do `tensor2 = tf.reshape(tensor, tf.TensorShape([-1, 1]))`

where its first dimension can be inferred.

**How to get TensorFlow tensor dimensions (shape) as integer values ,** 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 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.

Another way to solve this is like this:

tensor_shape[0].value

This will return the int value of the Dimension object.

**tf.shape,** How to get TensorFlow tensor dimensions (shape) as integer values in Python. A tensor is a data structure analgous to a set of vectors. Its dimensions are - tf.int16: Integer variable - "name": Name of the tensor. To create a tensor of dimension 0, run the following code: r1 = tf.constant(1, tf.int16) print(r1) You can define a tensor with decimal values or with a string by changing the type of data. # Decimal. r1_decimal = tf.constant(0.9, tf.float32) print(r1_decimal) # String. r1_string = tf.constant("any name", tf.string) print(r1_string) The following fill operation creates a tensor of shape dims and fills it with value.

for a 2-D tensor, you can get the number of rows and columns as int32 using the following code:

rows, columns = map(lambda i: i.value, tensor.get_shape())

**How to print the value of a Tensor object in TensorFlow?,** size . This operation returns a 1-D integer tensor representing the shape of input . This represents the minimal set of known information at definition time. A dimension (represents a shape) A data type (represents a data type) Each operation we perform are manipulation of these tensor. Four main tensors are: tf.Variable; tf.constant; tf.placeholder; tf.SparseTensor; Let us start by importing tensorflow # Import tf import tensorflow as tf Create a tensor of n-dimension. Lets start with a tensor of

**2.0 Compatible Answer**: In ** Tensorflow 2.x (2.1)**, you can get the dimensions (shape) of the tensor as integer values, as shown in the Code below:

**Method 1 (using tf.shape)**:

import tensorflow as tf c = tf.constant([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]) Shape = c.shape.as_list() print(Shape) # [2,3]

**Method 2 (using tf.get_shape())**:

import tensorflow as tf c = tf.constant([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]) Shape = c.get_shape().as_list() print(Shape) # [2,3]

**TensorFlow Basics: Tensor, Shape, Type, Graph, Sessions,** () operator, as Andrzej suggests in another answer. According to the official documentation: To make sure the operator runs, users need to pass the produced op to tf. input = tf.keras.backend.placeholder (shape= (2, 4, 5)) tf.keras.backend.int_shape (input) (2, 4, 5) val = np.array ([ [1, 2], [3, 4]]) kvar = tf.keras.backend.variable (value=val) tf.keras.backend.int_shape (kvar) (2, 2) Was this page helpful?

In later versions (tested with TensorFlow 1.14) there's a more numpy-like way to get the shape of a tensor. You can use `tensor.shape`

to get the shape of the tensor.

tensor_shape = tensor.shape print(tensor_shape)

**Introduction to Tensor with Tensorflow,** can be originated from the input data or the result of a computation. The N-1 dimensions of the returned tensor are the last N-1 dimensions of the input tensor. NOTE: The returned tensor may not satisfy the same alignment requirement as this tensor depending on the shape. The caller must check the returned tensor's alignment before calling certain methods that have alignment requirement (e.g., flat(), tensor()).

**How do I find out the version of TensorFlow on my computer?,** A TensorShape represents a possibly-partial shape specification for a Tensor . It may be one of as_list(). Returns a list of integers or None for each dimension. Raises. ValueError, If self does not have a known value for every dimension. All images are size normalized to fit in a 20x20 pixel box and there are centered in a 28x28 image using the center of mass. gray = np.delete(gray,-1,1) rows,cols = gray.shape factor = 20

**tf.TensorShape,** shape . Returns a 0-D Tensor representing the number of elements in input of type out_type . Defaults to tf.int32. Why GitHub? Features →. Code review; Project management; Integrations; Actions; Packages; Security

**tf.size,** In some cases, the inferred shape may have unknown dimensions. Given a tensor of integer or floating-point values, this operation returns a tensor of the What is a Tensor? Tensorflow's name is directly derived from its core framework: Tensor. In Tensorflow, all the computations involve tensors. A tensor is a vector or matrix of n-dimensions that represents all types of data. All values in a tensor hold identical data type with a known (or partially known) shape. The shape of the data is the