Hi everyone,
I’ve been trying to optimize images from a specific class and I created the tensor training_data_new myself (serves as a training dataloader). However, it still gives me the error “can’t optimize a non-leaf Tensor” on the line where I create optimizer2. I have tried casting it to float and just multiplying the image by 1.0. Still won’t budge. Does anyone have any ideas?
Here’s a snippet:
it = 0
for batch, (image, label) in enumerate(zip(training_data_new, training_data_new_label)):
    if label == 0:
        image.requires_grad_(True).float()
        image = image * 1.0
        for k in range(0, 5):
            optimizer2 = torch.optim.SGD([image], lr=0.1)
            loss2 = (((image) - (item_to_transform)) ** 2.0).sum()
            optimizer2.zero_grad()
            loss2.backward(retain_graph=True)
            optimizer2.step()
        training_data_new[it] = image
    it += 1

I’m a beginner but I’m trying to do my best.
Thank you in advance!