torch.nn.functional.pad

졜창우·2023λ…„ 1μ›” 20일
0

PyTorch

λͺ©λ‘ 보기
6/6
post-thumbnail

πŸ“• torch.nn.functional.pad

νŒ¨λ”© κΈ°λŠ₯을 ν•˜λŠ” ν•¨μˆ˜

πŸ“– 양식

  • torch.nn.functional.pad(input, pad, mode='constant', value=None)
  • 리턴값 : Tensor
  • Input : μž…λ ₯ λ°°μ—΄
  • pad : νŒ¨λ”©ν•  개수
    - constant μΌλ•Œ μˆœμ„œλŒ€λ‘œ (쒌, 우, μœ„, μ•„λž˜) 이닀.
  • mode
    - constant : κΈ°λ³Έ
    - reflect : μ›λž˜κ°’μ΄ λŒ€μΉ­μ΄λ™ν•˜μ—¬ νŒ¨λ”©λ¨
    - replicate : 제일 끝에가 μ­‰ λ³΅μ‚¬λ˜μ–΄ νŒ¨λ”©λ¨
    - circular : μˆœμ„œλŒ€λ‘œ μˆœν™˜ν•˜λ©΄μ„œ νŒ¨λ”©λ¨
  • value : νŒ¨λ”©μ— 넣을 κ°’

μ˜ˆμ‹œλ‘œ μ΄ν•΄ν•΄λ³΄μž.

# 일단 κΈ°λ³Έ 데이터생성
import torch
import torch.nn.functional as F
temp = torch.randn(3,3)
print(temp)
> tensor([[-0.8379, -0.1674,  0.2919],
          [-0.7246,  0.3931,  0.1963],
          [ 0.6709,  0.2197, -0.6927]])

πŸ“– constant

out = F.pad(temp,(1,1,1,1),'constant',10)
print(out)
> tensor([[10.0000, 10.0000, 10.0000, 10.0000, 10.0000],
          [10.0000, -0.8379, -0.1674,  0.2919, 10.0000],
          [10.0000, -0.7246,  0.3931,  0.1963, 10.0000],
          [10.0000,  0.6709,  0.2197, -0.6927, 10.0000],
          [10.0000, 10.0000, 10.0000, 10.0000, 10.0000]])

πŸ“– reflect

out = F.pad(temp,(2,2),'reflect',0)
print(out)
> tensor([[ 0.2919, -0.1674, -0.8379, -0.1674,  0.2919, -0.1674, -0.8379],
          [ 0.1963,  0.3931, -0.7246,  0.3931,  0.1963,  0.3931, -0.7246],
          [-0.6927,  0.2197,  0.6709,  0.2197, -0.6927,  0.2197,  0.6709]])

πŸ“– replicate

out = F.pad(temp,(2,2),'reflicate',0)
print(out)
> tensor([[-0.8379, -0.8379, -0.8379, -0.1674,  0.2919,  0.2919,  0.2919],
          [-0.7246, -0.7246, -0.7246,  0.3931,  0.1963,  0.1963,  0.1963],
          [ 0.6709,  0.6709,  0.6709,  0.2197, -0.6927, -0.6927, -0.6927]])

πŸ“– circular

out = F.pad(temp,(1,1,1,1),'circular',10)
print(out)
> tensor([[10.0000, 10.0000, 10.0000, 10.0000, 10.0000],
          [10.0000, -0.8379, -0.1674,  0.2919, 10.0000],
          [10.0000, -0.7246,  0.3931,  0.1963, 10.0000],
          [10.0000,  0.6709,  0.2197, -0.6927, 10.0000],
          [10.0000, 10.0000, 10.0000, 10.0000, 10.0000]])

πŸ“š μ°Έκ³ 

https://pytorch.org/docs/stable/generated/torch.nn.functional.pad.html

profile
유λŠ₯ν•œ κ°œλ°œμžκ°€ 되고 싢은 헬린이

0개의 λŒ“κΈ€