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 a AE 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 = "cuda" if torch.cuda.is_available() else "cpu"
data = data.to(device)
print(f"Using device = {device}")
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)

I cannot reproduce the error using your code snippet, but would guess you could detach() the outputs passed as the target to nn.BCELoss. However, I also don’t quite understand why you would like to calculate the loss between the output and itself. Shouldn’t it be the output and the input ot a target?

However, I’m running into an error complaining about the input value range as nn.BCELoss expects probabilities while you are returning the output of an nn.ReLU layer:

RuntimeError: all elements of input should be between 0 and 1

You could try to use a sigmoid activation and revisit your use case.

Note that the data input tensor is not normalized, so you might also want to divide it by 255.

Thank you for your reply. Your suggestions worked and the code is running now!