PyTorch
install pytorch
pip install torch torchvision실행 결과:
import torch
import torch실행 결과:
Tensor
torch.tensor([1, 2, 3, 4])실행 결과:
torch.tensor([1, 2, 3, 4])1차원 텐서 -> 2차원 텐서(reshape)
torch.arange(8, dtype=torch.int32).reshape((2, 4))실행 결과:
tensor([[0, 1, 2, 3], [4, 5, 6, 7]], dtype=torch.int32)텐서 병합(cat)
tensor1 = torch.tensor([[1, 2], [3, 4]])
tensor2 = torch.tensor([[5, 6], [7, 8]])
torch.cat((tensor1, tensor2), dim=0)실행 결과:
[[1, 2], [3, 4], [5, 6], [7, 8]]텐서 복사
A = torch.arange(4)
B = A
C = A.clone()
id(A) == id(B), id(A) == id(C)실행 결과:
(True, False)텐서 열 합(sum)
A = torch.arange(12).reshape((3, 4))
_sum = A.sum(dim=0)
A, A_sum실행 결과:
(tensor([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]]),
tensor([12, 15, 18, 21]))텐서 행 합 - 차원 유지
A_sum = A.sum(axis=1, keepdims=True)
A, A_sum실행 결과:
(tensor([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]]),
tensor([[ 6],
[22],
38]]))행렬의 곱(dot product)
torch.mv(A, B)실행 결과:
벡터의 길이(norm)
V = torch.tensor([3.0, -4.0, 1.0])
V_norm = torch.norm(V)
V_norm실행 결과:
tensor(5.0990)실행 결과:
실행 결과:
실행 결과:
실행 결과:
실행 결과:
실행 결과:
실행 결과:
실행 결과:
실행 결과:
실행 결과: