- Pytorch Tensor 기본연산

\[a= \begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix},\quad b= \begin{bmatrix} 5 & 6 \\ 7 & 8 \end{bmatrix}\]
import torch
a = torch.Tensor([[1,2], [3,4]])
b = torch.Tensor([[5,6], [7,8]])


  • \[a + b = \begin{bmatrix} 1 + 5 & 2 + 6\\ 3 + 7 & 4 + 8 \end{bmatrix}\]
print(a + b)
tensor([[ 6.,  8.],
        [10., 12.]])


  • \[a - b = \begin{bmatrix} 1 - 5 & 2 - 6\\ 3 - 7 & 4 - 8 \end{bmatrix}\]
print(a - b)
tensor([[-4., -4.],
        [-4., -4.]])


  • \[a * b = \begin{bmatrix} 1 * 5 & 2 * 6\\ 3 * 7 & 4 * 8 \end{bmatrix}\]
print(a * b)
print(a.mul(b))
tensor([[ 5., 12.],
        [21., 32.]])
tensor([[ 5., 12.],
        [21., 32.]])


  • \[a / b = \begin{bmatrix} 1 / 5 & 2 / 6\\ 3 / 7 & 4 / 8 \end{bmatrix}\]
print(a / b)
tensor([[0.2000, 0.3333],
        [0.4286, 0.5000]])


  • \[a + b = \begin{bmatrix} 1 ^ 5 & 2 ^ 6\\ 3 ^ 7 & 4 ^ 8 \end{bmatrix}\]
print(a ** b)
tensor([[1.0000e+00, 6.4000e+01],
        [2.1870e+03, 6.5536e+04]])


  • \[a == b = \begin{bmatrix} 1 == 5 & 2 == 6\\ 3 == 7 & 4 == 8 \end{bmatrix}\]
print(a == b)
tensor([[False, False],
        [False, False]])


  • \[a != b = \begin{bmatrix} 1 != 5 & 2 != 6\\ 3 != 7 & 4 != 8 \end{bmatrix}\]
print(a != b)
tensor([[True, True],
        [True, True]])

태그:

카테고리:

업데이트:

댓글남기기