Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Data Science
  • Data Science Projects
  • Data Analysis
  • Data Visualization
  • Machine Learning
  • ML Projects
  • Deep Learning
  • NLP
  • Computer Vision
  • Artificial Intelligence
Open In App
Next Article:
What is Tensor and Tensor Shapes?
Next article icon

What is Tensor and Tensor Shapes?

Last Updated : 06 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Tensors are multidimensional arrays, fundamental to TensorFlow's operations and computations. Understanding key concepts like tensor shape, size, rank, and dimension is crucial for effectively using TensorFlow in machine learning projects.

In this article, we are going to understand tensor and its properties.

What are Tensors?

In TensorFlow, tensors are the basic building blocks used to represent data. A tensor can be thought of as a multi-dimensional array, similar to a matrix but with an arbitrary number of dimensions. Tensors can hold various data types, including integers, floating-point numbers, and strings.

Tensors are important in deep learning frameworks like TensorFlow and PyTorch. The tensors are responsible to provide insights into the structure, dimensions, and size of the tensors. The properties of tensor includes Shape, Rank, Axis and Size.

What is Tensor Shape?

Tensor shape refers to the layout or structure of a tensor, which defines the number of dimensions and the size of each dimension in the tensor. It describes how many elements are along each axis of the tensor.

Python
import tensorflow as tf   # Create a tensor tensor = tf.constant([[1, 2, 3], [4, 5, 6]])  # Get the shape of the tensor shape = tensor.shape  print('Tensor: ', tensor) print('Tensor shape: ', shape) 

Output:

Tensor shape: (2, 3)

Tensor shape is crucial in TensorFlow as determines how the data is organized and how operations can be applied to the tensor. TensorFlow provides methods to get and manipulate the shape of a tensor, allowing developers to work with tensors effectively in machine learning models and other numerical computations.

What is Tensor Size?

Tensor size refers to the total number of elements in a tensor. It is the product of all the dimensions (sizes) of the tensor. In other words, it represents the total amount of data stored in the tensor.

Python
import tensorflow as tf tensor = tf.constant([[1, 2, 3], [4, 5, 6]])  # Get the size of the tensor size = tf.size(tensor) print("Tensor size: ", size) 

Output:

Tensor size: tf.Tensor(6, shape=(), dtype=int32)

What is Tensor Rank?

Tensor rank, also known as the tensor's number of dimensions, is a fundamental concept in TensorFlow. It indicates the number of dimensions present in a tensor.

Here's a brief overview of tensor rank:

  • Rank 0: Scalars. Tensors of rank 0 represent single values.
  • Rank 1: Vectors. Tensors of rank 1 have one dimension and represent arrays of values.
  • Rank 2: Matrices. Tensors of rank 2 have two dimensions and represent 2D arrays of values.
  • Rank 3 and above: Tensors of rank 3 or higher have three or more dimensions and represent higher-dimensional arrays of values.
Python
import tensorflow as tf  # Create tensors of different ranks scalar = tf.constant(5)  # Rank 0 tensor (scalar) vector = tf.constant([1, 2, 3])  # Rank 1 tensor (vector) matrix = tf.constant([[1, 2], [3, 4]])  # Rank 2 tensor (matrix) tensor_3d = tf.constant([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])  # Rank 3 tensor (3D)  # Get the rank of tensors rank_scalar = tf.rank(scalar) rank_vector = tf.rank(vector) rank_matrix = tf.rank(matrix) rank_3d = tf.rank(tensor_3d)  print('Rank of scalar:', rank_scalar.numpy()) print('Rank of vector:', rank_vector.numpy()) print('Rank of matrix:', rank_matrix.numpy()) print('Rank of 3D tensor:', rank_3d.numpy()) 

Output:

Rank of scalar: 0
Rank of vector: 1
Rank of matrix: 2
Rank of 3D tensor: 3

What is Tensor Dimension?

Tensor dimension refers to the length along a particular axis of a tensor. In simpler terms, it is the size or extent of a tensor along a specific direction. Each axis of a tensor corresponds to a dimension, and the number of dimensions in a tensor is its rank.

Python
import tensorflow as tf tensor = tf.constant([[1, 2, 3], [4, 5, 6]]) shape = tensor.shape  # Get the length of the first dimension dim1_length = shape[0]  # Get the length of the second dimension dim2_length = shape[1]  print('Tensor shape:', shape) print('Length of first dimension:', dim1_length) print('Length of second dimension:', dim2_length) 

Output:

Tensor shape: (2, 3)
Length of first dimension: 2
Length of second dimension: 3

Next Article
What is Tensor and Tensor Shapes?

S

shivangibhargava22
Improve
Article Tags :
  • Deep Learning
  • Dev Scripter
  • AI-ML-DS
  • Tensorflow
  • Dev Scripter 2024

Similar Reads

    How to Reshape a Tensor in Tensorflow?
    Tensor reshaping is the process of reshaping the order and total number of elements in tensors while only the shape is being changed. It is a fundamental operation in TensorFlow that allows you to change the shape of a tensor without changing its underlying data. Using tf.reshape() In TensorFlow, th
    4 min read
    Tensorflow.js tf.Tensor Class
    Tensorflow.js is an open-source library developed by Google for running machine learning models and deep learning neural networks in the browser or node environment. A tf.Tensor object represents an immutable, multidimensional array of numbers that has a shape and a data type. Tensors are the core d
    4 min read
    Tensorflow - is_tensor() method
    TensorFlow is an open-source Python library designed by Google to develop Machine Learning models and deep learning neural networks. Tensors are used by many Tensorflow operations. Sometimes it is important to check whether given variable is a tensor or not before using it. is_tensor() method can be
    2 min read
    Sparse tensors in Tensorflow
    Imagine you are working with a massive dataset which is represented by multi-dimensional arrays called tensors. In simple terms, tensors are the building blocks of mathematical operations on the data. However, sometimes, tensors can have majority of values as zero. Such a tensor with a lot of zero v
    10 min read
    Tensor Indexing in Tensorflow
    In the realm of machine learning and deep learning, tensors are fundamental data structures used to represent numerical data with multiple dimensions. TensorFlow, a powerful numerical computation library, equips you with an intuitive and versatile set of operations for manipulating and accessing dat
    10 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences