What happens to an incomplete final batch?
batched consumes up to n items at a time and yields a shorter tuple when the input ends mid-batch.
Python example and interpretation
Five images sent in pairs leave one image for the last request. Decide whether to pad it or handle it separately if the model requires a fixed shape.
from itertools import batched
list(batched(range(5),2))Result [(0, 1), (2, 3), (4,)]
中文
样本流的最后一个小批次会被丢弃吗?
batched 按需从迭代器取最多 n 个元素组成元组,输入耗尽时仍交出剩余元素。
Python 算例与适用条件
五张图片每次送两张,最后一次只有一张。送入要求固定形状的模型前,要自己决定补齐还是单独处理。
from itertools import batched
list(batched(range(5),2))结果 [(0, 1), (2, 3), (4,)]