It seems in my case the reason is due to the fact that only instances of nn.Modules can be scripted. Therefore, it is necessary to first produce an instance out of torch.nn.BCEWithLogitsLoss and then continue with TorchScripting that instance.