[PyTorch] 1. PyTorch의 기초 : tensor

happy_quokka·2023년 12월 16일
0

PyTorch

목록 보기
1/1

PyTorch

  • 딥러닝 프레임워크
  • tensorflow : define - run
  • pytorch : define by run

tensor

tensor 초기화

data 넣어서 생성

  • 데이터 타입을 안 적어주면 자동으로 유추
#int16
a = torch.tensor([[1,2],[3,4]], dtype=torch.int16)
#float
b = torch.tensor([2], dtype=torch.float32)
#double
c = torch.tensor([3], dtype=torch.float64)

무작위, 상수 값 사용해서 생성

shape = (2, 1)
zero_t = torch.zeros(3) #tensor([0., 0., 0.])
one_t = torch.ones(shape) #tensor([[1.],[1.]])
random_t = torch.rand(3) #tensor([0.0645, 0.4991, 0.2454])
range_t = torch.arange(0,5) #tensor([0, 1, 2, 3, 4])

numpy 배열로부터 생성

data = [[1,2],[3,4]]
np_array = np.array(data)
t_np = torch.from_numpy(np_array)

#반대의 경우도 가능
a = torch.arange(1, 13).view(4, 3)
a_np = a.numpy()

tensor 속성

  • t.shape : tensor의 shape 확인
  • t.dtype : tensor의 data type 확인
  • t.device : tensor가 올라가있는 device 확인, cpu인지 gpu인지
t = torch.tensor([[1,2],[3,4]], dtype=float64)
print("shape of tensor {}".format(t.shape))  #torch.Size([2, 2])
print("datatype of tensor {}".format(t.dtype)) #torch.float64
print("device of tensor is stored on {}".format(t.device)) #cpu

tensor 연산

덧셈, 뺄셈

a = torch.tensor([3,2])
b = torch.tensor([5,3])

sum = a + b
print("sum : {}".format(sum)) #sum : tensor([8, 5])

sub = a - b
print("sub : {}".format(sub)) #sub : tensor([-2, -1])

#원소 더하기
sum_element_a = a.sum()
print(sum_element_a) #tensor(5)

곱셈

  • matmul : 행렬 곱
  • mul : 같은 위치의 원소끼리의 곱
a = torch.arange(0,9).view(3, 3)
b = torch.arange(0,9).view(3, 3)

mat_mul = torch.matmul(a, b)
print(mat_mul) #tensor([[ 15,  18,  21],
               #        [ 42,  54,  66],
               #        [ 69,  90, 111]])

#elementwise multiplication
ele_mul = torch.mul(a, b)
print(ele_mul) #tensor([[ 0,  1,  4],
               #        [ 9, 16, 25],
               #        [36, 49, 64]])


tensor slicing

a = torch.arange(1, 13).view(4, 3)

print(a[:, 0]) # tensor([ 1,  4,  7, 10])
print(a[0,:]) # tensor([1, 2, 3])

tensor 합치기

concat

  • 기존의 shape을 유지하면서 합치기
a = torch.arange(1, 10).view(3,3)
b = torch.arange(10, 19).view(3,3)
c = torch.arange(19, 28).view(3,3)

abc = torch.cat([a,b,c], dim=0)
print("concat : \n {}".format(abc)) # tensor([[ 1,  2,  3],
                                    #         [ 4,  5,  6],
                                    #         [ 7,  8,  9],
                                    #         [10, 11, 12],
                                    #         [13, 14, 15],
                                    #         [16, 17, 18],
                                    #         [19, 20, 21],
                                    #         [22, 23, 24],
                                    #         [25, 26, 27]])
print("shape: {}".format(abc.shape)) #torch.Size([9, 3])

abc = torch.cat([a,b,c], dim=1)
print("concat : \n {}".format(abc)) # tensor([[ 1,  2,  3, 10, 11, 12, 19, 20, 21],
                                    #         [ 4,  5,  6, 13, 14, 15, 22, 23, 24],
                                    #         [ 7,  8,  9, 16, 17, 18, 25, 26, 27]])
print("shape: {}".format(abc.shape)) #torch.Size([3, 9])

stack

  • 2D를 3D로 합친다
a = torch.arange(1, 10).view(3,3)
b = torch.arange(10, 19).view(3,3)
c = torch.arange(19, 28).view(3,3)

abc = torch.stack([a,b,c], dim=0)
print("stack : \n {}".format(abc))
# tensor([[[ 1,  2,  3],
#         [ 4,  5,  6],
#         [ 7,  8,  9]],
#
#        [[10, 11, 12],
#         [13, 14, 15],
#         [16, 17, 18]],
#
#        [[19, 20, 21],
#         [22, 23, 24],
#         [25, 26, 27]]])
print("shape: {}".format(abc.shape)) #torch.Size([3, 3, 3])

tensor reshape

  • view를 사용하여 원하는 shape으로 변경
a = torch.tensor([2,4,5,6,7,8])
b = a.view(2, 3) #tensor([[2, 4, 5],
                 #        [6, 7, 8]])

tensor transpose

t

#transpose
bt = b.t() #tensor([[2, 6],
           #        [4, 7],
           #        [5, 8]])

transpose

  • torch.transpose(input, dim0, dim1)
a = torch.arange(1, 10).view(3,3)
at = torch.transpose(a,0,1)
# tensor([[1, 4, 7],
#        [2, 5, 8],
#        [3, 6, 9]])

permute

  • torch.permute(input, dims)
  • 모든 dimension을 index 순서대로 변환
b = torch.arange(1, 25).view(4, 3, 2)
bp = b.permute(2, 0, 1)  # (2, 4, 3)으로

print("permute b : \n {}".format(bt))
# tensor([[[ 1,  7, 13, 19],
#         [ 3,  9, 15, 21],
#         [ 5, 11, 17, 23]],
#
#        [[ 2,  8, 14, 20],
#         [ 4, 10, 16, 22],
#         [ 6, 12, 18, 24]]])
print(bp.shape) #torch.Size([2, 4, 3])

0개의 댓글