3.5 – 数据读取 (Data Loader)
DataLoader 是 torch 给你用来包装你的数据的工具. 所以你要讲自己的 (numpy array 或其他) 数据形式装换成 Tensor, 然后再放进这个包装器中. 使用 DataLoader 有什么好处呢? 就是他们帮你有效地迭代数据, 举例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| import torch import torch.utils.data as Data torch.manual_seed(1)
BATCH_SIZE = 5
x = torch.linspace(1, 10, 10) y = torch.linspace(10, 1, 10)
torch_dataset = Data.TensorDataset(data_tensor=x, target_tensor=y)
loader = Data.DataLoader( dataset=torch_dataset, batch_size=BATCH_SIZE, shuffle=True, num_workers=2, )
for epoch in range(3): for step, (batch_x, batch_y) in enumerate(loader):
print(\'Epoch: \', epoch, \'| Step: \', step, \'| batch x: \', batch_x.numpy(), \'| batch y: \', batch_y.numpy())
""" Epoch: 0 | Step: 0 | batch x: [ 6\. 7\. 2\. 3\. 1.] | batch y: [ 5\. 4\. 9\. 8\. 10.] Epoch: 0 | Step: 1 | batch x: [ 9\. 10\. 4\. 8\. 5.] | batch y: [ 2\. 1\. 7\. 3\. 6.] Epoch: 1 | Step: 0 | batch x: [ 3\. 4\. 2\. 9\. 10.] | batch y: [ 8\. 7\. 9\. 2\. 1.] Epoch: 1 | Step: 1 | batch x: [ 1\. 7\. 8\. 5\. 6.] | batch y: [ 10\. 4\. 3\. 6\. 5.] Epoch: 2 | Step: 0 | batch x: [ 3\. 9\. 2\. 6\. 7.] | batch y: [ 8\. 2\. 9\. 5\. 4.] Epoch: 2 | Step: 1 | batch x: [ 10\. 4\. 8\. 1\. 5.] | batch y: [ 1\. 7\. 3\. 10\. 6.] """
|
可以看出, 每步都导出了5个数据进行学习. 然后每个 epoch 的导出数据都是先打乱了以后再导出.
真正方便的还不是这点. 如果我们改变一下 BATCH_SIZE = 8 , 这样我们就知道, step=0 会导出8个数据, 但是, step=1 时数据库中的数据不够 8个, 这时怎么办呢:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| BATCH_SIZE = 8
...
for ...: for ...: ... print(\'Epoch: \', epoch, \'| Step: \', step, \'| batch x: \', batch_x.numpy(), \'| batch y: \', batch_y.numpy()) """ Epoch: 0 | Step: 0 | batch x: [ 6\. 7\. 2\. 3\. 1\. 9\. 10\. 4.] | batch y: [ 5\. 4\. 9\. 8\. 10\. 2\. 1\. 7.] Epoch: 0 | Step: 1 | batch x: [ 8\. 5.] | batch y: [ 3\. 6.] Epoch: 1 | Step: 0 | batch x: [ 3\. 4\. 2\. 9\. 10\. 1\. 7\. 8.] | batch y: [ 8\. 7\. 9\. 2\. 1\. 10\. 4\. 3.] Epoch: 1 | Step: 1 | batch x: [ 5\. 6.] | batch y: [ 6\. 5.] Epoch: 2 | Step: 0 | batch x: [ 3\. 9\. 2\. 6\. 7\. 10\. 4\. 8.] | batch y: [ 8\. 2\. 9\. 5\. 4\. 1\. 7\. 3.] Epoch: 2 | Step: 1 | batch x: [ 1\. 5.] | batch y: [ 10\. 6.] """
|
这时, 在 step=1 就只给你返回这个 epoch 中剩下的数据就好了.
所以这也就是在我 github 代码 中的每一步的意义啦.
文章来源:莫烦