c = torch.add(a, b)a = torch.tensor([[1, 2], [3, 4]])
b = torch.tensor([[5, 6], [7, 8]])
c = torch.add(a, b) # tensor([[6, 8], [10, 12]])
a.add_(b) 와 같이 하면 된다.# add 2 tensors with different sizes
a = torch.tensor([[1, 2], [3, 4]])
b = torch.tensor([1, 3])
# b is expanded to tensor([[1, 3], [1, 3]]) and added
c = torch.add(a, b) # tensor([[2, 5], [4, 7]])
c = torch.sub(a,b)a = torch.tensor([[5, 6], [7, 8]])
b = torch.tensor([[1, 2], [3, 4]])
c = torch.sub(a, b) # tensor([[4, 4],[4, 4]])
a.sub_(b) 와 같이 하면 된다.c = torch.mul(a, b) 과 같이 사용a = 3
b = torch.tensor([[1, 2], [3, 4]])
c = torch.tensor([[5, 6], [7, 8]])
# scalar multiplication
d = torch.mul(a, b) # tensor([3, 6], [9, 12]])
# elementwise product (= hardmode product)
e = torch.mul(b, c) # tensor([[5, 12], [21, 28]])
a.mul_(b) 와 같이 하면 된다.c = torch.div(a, b)a.div_(b) 와 같이 하면 된다.a = torch.tensor([[20, 18], [7, 16]])
b = torch.tensor([[5, 6], [7, 8]])
c = torch.div(a, b) # tensor([[4, 3], [1, 3]])
c = torch.pow(a, b)a = torch.tensor([[1, 2], [3, 4]])
b = 3
c = torch.tensor([[5, 4], [3, 2]])
# exponentiation between a scalar and a tensor
d = torch.pow(a, b) # tensor([[1, 8], [27, 64]])
# exponentiation between tensors
e = torch.pow(a, c) # tensor([[1, 16], [27, 16]])
a.pow_(b)c = torch.eq(a, b)a = torch.tensor([[1, 2], [3, 4]])
b = torch.tensor([[1, 5], [3, 4]])
c = torch.eq(a, b) # tensor([[True, False], [True, True]])
torch.ne(a, b)를 이용하면 된다.torch.gt(a, b) 와 같이 사용하는데 a가 b보다 큰지 (greater than) 비교torch.ge(a, b) 를 하면 a가 b보다 크거나 같은지 (greater or equal than) 비교torch.lt(a, b) 해서 a가 b보다 작은지 (less than) 비교torch.le(a, b) 해서 a가 b보다 작거나 같은지 (less or equal than) 비교a = torch.tensor([1, 2, 3, 4])
b = torch.tensor([1, 3, 1, 2])
# compare
c = torch.gt(a, b) # tensor([False, False, True, True])
d = torch.ge(a, b) # tensor([True, False, True, True])
e = torch.lt(a, b) # tensor([False, True, False, False])
f = torch.le(a, b) # tensor([True, True, False, False])
a.matmul(b), a.mm(b), a@b 를 통해서 2-D tensor a와 b 사이의 곱셈을 계산할 수 있다.a = torch.tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
b = torch.tensor([[1, 0], [1, -1], [2, 1]])
c = a.matmul(b)
d = a.mm(b)
e = a@b
# c, d, e all returns tensor([[ 9, 1], [21, 1], [33, 1]])
★ 행렬의 곱셈 연산을 이용하면 흑백 이미지 (→ 2-D tensor) 의 대칭 이동 (축을 기준으로 이미지를 뒤집는 변환) 을 할 수 있다.