RuntimeError: the derivative for ‘target’ is not implemented for Auto Encoder
I am getting the following error - RuntimeError: the derivative for ‘target’ is not implemented
I did have a look at similar posts however they are different from my problem. I’m trying to code an Auto Encoder from scratch. Here’s my code -
import torch
import torchvision
import torchvision.transforms as transforms
import matplotlib.pyplot as plt
from torch import nn
from torchviz import make_dot

trainset = torchvision.datasets.FashionMNIST(root='./data', train=True,
                                        download=True)
data = trainset.data.float()
# print(trainset.data.shape)
# plt.imshow(trainset.data[9])
# plt.show()
device = &quot;cuda&quot; if torch.cuda.is_available() else &quot;cpu&quot;
data = data.to(device)
print(f&quot;Using device = {device}&quot;)
class NeuralNetwork(nn.Module):
    def __init__(self):
        super(NeuralNetwork, self).__init__()
        self.flatten = nn.Flatten()
        self.encode = nn.Sequential(
            nn.Linear(28*28, 512),
            nn.ReLU(),
            nn.Linear(512, 30),
            nn.ReLU()
        )
        self.decode = nn.Sequential(
            nn.Linear(30, 512),
            nn.ReLU(),
            nn.Linear(512, 28*28),
            nn.ReLU()
        )

    def forward(self, x):
        x = self.flatten(x)
        encoded = self.encode(x)
        decoded = self.decode(encoded)
        return decoded

model = NeuralNetwork().to(device)
# print(model)

lossFn  = nn.BCELoss()

optimizer = torch.optim.SGD(model.parameters(), lr = 1e-3)

for epoch in range(1000):
    optimizer.zero_grad()
    outputs = model(data)
    loss = lossFn(outputs, outputs)
    loss.backward()
    optimizer.step()
    model.forward(data)


The reason you're having this error message is because you are calling a function which expects its argument to not require gradient computation. More specifically, nn.BCELoss expects the target (2nd argument) to not require gradient.
You can fix this by detaching the argument from the graph:
lossFn(outputs, outputs.detach())

