Posts about Deep Learning (old posts, page 1)

PyTorch Basic Operations

Goal

This post aims to introduce basic PyTorch operations e.g., addition, multiplication,

Libraries

In [2]:
import numpy as np
import pandas as pd
import torch

Create a Tensor

In [5]:
t_x1 = torch.Tensor([[1, 2, 3],
                     [4, 5, 6],
                     [7, 8, 9]])

t_x2 = torch.Tensor([[9, 8, 7],
                     [6, 5, 4],
                     [3, 2, 1]])
print(t_x1)
print(t_x2)
tensor([[1., 2., 3.],
        [4., 5., 6.],
        [7., 8., 9.]])
tensor([[9., 8., 7.],
        [6., 5., 4.],
        [3., 2., 1.]])

Addition

+ operator

In [6]:
t_x1 + t_x2
Out[6]:
tensor([[10., 10., 10.],
        [10., 10., 10.],
        [10., 10., 10.]])