numpyとtorchのdimension拡大と縮小

###背景###
numpyとpytorchには次元拡張と削減する方法は機械学習には頻繁に使われてます。今回は軽くそれを説明いたします。
次元拡張
np.expand_dims
torch.unsqueeze

次元縮小
np.squeeze
torch.squeeze

###1.numpy###

####1-1.np.expand_dims####
次元拡張

![numpyとtorchのdimension追加.001.jpeg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/191439/7b589482-3cd2-7ce4-8028-36ca63c6209e.jpeg)

![numpyとtorchのdimension追加.001.jpeg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/191439/e63fb9e7-1448-8982-c1ac-d33024d60c9f.jpeg)

![numpyとtorchのdimension追加.002.jpeg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/191439/6a04ac3f-624c-982f-4a7d-a5714205553f.jpeg)

![numpyとtorchのdimension追加.003.jpeg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/191439/042c451f-fa05-82c7-51ae-b86f69b5e722.jpeg)

####テストコード####
```python
a = np.array([
    [1, 3, 5],
    [1, "A", 3],
    [5, 1, "F"],
    [4, "G", "S"],
    ["S", "G", 2]
])


print(a)
print("np.expand_dims(a, 0)")
b = np.expand_dims(a, 0)
print(b)


print("np.expand_dims(a, 1)")
b = np.expand_dims(a, 1)
print(b)


print("np.expand_dims(a, 2)")
b = np.expand_dims(a, 2)
print(b)
```

####output####
```sh
[['1' '3' '5']
 ['1' 'A' '3']
 ['5' '1' 'F']
 ['4' 'G' 'S']
 ['S' 'G' '2']]

np.expand_dims(a, 0)
[[['1' '3' '5']
  ['1' 'A' '3']
  ['5' '1' 'F']
  ['4' 'G' 'S']
  ['S' 'G' '2']]]

np.expand_dims(a, 1)
[[['1' '3' '5']]

 [['1' 'A' '3']]

 [['5' '1' 'F']]

 [['4' 'G' 'S']]

 [['S' 'G' '2']]]

np.expand_dims(a, 2)
[[['1']
  ['3']
  ['5']]

 [['1']
  ['A']
  ['3']]

 [['5']
  ['1']
  ['F']]

 [['4']
  ['G']
  ['S']]

 [['S']
  ['G']
  ['2']]]
```

####1-2.np.squeeze####
次元縮小
![numpyとtorchのdimension追加.006.jpeg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/191439/7b87546d-e674-b416-7ab0-5f9b2f084ed3.jpeg)

![numpyとtorchのdimension追加.007.jpeg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/191439/8e4a2233-f51b-5e88-17c9-4b6679456fd8.jpeg)

![numpyとtorchのdimension追加.008.jpeg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/191439/6f9cd0f9-4466-f99b-3a9e-53ff5625e6e8.jpeg)

![numpyとtorchのdimension追加.009.jpeg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/191439/8944b60b-0aee-9f36-f79b-8f579dd09f07.jpeg)

####テストコード####
```python

a = np.array([
    [1, 3, 5],
    [1, "A", 3],
    [5, 1, "F"],
    [4, "G", "S"],
    ["S", "G", 2]
])

print("a")
print(a)
print()
print("np.expand_dims(a,2)")
b = np.expand_dims(a,2)
print(b)

print()
print("c = np.squeeze(b,2)")
c = np.squeeze(b,2)
print(c)

print()
print("d = np.expand_dims(b,0)")
d = np.expand_dims(b,0)
print(d)

print()
print("e = np.squeeze(d,3)")
e = np.squeeze(d,3)
print(e)

```

####output####
```sh
a
[['1' '3' '5']
 ['1' 'A' '3']
 ['5' '1' 'F']
 ['4' 'G' 'S']
 ['S' 'G' '2']]

np.expand_dims(a,2)
[[['1']
  ['3']
  ['5']]

 [['1']
  ['A']
  ['3']]

 [['5']
  ['1']
  ['F']]

 [['4']
  ['G']
  ['S']]

 [['S']
  ['G']
  ['2']]]

c = np.squeeze(b,2)
[['1' '3' '5']
 ['1' 'A' '3']
 ['5' '1' 'F']
 ['4' 'G' 'S']
 ['S' 'G' '2']]

d = np.expand_dims(b,0)
[[[['1']
   ['3']
   ['5']]

  [['1']
   ['A']
   ['3']]

  [['5']
   ['1']
   ['F']]

  [['4']
   ['G']
   ['S']]

  [['S']
   ['G']
   ['2']]]]

e = np.squeeze(d,3)
[[['1' '3' '5']
  ['1' 'A' '3']
  ['5' '1' 'F']
  ['4' 'G' 'S']
  ['S' 'G' '2']]]

```

###2.torch###

####2-1.torch.unsqueeze####
次元拡大
![numpyとtorchのdimension追加.010.jpeg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/191439/ad83702f-dc76-390f-f1c4-ebc84fe1265a.jpeg)

![numpyとtorchのdimension追加.011.jpeg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/191439/bfad0964-d3ac-d08c-22ab-7539516982ab.jpeg)

![numpyとtorchのdimension追加.012.jpeg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/191439/58e4a88e-81a6-192d-32ba-7cb87d51965a.jpeg)

![numpyとtorchのdimension追加.013.jpeg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/191439/562eb34b-8d70-422a-9d07-12f53a7127f6.jpeg)

![numpyとtorchのdimension追加.014.jpeg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/191439/921de0a7-58c5-8eed-4a53-8cf3a62b5e2e.jpeg)


####テストコード####
```python
import torch
a = torch.tensor([
    [1, 3, 5],
    [1, 2, 3],
    [5, 1, 3],
    [4, 2, 7],
    [98, 11, 2]
])

c0 = torch.unsqueeze(a, 0)
print(c0)

c1 = torch.unsqueeze(a, 1)
print(c1)

c2 = torch.unsqueeze(a, 2)
print(c2)
```

####output####
```shell
tensor([[[ 1,  3,  5],
         [ 1,  2,  3],
         [ 5,  1,  3],
         [ 4,  2,  7],
         [98, 11,  2]]])
tensor([[[ 1,  3,  5]],

        [[ 1,  2,  3]],

        [[ 5,  1,  3]],

        [[ 4,  2,  7]],

        [[98, 11,  2]]])
tensor([[[ 1],
         [ 3],
         [ 5]],

        [[ 1],
         [ 2],
         [ 3]],

        [[ 5],
         [ 1],
         [ 3]],

        [[ 4],
         [ 2],
         [ 7]],

        [[98],
         [11],
         [ 2]]])
```

####2-2.torch.squeeze####
次元縮小
![numpyとtorchのdimension追加.015.jpeg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/191439/dfed9f87-159f-775f-3618-b2b86024060d.jpeg)

![numpyとtorchのdimension追加.016.jpeg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/191439/28201a1f-7594-40d0-59fb-5e1bee667996.jpeg)

![numpyとtorchのdimension追加.017.jpeg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/191439/bc997ee8-8a4c-8187-8ee6-05695d9d0f74.jpeg)

![numpyとtorchのdimension追加.018.jpeg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/191439/a73b1b96-0f60-ae1f-e5c2-62e4c71537ba.jpeg)

![numpyとtorchのdimension追加.019.jpeg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/191439/e2be1d45-7fd6-099b-1cda-1e3a6e90a962.jpeg)

####テストコード####
```python

import torch
a = torch.tensor([
    [[1], [3], [5]],
    [[1], [2], [3]],
    [[5], [1], [3]],
    [[4], [2], [7]],
    [[98], [11], [2]]
])

print("torch.squeeze(a, 0)")
c0 = torch.squeeze(a, 0)
print(c0)

print()
print("torch.squeeze(a, 1)")
c1 = torch.squeeze(a, 1)
print(c1)

print()
print("torch.squeeze(a, 2)")
c2 = torch.squeeze(a, 2)
print(c2)
```

####output####
```sh
torch.squeeze(a, 0)
tensor([[[ 1],
         [ 3],
         [ 5]],

        [[ 1],
         [ 2],
         [ 3]],

        [[ 5],
         [ 1],
         [ 3]],

        [[ 4],
         [ 2],
         [ 7]],

        [[98],
         [11],
         [ 2]]])

torch.squeeze(a, 1)
tensor([[[ 1],
         [ 3],
         [ 5]],

        [[ 1],
         [ 2],
         [ 3]],

        [[ 5],
         [ 1],
         [ 3]],

        [[ 4],
         [ 2],
         [ 7]],

        [[98],
         [11],
         [ 2]]])

torch.squeeze(a, 2)
tensor([[ 1,  3,  5],
        [ 1,  2,  3],
        [ 5,  1,  3],
        [ 4,  2,  7],
        [98, 11,  2]])
```


###まとめ###
次元拡張
np.expand_dims -> torch.unsqueeze

次元縮小
np.squeeze -> torch.squeeze