You can download this code by clicking the button below.
This code is now available for download.
This function takes a 2D tensor as input and returns its transpose. If the input is not a 2D tensor, it raises an error.
Technology Stack : PyTorch
Code Type : Function
Code Difficulty : Intermediate
import torch
import random
def random_matrix_transpose(input_matrix):
# This function takes a tensor as input and returns its transpose.
# The tensor must be 2-dimensional.
# Check if the input is a 2D tensor
if input_matrix.dim() != 2:
raise ValueError("Input tensor must be 2-dimensional")
# Transpose the matrix
transposed_matrix = input_matrix.t()
return transposed_matrix