My loss function is throwing a detach error

Here is my loss function:

def validation_step(self, batch):
inputs, targets = batch
# Generate predictions
out = self(inputs)
# Calculate loss
loss = nn.CTCLoss(out, targets) # fill this
return {‘val_loss’: loss.detach()}

I get this error in the evaluate step:

  ModuleAttributeError: 'CTCLoss' object has no attribute 'detach'

any insight would be helpful. I have tried other loss functions and all throw the same error. My guess is the error is python/user related and not PyTorch related.

Thank you,

You’re mistaking 2 things.

Your nn.CTCLoss here is actually a class representing a loss function, which needs to be constructed first, and then used.

I have no idea why it’s constructor didn’t throw any error, because the arguments are different from what it’s supposed to accept.

If you’re really inclined to use this function, you should look for F.ctc_loss().

But I doubt this will be useful here, since this function was created for a totally different task.


Thanks for your help and you are correct. The first code snippet is from the class in Step 3: Create a Linear Regression Model of assignment 2 where the loss function including the detach attribute is defined. The ModuleAttributeError error is thrown later in Step 4: Train the model to fit the data when I call the evaluate function to calculate the loss on the validation.

I have tried F.ctc_loss() as well and get the same error.

Thanks again for your help.