I am trying to build an auto-encoder model, but when I add losses on intermediate layers, pytorch raises the following AssertionError:

AssertionError: nn criterions don’t compute the gradient w.r.t. targets - please mark these variables as volatile or not requiring gradients.

My code is listed as follows:

import torch

fc1 = nn.Linear(50, 20)

fc2 = nn.Linear(20, 10)

fc2_r = nn.Linear(10, 20)

fc1_r = nn.Linear(20, 50)

mse = nn.MSELoss()

x = Variable(torch.randn(100, 50))

l1 = fc1(x)

l2 = fc2(l1)

l1_r = fc2_r(l2)

x_r = fc1_r(l1_r)

loss0 = mse(x_r, x)

loss1 = mse(l1_r, l1)

Any suggestions?