Search code examples
pythondeep-learningneural-networkpytorch

Is there a way to train a neural network with untrainable params in torch?


So, I am trying to learn the transfer learning and trying to implement it in an autoencoder while I have frozen some of my initial layers and then i added new layers in it as well now i am trying to train it but for some reasons i cannot train them. Any concept on how to training really works with the untrainable params?

This is the code for my training the error occurs in backward propagation and it thinks due to some modification in neural network it cannot work.

nb_epoch = 10
for epoch in range(1, nb_epoch+1):
    train_loss = 0
    s = 0. #Compute RMSE
    for id_user in range(nb_users):
        input = Variable(training_set_torch[id_user]).unsqueeze(0)#Batch of 1 input vector or else wont work
        target = input.clone()
        if torch.sum(target.data > 0) > 0: #Target.data is rating
            output = net(input)
            #Now we create optimize the memory
            target.require_grad = False
            output[target == 0] = 0
            #calculating loss
            loss = criterion(output, target)
            #Average of movie considered like non zero ratings
            mean_corrector = nb_users/float(torch.sum(target.data > 0)+ 1e-10)# 1e-10 is to avoid 0/0
            #Now calling backward method
            loss.backward()*
            train_loss += np.sqrt(loss.data*mean_corrector)
            s += 1.
            #Using the Optimzer
            optimizer.step()
    
    #Printing what happens at every epoch
    print('Epoch: ' +str(epoch)+ 'Loss: '+str(train_loss/s))

*Error occured

ERROR: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.FloatTensor [1, 1000]], which is output 0 of SigmoidBackward, is at version 1; expected version 0 instead. Hint: enable anomaly detection to find the operation that failed to compute its gradient, with torch.autograd.set_detect_anomaly(True).

and when I set that to True it shows another error

ERROR: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.FloatTensor [1, 1000]], which is output 0 of SigmoidBackward, is at version 1; expected version 0 instead. Hint: the backtrace further above shows the operation that failed to compute its gradient. The variable in question was changed in there or anywhere later. Good luck!


Solution

  • I suspect this line is causing you the issue:

    output[target == 0] = 0
    

    Try removing it and see if you get an error.

    Update:
    You can remove these entries, and still avoid the "in-place" error by replacing the assignment with a multiplication by a mask:

    output = output * (target != 0)