Modify the output

Hi there,
I want to control parts of the output of my NN, but I get runtime error.
here is the code:

    for i in range(epochs):
        yhat = net(inputs) 
        yhat[0] = yhat[1]   # here
        yhat[-1] = yhat[-2] # here
        loss = free_energy(yhat, x, matrix, rho, lambd) 

Runtime error: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.FloatTensor [511, 1]], which is output 0 of SigmoidBackward, is at version 2; expected version 0 instead. Hint: enable anomaly detection to find the operation that failed to compute its gradient, with torch.autograd.set_detect_anomaly(True).

Appreciate any advice.

Instead of modifying inplace, can you try creating a clone of the object. Something like

yhat = net(inputs) 
yhat_clone = yhat.clone()
yhat_clone[0] = yhat[1]   # here
yhat_clone[-1] = yhat[-2] # here
loss = free_energy(yhat_clone, x, matrix, rho, lambd)