PyTorch:数据读取2 - Dataloader
Dataloader的处理逻辑是先通过Dataset类里面的函数获取单个数据,然后组合成batch,再使用collate_fn所指定的函数对这个batch做一些操作(比如每个batch中实际lengths,padding,cuda之类的)。因为dataloader是有batch_size参数的,我们可以通过自定义来设计数据收集的方式,意思是已经通过上面的Dataset类中的函数采样了batch_s
dataloader的使用
torch.utils.data.DataLoader
中的参数:
-
dataset (Dataset) – dataset from which to load the data.
- batch_size (int, optional) – how many samples per batch to load (default: 1).
- shuffle (bool, optional) – set to
True
to have the data reshuffled at every epoch (default: False). - sampler (Sampler, optional) – defines the strategy to draw samples from the dataset. If specified,
shuffle
must be False. - batch_sampler (Sampler, optional) – like sampler, but returns a batch of indices at a time. Mutually exclusive with batch_size, shuffle, sampler, and drop_last.
- num_workers (int, optional) – how many subprocesses to use for data loading. 0 means that the data will be loaded in the main process. (default: 0)
- collate_fn (callable*, *optional) – merges a list of samples to form a mini-batch.
- pin_memory (bool, optional) – If
True
, the data loader will copy tensors into CUDA pinned memory before returning them. 如果pin_memory=True,那么Pytorch会采取一系列操作把数据拷贝到GPU,总之就是为了加速。 - drop_last (bool, optional) – set to
True
to drop the last incomplete batch, if the dataset size is not divisible by the batch size. IfFalse
and the size of dataset is not divisible by the batch size, then the last batch will be smaller. (default: False) - timeout (numeric, optional) – if positive, the timeout value for collecting a batch from workers. Should always be non-negative. (default: 0)
- worker_init_fn (callable, optional) – If not None, this will be called on each worker subprocess with the worker id (an int in
[0, num_workers - 1]
) as input, after seeding and before data loading. (default: None)
返回值
返回值是一个实现了__iter__的对象,可以使用for循环进行迭代,或者转换成迭代器取第一条batch数据查看。
for循环进行迭代时返回的每条数据就是(batch_size,*)大小的。
示例1:
self.data_loader = torch.utils.data.DataLoader(
dataset=self.dataset, collate_fn=self.collate_fn,
batch_size=batch_size, shuffle=if_shuffle, num_workers=args.num_workers)
示例2:
DataLoader和TensorDataset结合使用
import torch
from torch.utils.data import DataLoader, TensorDataset
a = torch.tensor([[1, 1, 1], [2, 2, 2], [3, 3, 3], [4, 4, 4]])
b = torch.tensor([1, 2, 3, 4])
train_data = TensorDataset(a, b)
data = DataLoader(train_data, batch_size=2, shuffle=True)
for i, j in enumerate(data):
x, y = j
print(' batch:{0} x:{1} y: {2}'.format(i, x, y))
# batch:0 x:tensor([[4, 4, 4],
# [1, 1, 1]]) y: tensor([4, 1])
# batch:1 x:tensor([[3, 3, 3],
# [2, 2, 2]]) y: tensor([3, 2])
DataLoader数据查看
batch数目
batch_num = len(train_dataloader)
获取dataset中的第一条数据
train_dataloader.dataset[0] # 完全等价于原始dataset=self.dataset的self.dataset[0]
获取dataloader中batch中的第一条数据
def get_one_data(item_dict, i):
return {k: v[i] for k, v in item_dict.items()}
print(get_one_data(next(iter(train_data_loader)), 1))
或者
for item_dict in train_data_loader:
print(get_one_data(item_dict, 1))
break
自定义dataloader
Dataloader的处理逻辑是先通过Dataset类里面的 __getitem__
函数获取单个数据,然后组合成batch,再使用collate_fn所指定的函数对这个batch做一些操作(比如每个batch中实际lengths,padding,cuda之类的)。
自定义collate_fn
data_loader = DataLoader(dataset, batch_size=batch_size, shuffle=True,
num_workers=num_worker, collate_fn=collate_fn, pin_memory=True)
因为dataloader
是有batch_size
参数的,我们可以通过自定义参数collate_fn
来设计数据收集的方式,意思是已经通过上面的Dataset
类中的__getitem__
函数采样了batch_size
数据,以一个包的形式传递给collate_fn
所指定的函数。
自动的输入
batch:是一个batch_size大小的list。其中的元素就是dataset中定义的__getitem__返回的一样。
示例
示例1:实现一个Collator类,实例化后作为参数输入到collate_fn。
class Collator(object):
def __init__(self, conf):
self.device = conf.device
def __call__(self, batch):
# print(batch)
# raise NotImplementedError
...
# id特征再处理
# label_id再处理(onehot/multihot)
batch_map = {
'label': tensor_labels,
'tokens1': torch.tensor(tokens1),
'tokens2': torch.tensor(tokens2),
'tokens1_mask': torch.tensor(tokens1).gt(0).float(),
'tokens2_mask': torch.tensor(tokens2).gt(0).float(),
'token_max_len': torch.tensor([token_max_len], dtype=torch.float32)}
return batch_map
collate_fn = Collator(conf)
示例2:通过collate_fn
进行解包
def collate_fn(data):
A,B,path,hop=zip(*data)
print('A:',A," B:",B," path:",path," hop:",hop)
raise Exception('utils collate_fun 147')
return A,B,path,hop
示例3:nlp任务中,经常在collate_fn指定的函数里面做padding,将同一个batch中不一样长的句子padding成一样长
def collate_fn(data):
src, tgt, original_src, original_tgt = zip(*data)
src_len = [len(s) for s in src]
src_pad = torch.zeros(len(src), max(src_len)).long()
for i, s in enumerate(src):
end = src_len[i]
src_pad[i, :end] = torch.LongTensor(s[end-1::-1])
tgt_len = [len(s) for s in tgt]
tgt_pad = torch.zeros(len(tgt), max(tgt_len)).long()
for i, s in enumerate(tgt):
end = tgt_len[i]
tgt_pad[i, :end] = torch.LongTensor(s)[:end]
return src_pad, tgt_pad, \
torch.LongTensor(src_len), torch.LongTensor(tgt_len), \
original_src, original_tgt
测试collate_fn
想要在collate_fn函数内部测试,需要在外面对dataloaders进行for调用,后再断点或者exit(),否则不会真正执行collate_fn,这样collate_fn函数中的print就不会执行了。
测试时,最好num_workers=0,这样print(batch)才不会输出太多。
for index, item in enumerate(data_loader):
A = item
exit()
一些问题
[为什么pytorch DataLoader在numpy数组和列表上的行为有所不同?]
1 import问题
使用torch.utils.data.DataLoader时,pycharm中无法直接点击进入代码。
2 num_workers设置过大问题
num_workers如果设置过大,资源不够,会出错:Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)
[Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)]
[https://github.com/pytorch/pytorch/issues/41794]
from: -柚子皮-
ref:
开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!
更多推荐
所有评论(0)