Search code examples
pythonmachine-learningdeep-learningpytorchneural-network

Getting None from loss in neural network despite tensors being leaf


I checked all the tensors and input parameters, they were all leaf, according to the code below,

def train_step(w1,b1):
    print("w=",w1)
    trainable_variables = [w1,b1]
    optimizer = torch.optim.SGD(trainable_variables, lr=learning_rate)
    loss = Variable(loss2_function(), requires_grad = True)
    print(loss.backward())
    with torch.no_grad():
        w1 -=(learning_rate * w1.grad)
        b1 -= (learning_rate * b1.grad)
        w1.grad.zero_()
        b1.grad.zero_()
    optimizer.step()
    optimizer.zero_grad()

I still get none, and even with the change in learning rate, weight and bias, the network still does not work, please guide me.


Solution

  • none returned after "loss.backward()": The "loss2_function()" should return a scalar tensor representing the loss, not the backward pass. So, it should just compute and return the loss value. make sure that "loss2_function()" computes and returns the loss tensor.

    Updating weights and biases: When updating the weights and biases, you need to access their grad attributes correctly. Also, after updating the parameters, you should zero out their gradients.

    import torch
    
    def train_step(w1, b1, learning_rate, loss2_function):
        print("Initial weights: w =", w1, ", b =", b1)
        
        # Define trainable variables and optimizer
        trainable_variables = [w1, b1]
        optimizer = torch.optim.SGD(trainable_variables, lr=learning_rate)
        
        # Calculate loss
        loss = loss2_function()
        
        # Perform backpropagation
        loss.backward()
        
        # Update weights and biases using gradient descent
        with torch.no_grad():
            w1 -= learning_rate * w1.grad
            b1 -= learning_rate * b1.grad
            
            # Reset gradients to zero
            w1.grad.zero_()
            b1.grad.zero_()
        
        # Perform optimization step
        optimizer.step()
        
        # Print updated weights
        print("Updated weights: w =", w1, ", b =", b1)
    

    Make sure that "loss2_function()" computes and returns the loss tensor correctly. Also, ensure that you call "train_step()" function properly by passing all the required arguments.