Open In App

Tensorflow.js tf.transpose() Function

Last Updated : 12 May, 2021
Improve
Improve
Like Article
Like
Save
Share
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 tf.transpose() function is used to perform a regular matrix transpose operation on the specified 2-D input tensor.

Syntax:

tf.transpose (x)

Parameters: This function accepts a parameter which is illustrated below:

  • x: The specified input tensor to transpose.

Return Value: It returns a tensor as the output for the transpose operation.

Example 1:

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Initializing a tensor of matrix along with
// its dimension
const a = tf.tensor2d([2, 4, 6, 8], [2, 2]);
  
// Calling the .transpose() function over the
// above tensor as its parameter
a.transpose().print();


Output:

 Tensor
   [[2, 6],
    [4, 8]]

Example 2:

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Using some tensor of matrix along with its dimensions
// as the parameter for .transpose() functions
tf.tensor2d([1, 3, 5, 7, 9, 11], [2, 3]).transpose().print(); 
tf.tensor2d([1, 3, 5, 7, 9, 11], [3, 2]).transpose().print();


Output:

Tensor
   [[1, 7 ],
    [3, 9 ],
    [5, 11]]
Tensor
   [[1, 5, 9 ],
    [3, 7, 11]]

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


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads