今天在学习pytorch的时候尝试运行以下程序:
transform = transforms.Compose( [transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))]) trainset = torchvision.datasets.CIFAR10(root='./data', train=True, download=True, transform=transform) trainloader = torch.utils.data.DataLoader(trainset, batch_size=4, shuffle=True, num_workers=2) testset = torchvision.datasets.CIFAR10(root='./data', train=False, download=True, transform=transform) testloader = torch.utils.data.DataLoader(testset, batch_size=4, shuffle=False, num_workers=2) def imshow(img): img = img / 2 + 0.5 npimg = img.numpy() plt.imshow(np.transpose(npimg, (1, 2, 0))) plt.show() dataiter = iter(trainloader) images, labels = next(dataiter)
结果运行的时候出现了错误:
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.
我看了一下,iter是一个迭代器,好像是因为多线程的问题导致程序运行不下去了,想到之前看的一篇文章讲的是有关参数的说明,链接如下:
PyTorch常用的torchvision transforms函数 - 知乎 (zhihu.com)
好像是因为复线程的数量太多了,所以没法运行
于是我把前面的trainloader中的num_workers参数改为了0
trainloader = torch.utils.data.DataLoader(trainset, batch_size=4, shuffle=True, num_workers=0)
又尝试运行,结果成功了。
Files already downloaded and verified
Files already downloaded and verified
car cat plane frog
希望对大家有帮助。