Random Tensor Generator

  • Share this:

Code introduction


This function generates a random tensor with a specified shape and data type.


Technology Stack : PyTorch

Code Type : PyTorch Function

Code Difficulty : Intermediate


                
                    
import random
import torch
import torch.nn as nn
import torch.optim as optim

def generate_random_tensor(shape, dtype=torch.float32):
    """
    Generates a random tensor with a given shape and data type.
    """
    return torch.randn(*shape, dtype=dtype)                
              
Tags: