How can one image carry an explicit batch dimension?

expand_dims inserts a length-one axis at a chosen position, adding an indexing level to existing data.

numpy.expand_dims in practice

Add a leading axis to a 2×3 grayscale image and a model can retrieve it as batch item zero. The extra axis creates no additional training sample.

import numpy as np
np.expand_dims(np.zeros((2,3)), 0).shape

Result [1, 2, 3]

中文

单张图像怎样保留长度为一的批次轴?

expand_dims 在指定位置插入长度为一的轴,给已有数据增加新的索引层级。

numpy.expand_dims 算例

给一张 2×3 的灰度图加上最前面的批次轴,模型便可用批次下标 0 取图。这个步骤没有生成第二个训练样本。

import numpy as np
np.expand_dims(np.zeros((2,3)), 0).shape

结果 [1, 2, 3]

References / 参考来源

Keep reading / 继续阅读