title: Setting num_workers >= 0 makes my script skip the trainloader and start from line 0. My model will not train and just finish the script
body: <p>I am currently trying to optimize my Model with Hyperopt, thus I have put my whole training and validation loop inside the objective function. So now I want to set num_workers to a higher value because I run my script on a PC with 16 cores. For it to work on the Windows PC I put the fmin function from hyperopt that calls the objective function in a if <strong>name</strong> == &quot;<strong>main</strong>&quot;: statement like this:</p>
  <pre><code>if __name__ == &quot;__main__&quot;:
      num_eval = 3
      best_param = fmin(objective, space, algo=tpe.suggest, max_evals=num_eval, trials=trials_obj)
  </code></pre>
  <p>If I run my code like this without changing the default value for num_workers everything works.
  If I change num_workers to any value my script runs for a while and then starts again without going through training or validation. I will my put the code below with print statements so you can understand the order in which everything runs in my code. Basically it will not start the training loop getting the data from the trainloader and instead start my script from the first line. I will put my Code below and mark the import spots so you can maybe understand what I am trying to explain.</p>
  <p>Here though a longer explanation what happens in my code. Hopefully it is understandable.</p>
  <p>The script will start, go through the if statement and call the objective function and initializes the model etc. Then it will go through the first for loop which is responsible for the Cross Validation, then call the dataloader, go to the second for loop responsible for the Epochs and then before starting the train loop it will just start again from the top of my script where I load Data and preprocess it and just finish my script without going into the objective function again.</p>
  <p>I think its just some small error somewhere but I can't seem to find it. Any help is much appreciated.</p>
  <pre><code>import blabla 
  
  *Loading Data with asammdf*
  
  print(&quot;Data loading and preprocessing finished&quot;)
  
  
  
   def objective(params):
      
      global best_val_loss
      print(&quot;New Params&quot;)
      input_dim = feature_dim+features_added
      output_dim = 3
      n_hidden=params['n_hidden']
      n_layers=int(params['n_layers'])
      lr = params['learning_rate']
      n_epochs = 4
      best_val_loss = 100
      patience, trials = 30, 0
      batch_size = params['batch_size']
      weight_decay= params['weight_decay']
  
      dense_layers = int(params['dense_layers'])
      
  
      T_0 = int(params['T_0'])
      T_mult= params['T_mult']
      eta_min= params['eta_min']
  
      dropout = params['dropout']
  
      print(&quot;These are the Params :&quot;, params)
  
  
      model = Model_GRU(input_dim, output_dim, n_hidden, n_layers, dropout, dense_layers)
      criterion = nn.CrossEntropyLoss()
      opt = torch.optim.Adam(model.parameters(), lr=lr, weight_decay=weight_decay)
      sched = torch.optim.lr_scheduler.CosineAnnealingWarmRestarts(opt, T_0=T_0, T_mult=T_mult, eta_min=eta_min, last_epoch=-1)
      
      print(&quot;End script here A&quot;)
  
      for fold, (train_idx, test_idx) in enumerate(kfold.split(sequences, y_kfold)):
          patience, trials = 40, 0
          for name, module in model.named_children():
              print('resetting ', name)
              module.reset_parameters()
  
          train_subsampler = torch.utils.data.SubsetRandomSampler(train_idx)
          test_subsampler = torch.utils.data.SubsetRandomSampler(test_idx)
          dataset = BrakeNoiseData(sequences)
          trainloader = DataLoader(dataset, batch_size=batch_size, shuffle=False, sampler=train_subsampler, num_workers=2) 
          valloader = DataLoader(dataset, batch_size=batch_size, shuffle=False, sampler=test_subsampler, num_workers=2)
  
          train_losses = []
          train_accuracys = []
          validation_losses = []
          validation_accuracys = []
          roc_list = []
          lr_loss = []
          print(&quot;End script here B&quot;)
          for epoch in range(n_epochs):
              running_loss = 0
              train_acc = 0
              print(&quot;End script here C&quot;)
              for i, (inputs, labels) in enumerate(trainloader):
                  print(&quot;End script here D&quot;)
                  model.train()                                                               
                  opt.zero_grad()                                                             
                  out = model.forward(inputs)
                  loss = criterion(out, labels) 
                  .
                  .
                  .
  
  if __name__ == &quot;__main__&quot;:
      num_eval = 3
      best_param = fmin(objective, space, algo=tpe.suggest, max_evals=num_eval, trials=trials_obj)
      # Storing real values, not indices
      param_eval = space_eval(space, best_param)
  
  print(&quot;Script finished&quot;)
  </code></pre>
  <pre><code>&gt;&gt;&gt; Dataloading and preprocessing finished
  New Params
  These are the Params : {'T_0': 160.0, 'T_mult': 1, 'batch_size': 16.......
  End script here A
  resetting
  gru
  resetting
  classifier
  End script here B
  End script here C
  ls is not supported    #No clue what that comes from, haven't found anything about it on Google
  Dataloading and preprocessing finished
  Script finished
  </code></pre>