docstr: This method implements truncated backpropagation through time
  Returns: output tensor, None as hidden_state, which does not apply in this case, and loss value
docstr_tok: ["This","method","implements","truncated","backpropagation","through","time","Returns",":","output","tensor",",","None","as","hidden_state",",","which","does","not","apply","in","this","case",",","and","loss","value"]
code: def bptt(self, batch, criterion, device):
      """This method implements truncated backpropagation through time
      Returns: output tensor, None as hidden_state, which does not apply in this case, and loss value
      """
      loss = 0
      train_batch, len_batch = batch
      batch_size, timesteps, _ = train_batch.shape
  
      for start_chunk in range(0, timesteps, timesteps):
          data, targets, lengths_chunk = get_chunk(
              train_batch, len_batch, start_chunk, timesteps
          )
          # Transformer expects seq_length in first dimension, so we transpose
          data = data.transpose(0, 1)
          targets = targets.transpose(0, 1)
          output, hidden = self.forward(data, lengths_chunk, device)
          loss += criterion(output, targets, lengths_chunk)
  
      return output.transpose(0, 1), hidden, loss
  
file_path: lightwood/lightwood/encoder/time_series/helpers/transformer_helpers.py
imports: import math
  import torch
  import torch.nn
  from lightwood.helpers.torch import LightwoodAutocast
label_desc: Use image node_id 5 for calling the TransformerEncoder obj's underlying member method code with example usage: obj.bptt(batch, criterion, device) and returns: output,  hidden,  loss