TypeError: unsupported operand type(s) for +: 'MSELoss' and 'float'

It looks like you would like to create a static computation graph, which is then executed (theano-style).
In PyTorch you create a dynamic computation graph, so that each operation is executed immediately (with some exceptions like asynchronous execution of GPU operations).
If you would like to add some value to your criterion, you have to calculate the loss first:

criterion = nn.MSELoss()
...
# In your training loop
output = model(data)
loss = criterion(output, target)
loss = loss + E

Also, have a look at this thread for more information regarding custom regularization.

1 Like