Tensor Manipulation (1)

POOHYA·2021년 12월 5일
0

데이터 단위

  • 스칼라 - 차원이 없는 값
  • 벡터 - 1차원 텐서
  • 메트릭스 - 2차원 텐서
  • 텐서 - 3차원

텐서 크기 구하기

2차원 텐서 크키 = batch size x dim

3차원 비전 텐서 크기 = batch size x dim x height

3차원 nlp 텐서 크기 = batch size x length x dim

Tensor Manipulation

1D tensor with Pytorch

1차원 텐서의 dim, shape, size, Slicing

t = torch.FloatTensor([0., 1., 2., 3., 4., 5., 6.])
print(t.dim())  # rank  # 배열의 차원 수 or 배열의 축 수
print(t.shape)  # shape  # 텐서의 크기
print(t.size()) # shape와 같음
print(t[0], t[1], t[-1])  # Element
print(t[2:5], t[4:-1])    # Slicing
print(t[:2], t[3:])       # Slicing
1
torch.Size([7])
torch.Size([7])
tensor(0.) tensor(1.) tensor(6.)
tensor([2., 3., 4.]) tensor([4., 5.])
tensor([0., 1.]) tensor([3., 4., 5., 6.])

2D tensor with PyTorch

2차원 텐서의 dim, shape, size, Slicing

t = torch.FloatTensor([[1., 2., 3.],
                       [4., 5., 6.],
                       [7., 8., 9.],
                       [10., 11., 12.]
                      ])
print(t.dim())  # rank
print(t.size()) # shape
print(t[:, 1])
print(t[:, 1].size())
print(t[:, :-1])
2
torch.Size([4, 3])
tensor([ 2.,  5.,  8., 11.])
torch.Size([4])
tensor([[ 1.,  2.],
        [ 4.,  5.],
        [ 7.,  8.],
        [10., 11.]])

Broadcasting

same shape

m1 = torch.FloatTensor([[3, 3]])
m2 = torch.FloatTensor([[2, 2]])
print(m1 + m2)
tensor([[5., 5.]])

Vector + Scalar

m1 = torch.FloatTensor([[1, 2]])
m2 = torch.FloatTensor([3])     # [[3, 3]]
print(m1 + m2)
tensor([[4., 5.]])

2 x 1 Vector + 1 x 2 Vector

m1 = torch.FloatTensor([[1, 2]])	# [[1, 2], [1, 2]]
m2 = torch.FloatTensor([[3], [4]])	# [[3, 4], [4, 4]]
print(m1 + m2)
tensor([[4., 5.],
        [5., 6.]])

Multiplication vs Matrix Multiplication

곱셈(.mul)

m1 = torch.FloatTensor([[1, 2], [3, 4]])
m2 = torch.FloatTensor([[1], [2]])
print('Shape of Matrix 1: ', m1.shape)
print('Shape of Matrix 2: ', m2.shape) 
print(m1 * m2)		# m2를 [[1, 1], [2, 2]]로 broadcasting
print(m1.mul(m2))	# m2를 [[1, 1], [2, 2]]로 broadcasting
Shape of Matrix 1:  torch.Size([2, 2])
Shape of Matrix 2:  torch.Size([2, 1])
tensor([[1., 2.],
        [6., 8.]])
tensor([[1., 2.],
        [6., 8.]])

행렬곱셈(.matmul)

m1 = torch.FloatTensor([[1, 2], [3, 4]])
m2 = torch.FloatTensor([[1], [2]])
print('Shape of Matrix 1: ', m1.shape) # 2 x 2
print('Shape of Matrix 2: ', m2.shape) # 2 x 1
print(m1.matmul(m2)) # 2 x 1 #일치하는 행이 사라짐
Shape of Matrix 1:  torch.Size([2, 2])
Shape of Matrix 2:  torch.Size([2, 1])
tensor([[ 5.],
        [11.]])

Mean

t = torch.FloatTensor([1, 2])
print(t.mean())
t = torch.LongTensor([1, 2])
try:
    print(t.mean())
except Exception as exc:
    print(exc)
tensor(1.5000)

Can only calculate the mean of floating types. Got Long instead.
You can also use t.mean for higher rank tensors to get mean of all elements, or mean by particular dimension.
t = torch.FloatTensor([[1, 2], [3, 4]])
print(t.mean())
print(t.mean(dim=0))	# 1 x 2
print(t.mean(dim=1)) 	# 2 x 1
print(t.mean(dim=-1))
tensor(2.5000)
tensor([2., 3.])	
tensor([1.5000, 3.5000])
tensor([1.5000, 3.5000])

Sum

t = torch.FloatTensor([[1, 2], [3, 4]])
print(t.sum())
print(t.sum(dim=0))
print(t.sum(dim=1))
print(t.sum(dim=-1))
tensor(10.)
tensor([4., 6.])
tensor([3., 7.])
tensor([3., 7.])

Max and Argmax

t = torch.FloatTensor([[1, 2], [3, 4]])
print(t.max()) 
print(t.max(dim=0))
print('Max: ', t.max(dim=0)[0])
print('Argmax: ', t.max(dim=0)[1])
print(t.max(dim=1))
print(t.max(dim=-1))
tensor(4.)
(tensor([3., 4.]), tensor([1, 1])) # index값 같이 리턴
Max:  tensor([3., 4.])
Argmax:  tensor([1, 1])
(tensor([2., 4.]), tensor([1, 1]))
(tensor([2., 4.]), tensor([1, 1]))
profile
김효주

0개의 댓글