Greetings everyone,
I am trying to define a loss and use backward() on it. However, I am getting the error: One of the variables needed for gradient computation has been modified by an inplace operation.
Here is the code:
for displaying the target image, intermittently
show_every = 400
iteration hyperparameters
optimizer = optim.Adam([target], lr=0.003)
steps = 2000  # decide how many iterations to update your image (5000)
for ii in range(1, steps+1):
# get the features from your target image
target_features = get_features(target, vgg)
                               
# content loss
content_loss = torch.mean((target_features[content_layer] - content_features[content_layer])**2)

# photorealistic regularization loss
reg_loss, reg_grad = PhotorealismRegularization(content.detach().to("cpu").numpy().transpose(0, 2, 3, 1).squeeze().clip(0, 1), target.detach().to("cpu").numpy().transpose(0, 2, 3, 1).squeeze().clip(0, 1))

reg_grad_tensor = image_to_tensor(reg_grad).to(device)

#target.grad += reg_grad_tensor

# augmented style loss
#style_loss = augmented_style_loss(target, vgg, content_masks, style_masks)

layers = {
    '0':'conv1_1',
    '5':'conv2_1',
    '10':'conv3_1',
    '19':'conv4_1',
    '28':'conv5_1'
}

style_loss = 0.
x = target.clone().to(device)

# model._modules is a dictionary holding each module in the model
for name, layer in vgg._modules.items():
    x = layer(x)
    if isinstance(layer, torch.nn.MaxPool2d):
        style_masks = [layer(mask) for mask in style_masks]
        content_masks = [layer(mask) for mask in content_masks]
      
    if name in layers:
        target_gram = [gram_matrix(x * Variable(mask.to(device), requires_grad = True)) for mask in content_masks]
        style_gram = [gram_matrix(x * Variable(mask.to(device), requires_grad = True)) for mask in style_masks]
        
        layer_loss = style_weights[layers[name]] * sum(F.mse_loss(t_gram, s_gram) for t_gram, s_gram in zip(target_gram, style_gram))
        
        target_feature = target_features[layers[name]]
        _, d, h, w = target_feature.shape
        
        style_loss += layer_loss / (d * h * w)


# total loss
total_loss = content_weight * content_loss + style_weight * style_loss 

# update target image
optimizer.zero_grad()
total_loss.backward()
optimizer.step()

Kindly let me know where I am going wrong and how to fix it.
Thanks.