RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn
Hi,
Got RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn
Below details:
class Autoencoder(pl.LightningModule):
    
    def __init__(self, hparams: argparse.Namespace):
        super(Autoencoder,self).__init__() 
        self.hparams = hparams
           
        self.layer_e_1 = nn.Conv1d(hparams.in_channels, hparams.out_channels, hparams.kernel_size)
        self.layer_e_2 = nn.Conv1d(hparams.out_channels,hparams.in_channels,hparams.kernel_size)
        self.layer_d_1 = nn.ConvTranspose1d(hparams.in_channels,hparams.out_channels,hparams.kernel_size)
        self.layer_d_2 = nn.ConvTranspose1d(hparams.out_channels,hparams.in_channels,hparams.kernel_size)
        
        Train_x_t_e = CarrierDataset()
    
    def forward(self,x):
        x = self.layer_e_1(x)
        x = F.relu(x)
        x = self.layer_e_2(x)
        encoded = F.relu(x)
        x = self.layer_d_1(encoded)
        x = F.relu(x)
        decoded = self.layer_d_2(x)
        return decoded, encoded
    
    
    def training_step(self, train_batch, batch_idx):
        x, _ = train_batch
        decoded, encoded = self.forward(x)
        mse = nn.MSELoss()
        loss = mse(x, decoded)
        return {'loss': loss}
    
    def validation_step(self,val_batch, batch_idx):
        x, _ = val_batch
        decoded, encoded = self.forward(x)
        mse = nn.MSELoss()
        loss = mse(x, decoded)
        return {'val_loss': loss}
    
    def train_dataloader(self):
        loader = torch.utils.data.DataLoader(
            dataset=Train_x_t_e,
            batch_size=self.hparams.batch_size,
            shuffle=True,
            pin_memory=True)
        return loader

    def val_dataloader(self):
        return DataLoader(dataset=Train_x_t_e, batch_size=self.hparams.batch_size)

    def configure_optimizers(self):
        optimizer = torch.optim.Adam(self.parameters(), lr=self.hparams.lr)
        return optimizer

class CarrierDataset(data.Dataset):
    
    def __init__(self):
        Train_x_t_e1 = torch.load('Tensor_100K_1.pt')
        Train_x_t_e2 = torch.load('Tensor_100K_2.pt')
        ag_t = torch.cat((Train_x_t_e1,Train_x_t_e2))
        #df = read_csv(ds)
        self.len = ag_t.shape[0]
        self.ag_t = ag_t
        self.size = sys.getsizeof(ag_t)
        
    def __getitem__(self,index):
        return self.ag_t[index], self.ag_t[index]
    
    def __len__(self):
        return self.len
    
    def __size__(self):
        return self.size    

Got the following error:
RuntimeErrorTraceback (most recent call last)
<ipython-input-277-b675dec29cfe> in <module>
     16 print("Parameters:")
     17 print(args)
---> 18 main(args)

<ipython-input-276-23c135174ba6> in main(hparams)
     19     )
     20 
---> 21     trainer.fit(model)

/opt/conda/lib/python3.7/site-packages/pytorch_lightning/trainer/trainer.py in fit(self, model, train_dataloader, val_dataloaders)
    885             self.optimizers, self.lr_schedulers, self.optimizer_frequencies = self.init_optimizers(model)
    886 
--> 887             self.run_pretrain_routine(model)
    888 
    889         # return 1 when finished

/opt/conda/lib/python3.7/site-packages/pytorch_lightning/trainer/trainer.py in run_pretrain_routine(self, model)
   1013 
   1014         # CORE TRAINING LOOP
-> 1015         self.train()
   1016 
   1017     def test(

/opt/conda/lib/python3.7/site-packages/pytorch_lightning/trainer/training_loop.py in train(self)
    345                 # RUN TNG EPOCH
    346                 # -----------------
--> 347                 self.run_training_epoch()
    348 
    349                 # update LR schedulers

/opt/conda/lib/python3.7/site-packages/pytorch_lightning/trainer/training_loop.py in run_training_epoch(self)
    417             # RUN TRAIN STEP
    418             # ---------------
--> 419             _outputs = self.run_training_batch(batch, batch_idx)
    420             batch_result, grad_norm_dic, batch_step_metrics, batch_output = _outputs
    421 

/opt/conda/lib/python3.7/site-packages/pytorch_lightning/trainer/training_loop.py in run_training_batch(self, batch, batch_idx)
    595 
    596                 # calculate loss
--> 597                 loss, batch_output = optimizer_closure()
    598 
    599                 # check if loss or model weights are nan

/opt/conda/lib/python3.7/site-packages/pytorch_lightning/trainer/training_loop.py in optimizer_closure()
    573                     model_ref = self.get_model()
    574                     with self.profiler.profile('model_backward'):
--> 575                         model_ref.backward(self, closure_loss, optimizer, opt_idx)
    576 
    577                     # track metrics for callbacks

/opt/conda/lib/python3.7/site-packages/pytorch_lightning/core/hooks.py in backward(self, trainer, loss, optimizer, optimizer_idx)
    153                     scaled_loss.backward()
    154         else:
--> 155             loss.backward()

/opt/conda/lib/python3.7/site-packages/torch/tensor.py in backward(self, gradient, retain_graph, create_graph)
    196                 products. Defaults to ``False``.
    197         """
--> 198         torch.autograd.backward(self, gradient, retain_graph, create_graph)
    199 
    200     def register_hook(self, hook):

/opt/conda/lib/python3.7/site-packages/torch/autograd/__init__.py in backward(tensors, grad_tensors, retain_graph, create_graph, grad_variables)
     98     Variable._execution_engine.run_backward(
     99         tensors, grad_tensors, retain_graph, create_graph,
--> 100         allow_unreachable=True)  # allow_unreachable flag
    101 
    102 

RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn