텐서의 연산(Operations)
텐서에 대한 수학 연산, 삼각함수, 비트 연산, 비교 연산, 집계 등 사용
import math
import torch
a = torch.rand(1,2) * 2 - 1
print(a)
print(torch.abs(a))
print(torch.ceil(a))
print(torch.floor(a))
print(torch.clamp(a, -0.5, 0.5)) # 최대값, 최소값 설정
tensor([[ 0.7607, -0.2887]])
tensor([[0.7607, 0.2887]])
tensor([[1., -0.]])
tensor([[ 0., -1.]])
tensor([[ 0.5000, -0.2887]])
print(a)
print(torch.min(a))
print(torch.max(a))
print(torch.mean(a))
print(torch.std(a))
print(torch.prod(a))
print(torch.unique(torch.tensor([1,2,3,1,2,2])))
tensor([[ 0.7607, -0.2887]])
tensor(-0.2887)
tensor(0.7607)
tensor(0.2360)
tensor(0.7420)
tensor(-0.2196)
tensor([1, 2, 3])
max와 min은 dim(dimension) 인자를 줄 경우 argmax와 argmin도 함께 리턴 argmax: 최대값을 가진 인덱스
argmin: 최소값을 가진 인덱스
print(x)
print(x.max(dim=0))
print(x.max(dim=1))
tensor([[0.1984, 0.7085],
[0.4191, 0.0141]])
torch.return_types.max(
values=tensor([0.4191, 0.7085]),
indices=tensor([1, 0]))
torch.return_types.max(
values=tensor([0.7085, 0.4191]),
indices=tensor([1, 0]))
print(x)
print(x.min(dim=0))
print(x.min(dim=1))
tensor([[0.1984, 0.7085],
[0.4191, 0.0141]])
torch.return_types.min(
values=tensor([0.1984, 0.0141]),
indices=tensor([0, 1]))
torch.return_types.min(
values=tensor([0.1984, 0.0141]),
indices=tensor([0, 1]))
x = torch.rand(2, 2)
print(x)
y = torch.rand(2, 2)
print(y)
tensor([[0.9140, 0.9373],
[0.2630, 0.9968]])
tensor([[0.4242, 0.4459],
[0.6237, 0.2338]])
torch.add : 덧셈 print(x+y)
print(torch.add(x,y))
tensor([[1.3381, 1.3832],
[0.8867, 1.2305]])
tensor([[1.3381, 1.3832],
[0.8867, 1.2305]])
결과 텐서를 인자로 제공
result = torch.empty(2, 4)
torch.add(x, y, out=result) #x와 y의 합을 result에 저장 print(result)
tensor([[1.3381, 1.3832],
[0.8867, 1.2305]])
<ipython-input-10-81b52643204b>:2: UserWarning: An output with one or more torch.add(x, y, out=result) #x와 y의 합을 result에 저장
in-place 방식
in-place방식으로 텐서의 값을 변경하는 연산 뒤에는 _"가 붙음 x.copy_(y), x.t_()
print(x)
print(y)
y.add_(x) # return값이 y에 바로 저장 즉, y = torch.add(x,y)와 같은 기능 print(y)
tensor([[0.9140, 0.9373],
[0.2630, 0.9968]])
tensor([[0.4242, 0.4459],
[0.6237, 0.2338]])
tensor([[1.3381, 1.3832],
[0.8867, 1.2305]])
torch.sub: 뺄셈
print(x)
print(y)
print(x-y)
print(torch.sub(x,y))
print(x.sub(y))
tensor([[0.9140, 0.9373],
[0.2630, 0.9968]])
tensor([[1.3381, 1.3832],
[0.8867, 1.2305]])
tensor([[-0.4242, -0.4459],
[-0.6237, -0.2338]])
tensor([[-0.4242, -0.4459],
[-0.6237, -0.2338]])
tensor([[-0.4242, -0.4459],
[-0.6237, -0.2338]])
torch.mul: 곱셈
print(x)
print(y)
print(x*y)
print(torch.mul(x,y))
print(x.mul(y))
tensor([[0.9140, 0.9373],
[0.2630, 0.9968]])
tensor([[1.3381, 1.3832],
[0.8867, 1.2305]])
tensor([[1.2230, 1.2965],
[0.2332, 1.2266]])
tensor([[1.2230, 1.2965],
[0.2332, 1.2266]])
tensor([[1.2230, 1.2965],
[0.2332, 1.2266]])
torch.div: 나눗셈
print(x)
print(y)
print(x/y)
print(torch.div(x,y))
print(x.div(y))
tensor([[0.9140, 0.9373],
[0.2630, 0.9968]])
tensor([[1.3381, 1.3832],
[0.8867, 1.2305]])
tensor([[0.6830, 0.6776],
[0.2966, 0.8100]])
tensor([[0.6830, 0.6776],
[0.2966, 0.8100]])
tensor([[0.6830, 0.6776],
[0.2966, 0.8100]])
torch.mm: 내적(dot product)
print(x)
print(y)
print(torch.matmul(x,y))·#matrix·product(multiply)·행렬곱
z·=·torch.mm(x,y)
print(z)
print(torch.svd(z))·#·특이값·분해·singular·value·decomposition print(torch.matmul(x,y)) #matrix product(multiply) 행렬곱
tensor([[0.9140, 0.9373],
[0.2630, 0.9968]])
tensor([[1.3381, 1.3832],
[0.8867, 1.2305]])
tensor([[2.0541, 2.4176],
[1.2358, 1.5904]])
tensor([[2.0541, 2.4176],
[1.2358, 1.5904]])
torch.return_types.svd(
U=tensor([[-0.8443, -0.5358],
[-0.5358, 0.8443]]),
S=tensor([3.7570, 0.0743]),
V=tensor([[-0.6379, -0.7701],
[-0.7701, 0.6379]]))