PyTorch Basics

Programming
Data Science
Machine Learning
Python
PyTorch
Author

Rafiq Islam

Published

October 16, 2024

Introduction to Tensors

import torch
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
print(torch.__version__)
2.3.1+cu121

Creating Tensors

  • Scaler

    scaler = torch.tensor(7)
    print(scaler)
    print(scaler.ndim)
    tensor(7)
    0
  • Vector

    vec = torch.tensor([2,3,4])
    print(vec.ndim)
    print(vec.shape)
    1
    torch.Size([3])
  • Matrix

    MAT = torch.tensor([[2,3,4],
                        [3,2,6]])
    MAT
    tensor([[2, 3, 4],
            [3, 2, 6]])
  • Tensor

    TEN = torch.tensor([[[2,3,5],
                         [5,4,3]]])
    TEN.shape
    torch.Size([1, 2, 3])
Back to top