How can you remove a channel while retaining a one-image batch?
squeeze can remove selected singleton axes. An explicit axis specifies exactly which dimension the caller expects to disappear.
numpy.squeeze in practice
Shape (1,4,1) has singleton batch and channel axes. Removing only the last gives (1,4). Omitting the axis would also remove the batch dimension.
import numpy as np
np.squeeze(np.zeros((1,4,1)), axis=-1).shapeResult [1, 4]
中文
移除单通道轴时怎样保住单样本批次?
squeeze 可以有选择地移除长度为一的轴。指定 axis 能表达调用方期待哪个维度可以被删去。
numpy.squeeze 算例
形状 (1,4,1) 同时含批次和通道两个单元素轴。仅去掉末轴留下 (1,4),而无参数调用会把批次也去掉。
import numpy as np
np.squeeze(np.zeros((1,4,1)), axis=-1).shape结果 [1, 4]