Skip to content
geeksforgeeks
  • 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
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • TypeScript
  • Vue.js
  • D3.js
  • Collect.js
  • Underscore.js
  • Moment.js
  • Ember.js
  • Tensorflow.js
  • Fabric.js
  • JS Formatter
  • JavaScript
  • Web Technology
Open In App
Next Article:
Tensorflow.js tf.norm() Function
Next article icon

Tensorflow.js tf.tensor2d() Function

Last Updated : 28 Apr, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

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. The .tensor2d() function is used to create a new 2-dimensional tensor with the parameter namely value, shape, and datatype.

Syntax:

tf.tensor2d (value, shape, datatype)

Parameters:  

  • value: The value of the tensor which can be nested array of numbers, or a flat array, or a TypedArray.
  • shape: It takes the shape of the tensor. The tensor will infer its shape from the value if it is not provided. It is an optional parameter.
  • datatype: It can be a 'float32' or 'int32' or 'bool' or 'complex64' or 'string' value. It is an optional parameter.

Return Value: It returns the tensor of the same data type. The returned tensor will always be 2-dimensional.

Note: The 2d tensor functionality can also be achieved using tf.tensor() function, but using tf.tensor2d() makes the code easily understandable and readable.

Example 1: In this example, we are creating a 2D tensor and printing it. For creating a 2D tensor we are using the .tensor2d() function, and we use .print() function to print the tensor. Here, we will pass the 2D array (i.e. nested array) to the value parameter.

JavaScript
// Importing the tensorflow.js library import * as tf from "@tensorflow/tfjs";  // Create the tensor let example1 = tf.tensor2d([ [ 1, 2, 3 ], [ 4, 5, 6 ] ]);  // Print the tensor example1.print() 

Output:

Tensor      [[1, 2, 3],       [4, 5, 6]]

Example 2: In this example, we are creating the tensor where we are passing the flat array and specifying the shape parameter of the tensor. We will see the usage of shape parameter here.

JavaScript
// Import the tensorflow.js library import * as tf from "@tensorflow/tfjs";    // Define the value of the tensor const value = [1, 2, 3, 4, 5, 6, 7, 8];  // Specify the shape of the tensor const shape = [2, 4];  // Create the tensor let example2 = tf.tensor2d(value, shape);  // Print the tensor example2.print(); 

Output:

Tensor      [[1, 2, 3, 4],       [5, 6, 7, 8]]

In the above example, we have created a tensor of 2 x 4 dimension.

Example 3: In this example, we will create a tensor by specifying the value, shape, and datatype. We will create the tensor where all the values are of string datatype.

JavaScript
// Import the tensorflow.js library import * as tf from "@tensorflow/tfjs";  // Define the value of the tensor const value = ["C", "C++", "Java", "Python",                "PHP", "JS", "SQL", "React"];  // Specify the shape of the tensor const shape = [2, 4];  // Specify the datatype of value of the tensor const datatype = "string";  // Create the tensor let example3 = tf.tensor2d(value, shape, datatype);  // Print the tensor example3.print(); 

Output:

Tensor      [['C'  , 'C++', 'Java', 'Python'],       ['PHP', 'JS' , 'SQL' , 'React' ]]

In the above example, the printed values of the tensor are of string datatype.

Reference: https://js.tensorflow.org/api/latest/#tensor2d

Next Article
Tensorflow.js tf.norm() Function
author
girish_thatte
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • Tensorflow.js

Similar Reads

  • Tensorflow.js tf.tensor4d() Function
    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. The .tensor4d() function is used to create a new 4-dimensional tensor with the parameters namely value, shape, and datatype. Syntax:
    3 min read
  • Tensorflow.js tf.tensor3d() Function
    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. The .tensor3d() function is used to create a new 3-dimensional tensor with the parameters namely value, shape, and datatype. Syntax: t
    3 min read
  • Tensorflow.js tf.tensor5d() Function
    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. The .tensor5d() function is used to create a new 5-dimensional tensor with the parameters namely value, shape, and datatype. Syntax :
    3 min read
  • Tensorflow.js tf.tensor6d() Function
    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. The .tensor6d() function is used to create a new 6-dimensional tensor with the parameters namely value, shape, and datatype. Syntax:
    3 min read
  • Tensorflow.js tf.norm() Function
    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. The tf.norm() function is used compute the norm of matrices, vectors, and scalar. This function can also compute several other vector
    2 min read
  • Tensorflow.js tf.tan() Function
    Tensorflow.js is an open-source library that is being developed by Google for running machine learning models as well as deep learning neural networks in the browser or node environment. The .tan() function is used to find the tangent of the stated tensor input and is done element wise. Syntax: tf.t
    2 min read
  • Tensorflow.js tf.memory() Function
    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. Tensorflow.js tf.memory() function is used to get memory info of the program at the current time. This function returns a memoryInfo o
    2 min read
  • Tensorflow.js tf.prod() Function
    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. The tf.prod() function is used to calculate product of the elements of a specified Tensor across its dimension. It reduces the given i
    2 min read
  • Tensorflow.js tf.step() Function
    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. The tf.step() function is used to return the step of the input tensor's elements i.e. it returns 1 if the element is greater than 0 el
    2 min read
  • Tensorflow.js tf.ones() Function
    Tensorflow.js is an open-source library for running machine learning models and deep learning neural networks in the browser or node environment. The tf.ones() function is used to create a new tensor where all elements are set to 1. Syntax: tf.ones(shape, dtype, name) Parameters: shape: It takes the
    2 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