当数据量很大的时候,选择一批一批的数据进行训练,加快速度效率。
import torch
import torch.utils.data as Data #Data是进行小批量的模块
torch.manual_seed(1) # reproducible
BATCH_SIZE = 5 # 批训练的数据个数,大数量中每次抽取五个
x = torch.linspace(1, 10, 10) # x data (torch tensor) 从1-10的10个点
y = torch.linspace(10, 1, 10) # y data (torch tensor) 从10-1的10个点
# 先转换成 torch 能识别的 Dataset 把数据放入数据库
torch_dataset = Data.TensorDataset(data_tensor=x, target_tensor=y)
# loader使训练变成小批。 把 dataset 放入 DataLoader
loader = Data.DataLoader(
dataset=torch_dataset, # torch TensorDataset format
batch_size=BATCH_SIZE, # mini batch size
shuffle=True, # 要不要打乱数据 (打乱比较好)
num_workers=2, # 多线程来读数据,提取xy的时候几个数据一起提取
)
for epoch in range(3): # 把整个数据进行训练的次数,通过loader来确定是否打乱数据,打乱比较好
for step, (batch_x, batch_y) in enumerate(loader): # 每一步 loader 释放一小批数据用来学习,step=总数据量/batch_size,enumerate把每次提取编写索引。
# 假设这里就是你训练的地方...
# 打出来一些数据
print('Epoch: ', epoch, '| Step: ', step, '| batch x: ',
batch_x.numpy(), '| batch y: ', batch_y.numpy())
显示循环中选择的batch:
1.数据打乱结果:
"""
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.]
"""
2.数据不打乱的结果
每次都是一样的
"""
Epoch: 0 | Step: 0 | batch x: [ 1. 2. 3. 4. 5.] | batch y: [ 10. 9. 8. 7. 6.]
Epoch: 0 | Step: 1 | batch x: [ 6. 7. 8. 9. 10.] | batch y: [ 5. 4. 3. 2. 1.]
Epoch: 1 | Step: 0 | batch x: [ 1. 2. 3. 4. 5.]| batch y: [ 10. 9. 8. 7. 6.]
Epoch: 1 | Step: 1 | batch x: [ 6. 7. 8. 9. 10.] | batch y: [ 5. 4. 3. 2. 1.]
Epoch: 2 | Step: 0 | batch x: [ 1. 2. 3. 4. 5.] | batch y: [ 10. 9. 8. 7. 6.]
Epoch: 2 | Step: 1 | batch x: [ 6. 7. 8. 9. 10.] | batch y: [ 5. 4. 3. 2. 1.]
"""
3.当batch_size 不能被总数据数量整除的时候
此时总数据数量为10,shuffle选择true,如果我们改变一下 BATCH_SIZE = 8, 这样我们就知道, step=0 会导出8个数据, 但是, step=1 时数据库中的数据不够 8个, 则会在这个step中返回剩余不足batch_size的数据:
"""
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.]
"""
注意,我使用的IDE是Spyder,在运行到上面 ‘’for step, (batch_x, batch_y) in enumerate(loader): ‘’这一步的时候不能出结果,不报错,不停止,仿佛卡住了。
解决方法:
①如果在终端cmd的Python里直接运行就可以。
②如果想在Spyder中运行,要在所有的import后加一句话:
if __name__ == '__main__':
剩下的代码全选按tab键自动排版一下。
这样就可以出结果啦,最后,原因了解下:如何简单地理解Python中的if __name__ == '__main__'
还有以后,在使用multiprocessing写多进程的时候也要加上这个东西,不要忘啦
参考链接:
https://morvanzhou.github.io/tutorials/machine-learning/torch/3-05-train-on-batch/