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)
Addition¶
+ operator¶
In [6]:
t_x1 + t_x2
Out[6]:
torch.add
method¶
In [7]:
torch.add(t_x1, t_x2)
Out[7]:
In [8]:
torch.add(t_x1, -t_x2)
Out[8]:
Multiplication¶
Operator¶
In [9]:
t_x1 * t_x2
Out[9]:
torch.mm
method¶
In [11]:
torch.mm(t_x1, t_x2)
Out[11]:
Inner product torch.dot
¶
In [13]:
print(t_x1[:, 1], t_x2[:, 1])
torch.dot(t_x1[:, 1], t_x2[:, 1])
Out[13]:
Trace¶
In [15]:
torch.trace(t_x1)
Out[15]:
Diag¶
In [16]:
torch.diag(t_x1)
Out[16]:
Determinant¶
In [17]:
torch.det(t_x1)
Out[17]:
Comments
Comments powered by Disqus