Random Matrix Generator with Specified Data Type

  • Share this:

Code introduction


This function generates a random matrix of specified size with a specified data type. The element type of the matrix can be float32, float64, etc.


Technology Stack : torch

Code Type : Generate random matrix

Code Difficulty : Intermediate


                
                    
import torch
import random

def random_matrix_generator(rows, cols, dtype=torch.float32):
    """
    Generates a random matrix of given size with a specified data type.
    """
    return torch.randn(rows, cols, dtype=dtype)                
              
Tags: