[Pytorch]#0 쉽게 잊는 것들 링크로 모아보기

Clay Ryu's sound lab·2022년 3월 27일
0

Framework

목록 보기
4/48

torch.view vs torch.reshape

편하게 tensor의 모양을 바꿔주고 싶다면 torch.reshape를 사용한다. 메모리 사용량도 염려하고 두 텐서가 동일한 데이터를 공유하도록 하려면 torch.view를 사용한다.

https://sdr1982.tistory.com/317

tensor에 사용되는 method들

Tensor의 자료형
Tensor관련 함수와 메서드들

https://subinium.github.io/pytorch-Tensor-Variable/

torch.cat vs torch.stack (feat.np.concatenate)

이 둘은 합치는 기능과 더불어 tensor 리스트를 한번에 tensor로 만들 수 있다.
참고로 torch.cat과 np.concatenate는 같은 역할로 사용이 가능하다.

(1, 3252, 527)에 axis1에 1개를 앞쪽으로 붙여주고 싶다면
np.concatenate((1,1,527), 원본, axis=1)로 붙이면 된다.

https://sanghyu.tistory.com/85

dataloader, collate_fn

iter는 dataloader를 iterable한 객체로 바꿔주고 next는 순서대로 내용물을 꺼내주는 역할을 한다.

iris_loader = DataLoader(iris, batch_size=105, shuffle=True)

next(iter(iris_loader))

batch 값이 아래와 같이 tensor와 label의 조합이라면 이들을 따로 합쳐줄 기능이 collate function이다.

(tensor([-0.0533, -0.0542, -0.0393,  ...,  0.1852,  0.1835,  0.1619]), 3)

def collate_func(batch):
  audio = torch.stack([x[0]for x in batch])
  label = torch.LongTensor([x[1] for x in batch])
  return audio, label
  
  dataloader = DataLoader(dataset=audioset, batch_size=64, shuffle=True, collate_fn=collate_func)

torch.max

https://pytorch.org/docs/stable/generated/torch.max.html

지정한 dimension끼리 비교를 해서 가장 큰 숫자와 그 index를 남긴다. 아웃풋은 2개이므로 슬라이스를 해서 하나를 쓰면 된다.

torch.Size([64, 48000])짜리 데이터를 48 mel frequency spectrogram으로 만들고 transpose를 해주면 torch.Size([64, 12, 48])이 된다. 이를 48차원으로 10개의 클래스 분류를 위한 모델에 넣어주면 torch.Size([64, 12, 10])이 된다. 이를 최대값을 뽑아내는 차원 축소를 통해서 (64, 10)의 텐서로 바꿔주는 일을 하는 것이 torch.max이다.

out = torch.max(out_all, dim=1)[0]

torch.set_printoptions(sci_mode=False)

e가 포함된 숫자를 소수점 4째자리까지 사용

https://jungnamgyu.tistory.com/53

torch.tensor torch.longtensor

보통, 계산을 하기 위한 데이터들에는 FloatTensor를 사용하고 Int형 숫자를 사용할 때는 LongTensor 사용하며, True/False 사용 시, ByteTensor를 사용합니다.

https://dk-kang.tistory.com/entry/PyTorch-%EC%8B%A4%EC%8A%B5-Tensor-%EC%A1%B0%EC%9E%91%ED%95%98%EA%B8%B01

torch.repeat vs. torch.expand

repeat은 x.repeat(4,2,2)이면 dim=0에서 4번 dim=1에서 2번 dim=2에서 2번 반복해서 새로운 텐서를 만들어낸다. 만들어지는 것은 가장 마지막 차원에서부터 채워나가면 크게 어렵지 않다.

https://seducinghyeok.tistory.com/9

torch index slicing

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

profile
chords & code // harmony with structure

0개의 댓글