2 분 소요

차원 별 tensor

  • 0-D tensor

0-D tensor = scalar 이며

code: torch.tensor(30)

  • 1-D tensor

1-D tensor = vector 이며

code: torch.tensor([56,24,18,6])

  • 2-D tensor

2-D tensor = matrix 이며

code: ` torch.tensor([1,2,3,4],[2,4,5,6],[1,5,6,2])`

  • 3-D tensor

code: ` torch.tensor([[[255, 0, 0], [0, 255, 0]], [[0, 255, 255], [255, 0, 255]]])`

  • N-D tensor

(N-1) D tensor들이 여러개 쌓여서 만들어진 배열 구조


pytorch 데이터 타입

  • pytorch의 데이터 타입 : 정수형, 실수형

정수형

8비트 부호 없는 정수 => 부호가 없으므로 0~255 표현 가능!

code: dtype = torch.uint8

8비트 부호 있는 정수 => 부호 있으므로 -128~127까지 표현 가능(-128부터 -1, 0부터 127로 각각 128개)

code: dtype = torch.int8

16비트 부호 있는 정수

code: dtype = torch.int16 or torch.short

32비트 부호 있는 정수

code: dtype = torch.int32 or torch.int

64비트 부호 있는 정수

code: dtype = torch.int64 or torch.long


실수형

고정 소수점 방식 => 소수부 각자리마다 따로 저장해야 해서 메모리 감당하기 힘듬(각 자리마다 0부터 9 즉 4비트 씩 필요)

부동 소수점 방식 => 가수부와 지수부로 나뉨

ex) $104.5 = 1.045 x 10^2$

32비트 부동 소수점수

code: dtype = torch.float32 or float

64비트 부동 소수점수

code: dtype = torch.float64 or double


타입 캐스팅

타입 캐스팅 => 하나의 데이터 타입을 다른 데이터 타입으로 변환시키는 것

ex. ` i = torch.tensor([1, 2, 3], dtype = torch.int16)`

32비트 부동소수점 수로 변환 => j = i.float()

64비트 부동소수점 수로 변환 => k = i.double()


Tensor 기초 함수 및 메서드

기초 함수

i 텐서 기준

  • min 함수 => tensor의 모든 값들 중 최솟값 반환 torch.min(i)
  • max 함수 => 최댓값 반환 torch.max(i)
  • sum => 합 torch.sum(i)
  • prod => 곱 torch.prod(i)
  • mean => 평균 torch.mean(i)
  • var => 표본분산 torch.var(i)
  • std => 표본표준편차 torch.std(i)

특성 확인 메소드
  • dim => tensor의 차원 확인

ex) j 텐서 기준 code : j.dim()

  • size => tensor의 크기(모양) 확인

ex) j.size() or j.shape

  • numel => tensor의 요소 총개수 확인

ex) j.numel()


Tensor의 생성

a = torch.zeros(3) : 길이가 3인 0으로 초기화 된 1-D tensor a 생성

=> 그 외 차원도 생성가능

b = torch.ones(3) : 길이가 3인 1로 초기화 된 1-D tensor b 생성

torch.zeros_like(b): b와 크기와 자료형이 같은 0으로 초기화 된 tensor로 변환하기

torch.ones_like(b): b와 크기와 자료형이 같은 1로 초기화 된 tensor로 변환하기

torch.rand(): 연속균등분포에서 추출한 난수로 채워진 특정크기( 괄호안의 크기 )의 Tensor를생성

이때 연속균등분포 => 특정 두경계값 사이의 모든 값에 대해 일정한 확률을 가지는 확률분포

torch.randn(): 표준정규분포에서 추출한 난수로 채워진 ()안의 크기의 Tensor 생성

이때 표준정규분포 => 평균 = 0, 분산 = 1, 표준편차 = 1

torch.rand_like() : 크기, 자료형이 같은 연속균등분포난수 Tensor로 변환

torch.randn_like(): 크기, 자료형이 같은 표준정규분포난수 Tensor로 변환

torch.arange(): 지정 범위 내 일정 간격의 값을 가진 tensor 생성

ex) torch.arange(1,6,2) => torch.tensor([1,3,5])

초기화 되지 않은 Tensor 사용 => 성능 향상, 메모리 사용 최적화

a = torch.empty()

a.fill_() : 초기화 되지 않은 텐서에 다른 데이터로 수정하는 표현 (메모리 주소 변경 x)

torch.tensor(list) : 리스트를 tensor로 생성하는 표현

Numpy는 대규모 수치 데이터 연산 또는 조작에 있어 적합 (리스트는 그렇지 못함!)

torch.from_Numpy(): Numpy를 tensor로 생성하는 표현

torch.from_Numpy().float(): Numpy로 생성한 tensor는 기본적으로 정수형이므로 실수형으로 타입 캐스팅 필요!

torch.IntTensor : 정수형 CPU tensor 생성

torch.FloatTensor 실수형 CPU tensor 생성

a = torch.tensor([1,2]) 일 때 b = a.clone() : tensor 복제

c = a.detach(): tensor 복제, 계산 그래프에서 분리

a.device: 현재 어떤 디바이스에 있는지 확인

torch.cuda.is_available(): cuda 사용가능한지 확인

torch.cuda.get_device_name(device=0) device 이름 확인

torch.tensor.to('cuda') or torch.tensor.cuda(): tensor를 GPU에 할당

torch.tensor.to('cpu') or torch.tensor.cpu(): tensor를 CPU에 할당

댓글남기기