[莫烦 PyTorch 系列教程] 3.5 – 数据读取 (Data Loader)

PyTorch入门实战教程
DataLoader  是 torch 给你用来包装你的数据的工具. 所以你要讲自己的 (numpy array 或其他) 数据形式装换成 Tensor, 然后再放进这个包装器中. 使用 DataLoader  有什么好处呢? 就是他们帮你有效地迭代数据, 举例:
可以看出, 每步都导出了5个数据进行学习. 然后每个 epoch 的导出数据都是先打乱了以后再导出.

真正方便的还不是这点. 如果我们改变一下 BATCH_SIZE = 8 , 这样我们就知道, step=0  会导出8个数据, 但是, step=1  时数据库中的数据不够 8个, 这时怎么办呢:

这时, 在 step=1  就只给你返回这个 epoch 中剩下的数据就好了.

所以这也就是在我 github 代码 中的每一步的意义啦.

文章来源:莫烦

PyTorch入门实战教程
除特别注明外,本站所有文章均为 PyTorch 中文网原创,转载请注明出处:https://www.pytorchtutorial.com/3-5-data-loader/

Leave a Reply

Your email address will not be published. Required fields are marked *

4条评论

  1. 想请教一个问题,就是这样训练的时候,变量需要变成 Variable 类型的,我训练处的代码是这么写的,不知道这样写是否正确:

    # 数据整体训练三次
    for epoch in range(50):
    for step,(batch_x,batch_y) in enumerate(loader):
    print(‘Epoch : ‘,epoch,’step : ‘,step)
    batch_x,batch_y = Variable(batch_x),Variable(batch_y)
    prediction = net(batch_x) #预测值
    loss = loss_func(prediction,batch_y) #计算误差,注意prediction和y的顺序
    optimizer.zero_grad() #首先把所有梯度设为0
    loss.backward() #反向传递
    optimizer.step() #优化梯度

    1. 直接复制粘贴格式好像会出现问题,里面主要是这一步,不知道是否正确:
      batch_x,batch_y = Variable(batch_x),Variable(batch_y)

  2. 请教一个问题:
    在遍历“for step, (batch_x, batch_y) in enumerate(loader): ”这一步会报这样的错误:
    raceback (most recent call last):
    File “”, line 1, in
    Traceback (most recent call last):
    File “”, line 1, in
    File “G:\ANACONDA\lib\multiprocessing\spawn.py”, line 105, in spawn_main
    File “G:\ANACONDA\lib\multiprocessing\spawn.py”, line 105, in spawn_main
    exitcode = _main(fd)
    File “G:\ANACONDA\lib\multiprocessing\spawn.py”, line 114, in _main
    exitcode = _main(fd)
    File “G:\ANACONDA\lib\multiprocessing\spawn.py”, line 114, in _main
    prepare(preparation_data)
    File “G:\ANACONDA\lib\multiprocessing\spawn.py”, line 225, in prepare
    prepare(preparation_data)
    File “G:\ANACONDA\lib\multiprocessing\spawn.py”, line 225, in prepare
    _fixup_main_from_path(data[‘init_main_from_path’])
    File “G:\ANACONDA\lib\multiprocessing\spawn.py”, line 277, in _fixup_main_from_path
    _fixup_main_from_path(data[‘init_main_from_path’])
    File “G:\ANACONDA\lib\multiprocessing\spawn.py”, line 277, in _fixup_main_from_path
    run_name=”__mp_main__”)
    File “G:\ANACONDA\lib\runpy.py”, line 263, in run_path
    run_name=”__mp_main__”)
    File “G:\ANACONDA\lib\runpy.py”, line 263, in run_path
    pkg_name=pkg_name, script_name=fname)
    File “G:\ANACONDA\lib\runpy.py”, line 96, in _run_module_code
    pkg_name=pkg_name, script_name=fname)
    File “G:\ANACONDA\lib\runpy.py”, line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
    File “G:\ANACONDA\lib\runpy.py”, line 85, in _run_code
    mod_name, mod_spec, pkg_name, script_name)
    File “G:\ANACONDA\lib\runpy.py”, line 85, in _run_code
    exec(code, run_globals)
    File “G:\code\torch\net\DataLoader.py”, line 25, in
    for step, (batch_x, batch_y) in enumerate(loader): # 每一步 loader 释放一小批数据用来学习
    File “G:\ANACONDA\lib\site-packages\torch\utils\data\dataloader.py”, line 310, in __iter__
    exec(code, run_globals)
    File “G:\code\torch\net\DataLoader.py”, line 25, in
    for step, (batch_x, batch_y) in enumerate(loader): # 每一步 loader 释放一小批数据用来学习
    File “G:\ANACONDA\lib\site-packages\torch\utils\data\dataloader.py”, line 310, in __iter__
    return DataLoaderIter(self)
    File “G:\ANACONDA\lib\site-packages\torch\utils\data\dataloader.py”, line 167, in __init__
    return DataLoaderIter(self)
    File “G:\ANACONDA\lib\site-packages\torch\utils\data\dataloader.py”, line 167, in __init__
    w.start()
    File “G:\ANACONDA\lib\multiprocessing\process.py”, line 105, in start
    w.start()
    File “G:\ANACONDA\lib\multiprocessing\process.py”, line 105, in start
    self._popen = self._Popen(self)
    File “G:\ANACONDA\lib\multiprocessing\context.py”, line 223, in _Popen
    self._popen = self._Popen(self)
    File “G:\ANACONDA\lib\multiprocessing\context.py”, line 223, in _Popen
    return _default_context.get_context().Process._Popen(process_obj)
    File “G:\ANACONDA\lib\multiprocessing\context.py”, line 322, in _Popen
    return _default_context.get_context().Process._Popen(process_obj)
    File “G:\ANACONDA\lib\multiprocessing\context.py”, line 322, in _Popen
    return Popen(process_obj)
    File “G:\ANACONDA\lib\multiprocessing\popen_spawn_win32.py”, line 33, in __init__
    prep_data = spawn.get_preparation_data(process_obj._name)
    File “G:\ANACONDA\lib\multiprocessing\spawn.py”, line 143, in get_preparation_data
    return Popen(process_obj)
    File “G:\ANACONDA\lib\multiprocessing\popen_spawn_win32.py”, line 33, in __init__
    _check_not_importing_main()
    File “G:\ANACONDA\lib\multiprocessing\spawn.py”, line 136, in _check_not_importing_main
    prep_data = spawn.get_preparation_data(process_obj._name)
    File “G:\ANACONDA\lib\multiprocessing\spawn.py”, line 143, in get_preparation_data
    is not going to be frozen to produce an executable.”’)
    RuntimeError:
    An attempt has been made to start a new process before the
    current process has finished its bootstrapping phase.

    This probably means that you are not using fork to start your
    child processes and you have forgotten to use the proper idiom
    in the main module:

    if __name__ == ‘__main__’:
    freeze_support()

    The “freeze_support()” line can be omitted if the program
    is not going to be frozen to produce an executable.
    _check_not_importing_main()
    File “G:\ANACONDA\lib\multiprocessing\spawn.py”, line 136, in _check_not_importing_main
    is not going to be frozen to produce an executable.”’)
    RuntimeError:
    An attempt has been made to start a new process before the
    current process has finished its bootstrapping phase.

    This probably means that you are not using fork to start your
    child processes and you have forgotten to use the proper idiom
    in the main module:

    if __name__ == ‘__main__’:
    freeze_support()

    The “freeze_support()” line can be omitted if the program
    is not going to be frozen to produce an executable.

返回顶部