pytorch
Verwenden Sie 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
Es gibt eine Möglichkeit, "Umformen", "Neue Achse", "Erweitern_Dims" zu verwenden.
Wenn Sie "Umformen" oder "Neue Achse" verwenden, können Sie mehrere gleichzeitig erhöhen.
Reshape
ist nervig, also vielleicht newaxis
~.
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