speechbrain.dataio.batch 模块

批量整理

Authors
  • 阿库·柔赫 2020

摘要

类:

BatchsizeGuesser

尝试确定批量大小,但从不报错

PaddedBatch

当示例是字典并且具有可变长度序列时的Collate_fn。

PaddedData

参考

class speechbrain.dataio.batch.PaddedData(data, lengths)

基础:tuple

data

字段编号 0 的别名

lengths

字段编号1的别名

class speechbrain.dataio.batch.PaddedBatch(examples, padded_keys=None, device_prep_keys=None, padding_func=<function batch_pad_right>, padding_kwargs={}, apply_default_convert=True, nonpadded_stack=True)[source]

基础类:object

当示例是字典并且具有可变长度序列时的Collate_fn。

示例中的不同元素通过键进行匹配。 所有的numpy张量都会被转换为Torch(PyTorch的default_convert)。 然后,默认情况下,所有torch.Tensor类型的元素都会被填充,并支持 collective pin_memory()和to()调用。 常规的Python数据类型只是被收集在一个列表中。

Parameters:
  • 示例 (列表) – 示例字典的列表,由Dataloader生成。

  • padded_keys (list, None) – (可选) 需要填充的键列表。如果为None,则填充所有torch.Tensors

  • device_prep_keys (list, None) – (可选) 只有这些键参与集体内存固定和移动到 to()。 如果为None,默认值为所有具有torch.Tensor值的项。

  • padding_func (callable, optional) – 使用需要一起填充的张量列表调用。需要返回两个张量:填充后的数据和另一个用于数据长度的张量。

  • padding_kwargs (dict) – (可选)传递给 padding_func 的额外参数。例如:mode, value

  • apply_default_convert (bool) – 是否在所有数据上应用PyTorch的default_convert(递归地将numpy转换为torch等)。默认值:True,通常会自动处理正确的事情。

  • nonpadded_stack (bool) – 是否对未填充的值应用类似PyTorch-default_collate的堆叠。如果可以堆叠,则进行堆叠,但如果不能堆叠,则不会报错。默认值:True,通常能正确处理。

Example

>>> batch = PaddedBatch([
...     {"id": "ex1", "foo": torch.Tensor([1.])},
...     {"id": "ex2", "foo": torch.Tensor([2., 1.])}])
>>> # Attribute or key-based access:
>>> batch.id
['ex1', 'ex2']
>>> batch["id"]
['ex1', 'ex2']
>>> # torch.Tensors get padded
>>> type(batch.foo)
<class 'speechbrain.dataio.batch.PaddedData'>
>>> batch.foo.data
tensor([[1., 0.],
        [2., 1.]])
>>> batch.foo.lengths
tensor([0.5000, 1.0000])
>>> # Batch supports collective operations:
>>> _ = batch.to(dtype=torch.half)
>>> batch.foo.data
tensor([[1., 0.],
        [2., 1.]], dtype=torch.float16)
>>> batch.foo.lengths
tensor([0.5000, 1.0000], dtype=torch.float16)
>>> # Numpy tensors get converted to torch and padded as well:
>>> import numpy as np
>>> batch = PaddedBatch([
...     {"wav": np.asarray([1,2,3,4])},
...     {"wav": np.asarray([1,2,3])}])
>>> batch.wav  # +ELLIPSIS
PaddedData(data=tensor([[1, 2,...
>>> # Basic stacking collation deals with non padded data:
>>> batch = PaddedBatch([
...     {"spk_id": torch.tensor([1]), "wav": torch.tensor([.1,.0,.3])},
...     {"spk_id": torch.tensor([2]), "wav": torch.tensor([.2,.3,-.1])}],
...     padded_keys=["wav"])
>>> batch.spk_id
tensor([[1],
        [2]])
>>> # And some data is left alone:
>>> batch = PaddedBatch([
...     {"text": ["Hello"]},
...     {"text": ["How", "are", "you?"]}])
>>> batch.text
[['Hello'], ['How', 'are', 'you?']]
__iter__()[source]

遍历批次中的不同元素。

Return type:

遍历批次。

Example

>>> batch = PaddedBatch([
...     {"id": "ex1", "val": torch.Tensor([1.])},
...     {"id": "ex2", "val": torch.Tensor([2., 1.])}])
>>> ids, vals = batch
>>> ids
['ex1', 'ex2']
pin_memory()[source]

原地操作,将相关元素移动到固定内存中。

to(*args, **kwargs)[source]

原地移动/转换相关元素。

将所有参数传递给torch.Tensor.to,请参阅其文档。

at_position(pos)[source]

获取位置。

property batchsize

返回批次大小

class speechbrain.dataio.batch.BatchsizeGuesser[source]

基础类:object

尝试找出批量大小,但不要出错

如果无法确定其他任何内容,将回退到猜测1

Example

>>> guesser = BatchsizeGuesser()
>>> # Works with simple tensors:
>>> guesser(torch.randn((2,3)))
2
>>> # Works with sequences of tensors:
>>> guesser((torch.randn((2,3)), torch.randint(high=5, size=(2,))))
2
>>> # Works with PaddedBatch:
>>> guesser(PaddedBatch([{"wav": [1.,2.,3.]}, {"wav": [4.,5.,6.]}]))
2
>>> guesser("Even weird non-batches have a fallback")
1
find_suitable_method(batch)[source]

尝试不同的方法并注意哪些方法有效

attr_based(batch)[source]

实现attr_based。

torch_tensor_bs(batch)[source]

torch_tensor_bs 的实现。

len_of_first(batch)[source]

len_of_first 的实现。

len_of_iter_first(batch)[source]

实现 len_of_iter_first。

fallback(batch)[source]

实现回退。