EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials TensorFlow Tutorial TensorFlow Reshape
 

TensorFlow Reshape

Updated April 20, 2023

TensorFlow Reshape

 

 

Introduction to TensorFlow Reshape

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

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

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 the visualization of data at a deep level. This also enables easy and faster debugging of all the nodes in the neural network and resolves 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 where ideas from the community constantly improve 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

A 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:

TensorFlow Reshape

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:

TensorFlow Reshape. 2. JPG

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:

TensorFlow Reshape 3

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 a 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:

TensorFlow Reshape 4

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:

array into 3 dimension array

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())# your code goes here

Output:

into two dimension tensor

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

We hope that this EDUCBA information on “TensorFlow Reshape” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

  1. Caffe TensorFlow
  2. TensorFlow Debugging
  3. What is TensorFlow?
  4. TensorFlow Models

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW