pytorch Utilisez ʻunsqueeze () `.
a = torch.rand((3, 3))
a.size() # -> [3, 3]
a = a.unsqueeze(0)
a.size() # -> [1, 3, 3]
a = a.unsqueeze(1)
a.size() # -> [3, 1, 3]
numpy
Il existe un moyen d'utiliser reshape
, newaxis
, ʻexpand_dims. Si vous utilisez
reshape ou
newaxis`, vous pouvez en augmenter plusieurs en même temps.
«Remodeler» est ennuyeux, donc je me demande si c'est un «nouvel axe».
a = np.random.normal(size=(3,3))
a.shape # -> [3, 3]
# reshape
b = a.reshape(1, 3, 3)
b.shape # -> [1, 3, 3]
c = a.reshape(3, 1, 3)
c.shape # -> [3, 1, 3]
d = a.reshape(1, *a.shape)
d.shape # -> [1, 3, 3]
# newaxis
b = a[np.newaxis]
b.shape # -> [1, 3, 3]
c = a[:, np.newaxis]
c.shape # -> [3, 1, 3]
# expand_dims
b = np.expand_dims(a, 0)
b.shape # -> [1, 3, 3]
c = np.expand_dims(a, 1)
c.shape # -> [3, 1, 3]
Recommended Posts