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

Assignment 1 - Tensor Operations

Chosen Functions

Here shows some useful basic functions in PyTorch.

  • torch.tensor()
  • torch.randn()
  • torch.cat()
  • torch.arange()
  • torch.from_numpy()
# Import torch and other required modules
import torch

Function 1 - torch.tensor

This is the fundamental function to create one tensor (an array of components / vectors).

# Example 1 - creating one float32 tensor
print(torch.tensor([[1., 2], [3, 4]]))
print(torch.tensor([[1., 2], [3, 4]]).dtype)
tensor([[1., 2.], [3., 4.]]) torch.float32

It creates one tensor with 2 rows of 2 float32 elements.