莫烦Pytorch: RuntimeError: An attempt has been made to start a new process....

一. 问题描述

今天在B站上学习莫烦的大佬的pytorch时,遇到了一些问题。

测试代码如下:

import torch
import torch.utils.data as Data

BATCH_SIZE = 5

x = torch.linspace(1, 10, 10)   # this is x data (torch tensor)
y = torch.linspace(10, 1, 10)   # this is y data (torch)

torch_dataset = Data.TensorDataset(x, 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):
        # training....
        print('Epoch: ', epoch, '| Step: ', step, '| batch x: ',
              batch_x.numpy(), '| batch y: ', batch_y.numpy())

上面的代码运行将产生如下错误信息:

RuntimeError: 
        An attempt has been made to start a new process before the
        current process has finished its .

        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.

然后我就查了一下,原来是需要将相关代码放入if _name_ == '_main_'下面。下面贴出原文出处,感谢大佬。

二. 参考

csdn:https://blog.csdn.net/xiemanr/article/details/71700531

你可能感兴趣的:(Error)