multiprocessing error with pytorch on windows 10


i get the following error when i try to execute my code, which clearly shows its an mulitprocessing error:

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

On Linux, the following code runs fine, but Im wondering why i cant get it running on Windows 10. However, here is the code:

import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torchvision import datasets, transforms
from torch.autograd import Variable

kwargs = {'num_workers': 1, 'pin_memory': True}
train_data ='data', train=True,                 download=True,
                                                            transforms.Normalize((0.1307,), (0.3081,))])),
                                 batch_size=64, shuffle=True, **kwargs)

test_data ='data', train=False,
                                                            transforms.Normalize((0.1307,), (0.3081,))])),
                                 batch_size=64, shuffle=True, **kwargs)

class Netz(nn.Module):
    def __init__(self):
        super(Netz, self).__init__()
        self.conv1 = nn.Conv2d(1, 10, kernel_size=5)
        self.conv2 = nn.Conv2d(10, 20, kernel_size=5)
        self.conv_dropout = nn.Dropout2d()
        self.fc1 = nn.Linear(320, 60)
        self.fc2 = nn.Linear(60, 10)

    def forward(self, x):
        x = self.conv1(x)
        x = F.max_pool2d(x, 2)
        x = F.relu(x)
        x = self.conv2(x)
        x = self.conv_dropout(x)
        x = F.max_pool2d(x, 2)
        x = F.relu(x)

model = Netz()

optimizer = optim.SGD(model.parameters(), lr=0.1, momentum=0.8)

def train(epoch):
    for batch_id, (data, target) in enumerate(train_data):
        data = data.cuda()
        target = target.cuda()
        data = Variable(data)
        target = Variable(target)
        out = model(data)
        criterion = F.nll_loss
        loss = criterion(out, target)
        print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(epoch, batch_id * len(data), len(train_data.dataset),
                                                                   100. * batch_id / len(train_data),[0]))

for epoch in range(1, 30):

I tried to fix it with:

if __name__ == '__main__':
    for epoch in range(1, 30):

But also without success.
Anyone has an idea how i can fix this multiprocessing error?
Any help would be appreciated.
(Yes, i know pytorch is not officially released for windows, but i dont think this is causing the error here.)



I found it out myself.

I had to put the whole code into if name == ‘main‘:

Also i forgot the brakets in the end at the transforms.ToTensor part.

Answered By – Ginsor

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published