Generating Random Tensors with PyTorch

  • Share this:

Code introduction


This function uses the PyTorch library to generate a random tensor of a specified shape.


Technology Stack : PyTorch

Code Type : The type of code

Code Difficulty : Intermediate


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

def generate_random_tensor(shape):
    """
    Generates a random tensor of a specified shape using torch.

    Args:
        shape (tuple): The shape of the tensor to generate.

    Returns:
        torch.Tensor: A random tensor with the specified shape.
    """
    return torch.randn(*shape)                
              
Tags: