mindspore.dataset.dataloader.default_collate

查看源文件
mindspore.dataset.dataloader.default_collate(batch)[源代码]

当在 DataLoader 中启用批处理时,默认使用此函数将批数据沿第一个维度进行拼接。

此函数使用预定义的从数据类型到整理函数的映射来执行以下类型转换,然后根据 collate() 中描述的规则整理输入批数据:

参数:
  • batch (list) - 要整理的批数据。

返回:

Any ,整理后的数据。

样例:

>>> from mindspore.dataset.dataloader import default_collate
>>>
>>> default_collate([0, 1, 2])
Tensor(shape=[3], dtype=Int64, value= [0, 1, 2])
>>>
>>> default_collate([{"data": 0, "label": 2},
...                  {"data": 1, "label": 3}])
{'data': Tensor(shape=[2], dtype=Int64, value= [0, 1]), 'label': Tensor(shape=[2], dtype=Int64, value= [2, 3])}
>>>
>>> default_collate([(0, 3), (1, 4), (2, 5)])
[Tensor(shape=[3], dtype=Int64, value= [0, 1, 2]), Tensor(shape=[3], dtype=Int64, value= [3, 4, 5])]