[학부수업] Practicing Pytorch (9-1)
torch를 import
import torch
data를 리스트 형태로 선언 후 tensor로 변환 및 저장
data = [[1,2,3,4],[2,5,7,8],[0,1,0,1]]
tensor = torch.tensor(data)
data와 tensor 출력
print("data:",data)
print("tensor:",tensor)
data: [[1, 2, 3, 4], [2, 5, 7, 8], [0, 1, 0, 1]]
tensor: tensor([[1, 2, 3, 4],
[2, 5, 7, 8],
[0, 1, 0, 1]])
tensor - 인덱싱
python의 list처럼 tensor 역시 인덱싱이 가능하다.
print(tensor[1]) # 1행만 출력
print(tensor[:,2]) # 2열만 출력
# print(data[:,2]) # list는 tensor처럼 인덱싱 불가능
print(tensor.size()) # 3행 4열
tensor([2, 5, 7, 8])
tensor([3, 7, 0])
torch.Size([3, 4])
tensor - transposing
torch.transpose()를 통해 tensor의 차원을 원하는 대로 바꿔줄 수 있다.
x = torch.rand(2,1,3,3) # 2x1x3x3 tensor
print(x)
y = torch.transpose(x,1,2) # 1차원과 2차원 채널 transpose, 2x3x1x3
print("")
print(y.size())
print(y)
tensor([[[[0.0770, 0.2961, 0.1975],
[0.5103, 0.9089, 0.9992],
[0.8630, 0.6294, 0.8499]]],
[[[0.1664, 0.6861, 0.7740],
[0.0430, 0.3935, 0.6138],
[0.0684, 0.1667, 0.4450]]]])
torch.Size([2, 3, 1, 3])
tensor([[[[0.0770, 0.2961, 0.1975]],
[[0.5103, 0.9089, 0.9992]],
[[0.8630, 0.6294, 0.8499]]],
[[[0.1664, 0.6861, 0.7740]],
[[0.0430, 0.3935, 0.6138]],
[[0.0684, 0.1667, 0.4450]]]])
tensor - view
tensor.view() 함수를 이용해 tensor의 크기와 차원을 원하는 대로 수정(reshape)할 수 있다.
def print_tensor(tensor):
print(tensor)
print("Size of is {}".format(tensor.size()))
print("")
ex1.
x = torch.rand(4,4)
print_tensor(x)
y = x.view(16)
print_tensor(y)
z = x.view(2,-1)
print_tensor(z)
tensor([[0.8863, 0.4909, 0.3536, 0.7552],
[0.7911, 0.5916, 0.9027, 0.4505],
[0.1062, 0.5460, 0.5813, 0.5697],
[0.1520, 0.1169, 0.9155, 0.4392]])
Size of is torch.Size([4, 4])
tensor([0.8863, 0.4909, 0.3536, 0.7552, 0.7911, 0.5916, 0.9027, 0.4505, 0.1062,
0.5460, 0.5813, 0.5697, 0.1520, 0.1169, 0.9155, 0.4392])
Size of is torch.Size([16])
tensor([[0.8863, 0.4909, 0.3536, 0.7552, 0.7911, 0.5916, 0.9027, 0.4505],
[0.1062, 0.5460, 0.5813, 0.5697, 0.1520, 0.1169, 0.9155, 0.4392]])
Size of is torch.Size([2, 8])
ex2.
x = torch.rand(4,3,3)
print_tensor(x)
y = x.view(-1,9) # ch : 4,9
print_tensor(y)
z = x.view(3,2,-1) # 3,2,6
print_tensor(z)
tensor([[[0.6672, 0.0262, 0.4320],
[0.4642, 0.6424, 0.1856],
[0.7654, 0.4409, 0.3794]],
[[0.4826, 0.2202, 0.1335],
[0.0248, 0.9301, 0.0059],
[0.5290, 0.3606, 0.7517]],
[[0.5727, 0.7692, 0.6940],
[0.6618, 0.0026, 0.2463],
[0.5570, 0.2253, 0.7165]],
[[0.2668, 0.3071, 0.8153],
[0.5398, 0.8994, 0.7308],
[0.4200, 0.6862, 0.4599]]])
Size of is torch.Size([4, 3, 3])
tensor([[0.6672, 0.0262, 0.4320, 0.4642, 0.6424, 0.1856, 0.7654, 0.4409, 0.3794],
[0.4826, 0.2202, 0.1335, 0.0248, 0.9301, 0.0059, 0.5290, 0.3606, 0.7517],
[0.5727, 0.7692, 0.6940, 0.6618, 0.0026, 0.2463, 0.5570, 0.2253, 0.7165],
[0.2668, 0.3071, 0.8153, 0.5398, 0.8994, 0.7308, 0.4200, 0.6862, 0.4599]])
Size of is torch.Size([4, 9])
tensor([[[0.6672, 0.0262, 0.4320, 0.4642, 0.6424, 0.1856],
[0.7654, 0.4409, 0.3794, 0.4826, 0.2202, 0.1335]],
[[0.0248, 0.9301, 0.0059, 0.5290, 0.3606, 0.7517],
[0.5727, 0.7692, 0.6940, 0.6618, 0.0026, 0.2463]],
[[0.5570, 0.2253, 0.7165, 0.2668, 0.3071, 0.8153],
[0.5398, 0.8994, 0.7308, 0.4200, 0.6862, 0.4599]]])
Size of is torch.Size([3, 2, 6])
Leave a comment