LoginSignup
0
0

#0017 numpy.reshape(-1, ...) の使い方

Posted at

はじめに

  • 最近画像処理の勉強をしており、pytorchで実装されたPadimやpatch coreを実装している。
  • その中で画像のshapeを操作することがしばしばあり、x.reshape(-1, ...)に頻繁に出会った。
  • 初見、なんのための「-1」かが分からなかったため、調べてまとめることにした。

numpy.reshape(-1, ...) の使い方

  • 結論から言えば、-1を入れると平坦化される。
  • そして、-1以降に数字xを入れると、(平坦化したlength/x, x)で返される。
  • 上手な説明ができないため、以下に例を示す。
a = np.zeros((3,4,5,6))

print(type(a))                              # <class 'numpy.ndarray'>
print(a.shape)                              # (3, 4, 5, 6)
print((a.reshape(-1).shape))                # (360,)
print((a.reshape(-1, 1).shape))             # (360, 1)
print((a.reshape(-1, 5).shape))             # (72, 5)
print((a.reshape(-1, 1, 5).shape))          # (72, 1, 5)
print((a.reshape(-1, *a.shape[2:])).shape)  # (12, 5, 6) *がないとtupleで読まれてエラーとなる

おわりに

  • pytorchの次元の操作にもっと慣れていきたい。
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0