## Introduction to TensorFlow Reshape

TensorFlow Reshape functionality allows Data Scientists to play around with dimensions of the Tensors, in the way their application warrants and control the data flow to achieve the results. This manipulation of Tensor elements does not alter the original form and the consistency is maintained.

This reshapes facility enables Data Scientists to train neural networks in AI in different ways so that complex search operations, audio / Video file matching, image processing and natural language classification can be resolved accurately as well as within a short time. It also allows the designing of a variety of new data models to build robust and reliable AI solutions.

### Why TensorFlow Reshape?

- It allows the building of complex and multidimensional data models which in turn enables visualization of data at a deep level. This also enables easy and faster debugging of all the nodes in the neural network and resolve issues without going through code.
- It provides flexibility to try out different dimensions of the data and design as many views of the data and design solutions for any AI-related issues.
- It is an open-sourced platform that ideas from the community constantly improves the quality of solutions provided by this.
- This reshapes option enables users to build many use cases which were not thought of so far.

### Examples of TensorFlow Reshape

Tensor is an array of data elements arranged like a Matrix format. It can be a single dimension matrix or multi-dimension matrix (2D, 3D, 4D, 5D so on…). The matrix can hold integers or floating-point numbers or texts in strings.

Sample TensorFlow with multiple dimensions.

#### Example #1

Code to create a single dimension tensor.

**Code:**

`#Python program to define and list a single dimension Tensor`

import tensorflow as tfl

sm=tfl.constant([34,45,83,627,3,6,5,10,36,47]) #define a single dimension array

# printing attributes of Tensor

print sm

with tfl.Session() as tses:

sm_value = tses.run(sm)

print ("Elements in the single dimension tensor")

print sm_value

print ("Tensor Dimensions:", sm.get_shape())

**Output:**

The tensor has only one dimension with 10 elements in the first dimension (row of the matrix) and one element(default) in the second dimension (column of the matrix). It contains an integer datatype.

#### Example #2

Code to create a two-dimension tensor.

**Code:**

`#Python program to define and list a two dimension Tensor`

import tensorflow as tfl

dm=tfl.constant([[7,8],[10,9],[23,6],[42,19],[74,91]]) # two dimension array

# printing attributes of Tensor

print dm

with tfl.Session() as tses:

dm_value = tses.run(dm)

print ("Elements in the two dimension tensor")

print dm_value

print ("Tensor Dimensions", dm.get_shape())

**Output:**

The tensor has two dimensions with 5 elements in the first dimension (row of the matrix) and 2 elements in the second dimension (column of the matrix).

#### Example #3

Code to create three dimension tensor.

**Code:**

`#Python program to define and list a three dimension Tensor`

import tensorflow as tfl

dm=tfl.constant([[[7,8],[10,9],[23,6]],[[42,19],[74,91],[87,67]],[[45,23],[23,45],[45,56]]]) # three dimension array

# printing attributes of Tensor

print dm

with tfl.Session() as tses:

dm_value = tses.run(dm)

print ("Elements in the Three dimension tensor")

print dm_value

print ("Tensor Dimensions", dm.get_shape())

**Output:**

There are three dimensions with 2 elements in the first dimension and 3 elements in the other 2 dimensions.

### Reshape Function in TensorFlow

Each language has got its own function to reshape the tensor from any form to any other form. In python the syntax for reshape is:

`tensorflow.reshape (tensorname, shape, name=optional)`

tensorname - Name of the tensor data set that needs to converted into a new shape

shape

No. of Dimension |
Syntax |
No of Elements in Dimension |
|||

1 | 2 | 3 | 4 | ||

1 | [w] | w | |||

2 | [w , x] | w | x | ||

3 | [w, x, y] | w | x | y | |

4 | [w, x, y, z] | w | x | y | z |

**Name:** Operation name and it is optional.

Reshape function will not disturb the original tensor and it will create another tensor and it has to be stored in a different name.

**Reshape a single dimension array into 2 dimension array:**

A single dimension array with 24 elements and let us see how it is reshaped into three-dimensional array with 4 x 6 size.

**Code:**

`#Python program to convert single dimension array into two dimension array`

import tensorflow as tfl

sm=tfl.constant([17,98,24,23,98,19,23,46,34,32,45,83,62,31,62,53,10,36,47,78,89,87,23,12]) #define a single dimension array

# printing attributes of Tensor

print sm

# reshape single dimension into two dimension

dm=tfl.reshape(sm, [4,6]) #store the output in new tensor

with tfl.Session() as tses:

sm_value = tses.run(sm)

print ("Elements in the single dimension tensor")

print sm_value

print ("Tensor Dimensions:", sm.get_shape())

print (" ")

print ("After Reshape")

print dm

dm_value = tses.run(dm)

print ("Elements in the Two dimension tensor")

print dm_value

print ("Tensor Dimensions:", dm.get_shape())

**Output:**

From input tensor of single dimension with 24 data elements reshape() function has converted it into 2 dimension with 4 (rows) x 6 (column).

**Reshape a single dimension array into 3 dimension array:**

A single dimension array with 24 elements and let use see how it is reshaped into three-dimensional array with 2 x 3 x 4 size.

**Code:**

`#Python program to convert single dimension array into three dimension array`

import tensorflow as tfl

sm=tfl.constant([17,98,24,23,98,19,23,46,34,32,45,83,62,31,62,53,10,36,47,78,89,87,23,12]) #define a single dimension array

# printing attributes of Tensor

print sm

# reshape single dimension into three dimension

tm=tfl.reshape(sm, [4,3,2]) #store the output in new tensor

with tfl.Session() as tses:

sm_value = tses.run(sm)

print ("Elements in the single dimension tensor")

print sm_value

print ("Tensor Dimensions:", sm.get_shape())

print (" ")

print ("After Reshape")

print tm

tm_value = tses.run(tm)

print ("Elements in the Three dimension tensor")

print tm_value

print ("Tensor Dimensions:", tm.get_shape())

**Output:**

**Reshape a three-dimension tensor into two-dimension tensor:**

**Code:**

`#Convert three dimension tensor into two dimension`

import tensorflow as tfl

tm=tfl.constant([[[7,8,3],[10,23,9],[23,29,6]],[[42,98,19],[74,91,23],[87,67,87]],[[45,23,78],[23,45,89],[45,56,98]]]) # three dimension array

dm=tfl.reshape(tm, [3,9])

# printing attributes of Tensor

print tm

with tfl.Session() as tses:

tm_value = tses.run(tm)

print ("Elements in the Three dimension tensor")

print tm_value

print ("Tensor Dimensions", tm.get_shape())

print (" ")

print ("After Reshape")

print dm

dm_value = tses.run(dm)

print ("Elements in the Two dimension tensor")

print dm_value

print ("Tensor Dimensions", dm.get_shape())

**Output:**

A 3 x 3 x 3 three-dimension tensor is reshaped into a 3 x 9 two-dimension tensor.

### Benefits

TensorFlow reshape has the following benefits:

- Increases scalability of AI operations and penetrate into new areas in deep learning.
- Supported in all languages.
- Allows parallelism in the execution of data models.
- Any new use cases can be easily supported using reshape.

### Recommended Articles

This is a guide to TensorFlow Reshape. Here we discuss the introduction, reshape function in TensorFlow, examples and benefits respectively. You may also have a look at the following articles to learn more –

11 Online Courses | 3 Hands-on Projects | 55+ Hours | Verifiable Certificate of Completion

4.5

View Course

Related Courses