在训练网络的数据处理中,经常需要对数据进行维度处理。
遇到过的一些常见扩维方法:
a = np.zeros((10,5,5),dtype=np.uint8)
a1 = a[...,np.newaxis]
a2 = np.expand_dims(a, axis=3) # 或axis=-1,表示最后一个维度
a3 = np.reshape(a, (a.shape[0],a.shape[1],a.shape[2],1))
# a1.shape == a2.shape == a3.shape : (10,5,5,1)
b1 = np.array([a])
b2 = np.expand_dims(a, axis=0)
b3 = np.reshape(a, (1, a.shape[0], a.shape[1], a.shape[2]))
# b1.shape == b2.shape == b3.shape : (1,10,5,5)