In this Python tutorial, we will learn** how to find the shape of a TensorFlow in Python**. Also, we will cover the following topics.

- TensorFlow get shape of tensor
- TensorFlow get shape of dataset
- TensorFlow get shape as list
- TensorFlow get shape none
- TensorFlow get shape of placeholder
- Tensorflow x.get_shape().as_list()

## TensorFlow get shape

- In this section, we will learn how to get the shape in TensorFlow Python.
- To get the shape of a tensor, you can easily use the
**tf.shape()**function. This method will help the user to return the shape of the given tensor. For example, suppose you have a tensor filled with integer numbers and you want to check the shape of the given input tensor. - To do this task we can easily use the
**tf.shape()**function and it returns the shape property as a scaler input value.

**Syntax:**

Let’s have a look at the Syntax and understand the working of the** tf.shape()** function.

```
tf.shape
(
input,
out_type=tf.dtypes.int32,
name=None
)
```

- It consists of a few parameters
**input:**This parameter indicates the input tensor which we want to operate**out_type**= By default it takes**tf.dtypes.int32**value. This is an optional parameter and defines the output type.**name:**This parameter indicates the name of the operation.

**Example:**

Let’s take an example and check how to get the shape in TensorFlow Python.

**Source Code:**

```
#Tensor
import tensorflow as tf
tensor = tf.constant([[[15, 67, 89], [34, 27, 89]],
[[45, 89, 189], [68, 91, 46]]])
result=tf.shape(tensor)
result
```

In the above code, first, we imported the TensorFlow library and then use the **tf.constant()** function for creating a tensor. After that, we have declared a variable ‘result’ and assigned the** tf.shape()** function for getting the shape of a given tensor.

Here is the Screenshot of the following given code.

Also, check: Python TensorFlow reduce_sum

## TensorFlow get shape of tensor

- Let us see how to get the shape of the tensor in TensorFlow Python.
- To perform this particular task we are going to use the
**tf.shape()**function for getting the shape of the tensor. - Next, we will use the
**tf.compat.v1.disable_eager_execution()**for running the session and it will display the array size.

**Example:**

Let’s take an example and check how to get the shape of Tensor in TensorFlow Python.

**Source Code:**

```
#Tensor
import tensorflow as tf
tf.compat.v1.disable_eager_execution()
tensor = tf.constant([[18, 29], [34, 78]])
result = tf.shape(tensor)
print(result) #shape of tensor
with tf.compat.v1.Session() as val:
new_output=val.run(result)
new_output #Display rows and columns dimension
```

In the following given code, we have created a tensor by using the** tf.constant()** function, and forgetting the shape of the tensor we have applied the **tf.shape()** function.

And within this method, we passed tensor as an argument. Once you will execute this code, the output displays the shape of a tensor. After that, we have applied the function **tf.compat.v1.disable_eager_execution()**.

Here is the implementation of the following given code.

Read: Python TensorFlow reduce_mean

## TensorFlow get shape of dataset

- In this section, we will learn how to get the shape of dataset in TensorFlow Python.
- To do this task we are going to use the concept of
**tfds**datasets and it specifies the collection of datasets in TensorFlow. - In Python,
**tfds**is basically used with machine learning and the TensorFlow framework. - To install this package in your machine, use the
**pip install tfds-nightly**command.

**Example:**

```
import numpy as np
import tensorflow as tf
import tensorflow_datasets as tfds
tensor=tfds.list_builders()
print(tensor)
result=tf.shape(tensor)
result
```

In the above code first, we imported the tfds library by using tensorflow_datasets as tfds. After that, we have used the datasets named ‘list_builders and it will display the list of available builders. Now, we want to get the shape of given datasets to do this task we have used the **tf.shape()** function.

Here is the Screenshot of the following given code.

Read: Python TensorFlow one_hot

## TensorFlow get shape as list

- Here we will see how to get the TensorFlow tensor shape as a list in Python.
- To perform this particular task, we are going to use the
**shape.as_list()**function. - This method returns a list of integer values that indicates the shape and the dimension of an array.

**Syntax:**

Here is the Syntax of the **as_list()** function in TensorFlow Python.

`as_list()`

**Example:**

```
import tensorflow as tf
tensor = tf.constant([[[15, 67, 89], [34, 27, 89]],
[[45, 89, 189], [68, 91, 46]]])
new_output = tensor.shape.as_list()
new_output
```

In the following given code, we have imported the TensorFlow library and then create a tensor by using the** tf.constant()** function. After that, we have used **the shape.as_list() **function for displaying the shape of tensor in a list.

Here is the implementation of the following given code.

Read: TensorFlow next_batch + Examples

## TensorFlow get shape none

- In this section, we will learn how to get the none shape in TensorFlow Python.
- To do this task, we are going to use the
**tf.keras.layers.Input()**function and this method is a tensor object from the underlying backend.

**Syntax:**

Here is the Syntax of **tf.Keras.layers.input()** method.

```
tf.keras.layer.Input
(
shape=None,
batch_size=None,
name=None,
dtype=None,
sparse=False,
tensor=None,
**kwargs
)
```

**Example:**

```
import tensorflow as tf
var = tf.keras.layers.Input((None,None))
var.shape
```

Also, you can refer to the below screenshot.

Read: Tensorflow get static value

## TensorFlow get shape of placeholder

- In this Program, we will learn how to get the shape of placeholder in TensorFlow Python.
- To do this task, we are going to use the
**tf.compat.v1.placeholder()**method and this method allows us to declare our operation and it always fed the data.

**Syntax:**

Let’s have a look at the Syntax and understand the working of **tf.comapt.v1.placeholder()**.

```
tf.compat.v1.placeholder
(
dtype,
shape=None,
name=None
)
```

**Example:**

```
import tensorflow as tf
tf.compat.v1.disable_eager_execution()
tens=tf.compat.v1.placeholder(dtype=tf.int32,shape=(300,300))
tens
```

In the above code, we imported the numpy library and then use the **tf.compat.v1.disable_eager_execution()** function. After that, we have used **tf.compat.v1.placeholder **and within this function, we have passed the shape=(**300,300))** as an argument.

Here is the execution of the following given code.

Read: TensorFlow Sparse Tensor

## Tensorflow x.get_shape().as_list()

- Let us see how to get the shape of TensorFlow by using th
**e x.get_shape().as_list()**function in Python. - To perform this particular task, we are going to use t
**he tensor.get_shape.as_list()**function. This method returns a list of integer values that indicates the shape and the dimension of an array.

**Example:**

```
import tensorflow as tf
new_tens = tf.constant([[[78, 92, 167], [98, 178, 94]],
[[178, 236, 345], [109, 256, 345]]])
new_result = new_tens.get_shape().as_list()
new_result
```

Here is the Screenshot of the following given code.

Also, take a look at some more tutorials related to Python.

- What is Scikit Learn in Python
- What is Matplotlib in Python
- What is Pandas in Python
- TensorFlow Tensor to numpy
- Machine Learning using Python

In this tutorial, we have learned** how to find the shape of a TensorFlow in Python**. Also, we have covered the following topics.

- TensorFlow get shape of tensor
- TensorFlow get shape of dataset
- TensorFlow get shape as list
- TensorFlow get shape none
- TensorFlow get shape of placeholder
- Tensorflow x.get_shape().as_list()

Python is one of the most popular languages in the United States of America. I have been working with Python for a long time and I have expertise in working with various libraries on Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… I have experience in working with various clients in countries like United States, Canada, United Kingdom, Australia, New Zealand, etc. Check out my profile.