Deep Learning/framework(16)
-
[Pytorch] nn.ReflectionPad2d
input tensor를 input boundary의 reflection(반사)를 이용해서 패딩 적용 2차원 아닌 N차원 패딩을 이용하려면 torch.nn.functional.pad()를 사용 padding은 int형이나 tuple형 : 패딩 사이즈. int형이면 모든 바운더리에 같은 패딩을 적용. 4-tuple이면 (padding_left, padding_right, padding_top, padding_bottom)으로 적용 Input: (N, C, H_in, W_in) Output: (N, C, H_out, W_out) W_out = W_in + padding_left + padding_right H_out = H_in + padding_top + padding_bottom https://pyt..
2021.06.16 -
[Pytorch] Initializer (torch.nn.init)
torch.nn.init의 간단한 initializer들을 살펴보겠다 xavier같은 초기화기법들은 따로 다루기 1. torch.nn.init.normal_(tensor, mean=0.0, std=1.0) input tensor을 N(mean, std^2)의 normal distribution(정규분포)에 따라 초기화 2. torch.nn.init.uniform_(tensor, a=0.0, b=1.0) input tensor을 U(a, b)의 uniform distribution에 따라 초기화 * uniform distribution ? [a, b]의 범위에서 모든 값의 확률이 동일한 분포 3. torch.nn.init.constant_(tensor, val) input tensor를 특정 값 val로 ..
2021.06.15 -
[Pytorch] Tensor 의 .view()메소드 / .reshape()메소드
tensor의 shape를 변경해줌 view는 기존의 데이터와 같은 메모리 공간을 공유하며 stride 크기만 변경하여 보여주기만 다르게 합니다. 그래서 contigious해야만 동작하며, 아닌 경우 에러가 발생합니다. reshape은 가능하면 input의 view를 반환하고, 안되면 contiguous한 tensor로 copy하고 view를 반환합니다. view는 메모리가 기존 Tensor와 동일한 메모리를 공유하는게 보장되지만 reshape은 그렇지 않습니다. 안전하게 형태만 바꾸고 싶다 reshape 메모리가 공유되어 업데이트에 대한 보장이 이뤄진다 view (단 continguous하지 않은 경우 에러 발생 가능) >>> x = torch.randn(4, 4) >>> x.size() # torch..
2021.05.24 -
[Pytorch] torch.sort
torch.sort(input, dim=-1, descending=False, stable=False, *, out=None) -> (Tensor, LongTensor) input (Tensor) – the input tensor. dim (int, optional) – sort할 차원 descending (bool, optional) – 내림차순/올림차순 여부 stable (bool, optional) – sorting routine을 안정적으로 만들어 equivalent의 순서가 유지되도록 보장합니다. stable=True only works on the CPU for now. 일반 Python list의 sort와 달리 바뀐 인덱스를 담고 있는 텐서를 반환해주기 때문에, 아주 유용하게 쓸 수 있다
2021.05.22 -
[Pytorch] torch.no_grad
기록을 추적하는 것(과 메모리를 사용하는 것)을 방지하기 위해, 코드 블럭을 with torch.no_grad(): 로 감쌀 수 있음. https://pytorch.org/docs/stable/generated/torch.no_grad.html no_grad — PyTorch 1.8.1 documentation Shortcuts pytorch.org gradient 계산이 되지 않게 하는 Context-manager gradient calculation을 금지하는 것은 inference에서 유용 (Tensor.backward()를 호출하지 않는다는 가정하에) 이 context 내부에서 새로 생성된 tensor들은 requires_grad = False상태가 됨 메모리 사용량을 아껴줌 thread loca..
2021.05.22 -
[PyTorch] tensor 부등호 연산 (?), list와 차이점
tensor로 만들어줄 경우 import torch data = [[1, 2],[3, 4]] x_data = torch.tensor(data) pos = x_data > 0 print(pos) 결과 List일 경우 data = [[1, 2],[3, 4]] pos = data > 0 print(pos) 이 연산 자체가 불가능 for문 사용해야함 data = [[1, 2],[3, 4]] pos = [[a > 0, b > 0] for a, b in data] print(pos)
2021.05.13