Learn practical skills, build real-world projects, and advance your career

Creating Tensor data

An short introduction about PyTorch and about the chosen functions.

  • function 1 : creating tonsr data and fill it by zero or random
  • function 2 : Concatenates the given sequence of seq tensors in the given dimension.
  • function 3 : Splits the tensor into chunks
  • function 4 : Convert the data into a torch.Tensor.
  • function 5 : Returns a tensor of random numbers drawn from separate normal distributions

Before we begin, let's install and import PyTorch

# Uncomment and run the appropriate command for your operating system, if required

# Linux / Binder
# !pip install numpy torch==1.7.0+cpu torchvision==0.8.1+cpu torchaudio==0.7.0 -f https://download.pytorch.org/whl/torch_stable.html

# Windows
# !pip install numpy torch==1.7.0+cpu torchvision==0.8.1+cpu torchaudio==0.7.0 -f https://download.pytorch.org/whl/torch_stable.html

# MacOS
# !pip install numpy torch torchvision torchaudio
# Import torch and other required modules
import torch

Function 1 - Create data with random function

torch.randn give you the ability to create data with given size. it's very useful when you don't have a data and want to create a random data.

# Example 1 - working with Randn function
#Function 1 (create a data) #https://pytorch.org/docs/stable/generated/torch.randn.html#torch.randn

inputs = torch.zeros(3,4)
display (inputs)
inputs = torch.randn(3,4)
display(inputs)
tensor([[0., 0., 0., 0.],
        [0., 0., 0., 0.],
        [0., 0., 0., 0.]])
tensor([[ 0.3443,  0.5673,  0.6769, -0.6091],
        [-0.0864, -1.1892,  1.4165,  0.7665],
        [ 0.9718,  1.0737, -0.5271,  1.3133]])