docstr: This method decodes an input unrolled through time, given an initial hidden state
docstr_tok: ["This","method","decodes","an","input","unrolled","through","time",",","given","an","initial","hidden","state"]
code: def decode(
      self,
      data,
      initial_tensor,
      criterion,
      device,
      hidden_state=None,
      sos=0,
  ):
      """This method decodes an input unrolled through time, given an initial hidden state"""
      if isinstance(data, tuple):
          # when using transformer encoder, data contains a sequence length tensor
          data, len_data = data
  
      if initial_tensor.shape[1] > 1:
          # if tensor is a sequence (like the one yielded by the transformer),
          # we select only the last timestep for decoding
          initial_tensor = initial_tensor[:, -1:, :]
  
      loss = 0
      next_tensor = torch.full_like(
          initial_tensor, sos, dtype=torch.float32
      ).to(device)
      tensor_target = torch.cat(
          [next_tensor, data], dim=1
      )  # add SOS token at t=0 to true input
      if hidden_state is None:
          hidden_state = self.init_hidden(device, data.shape[0])
  
      for tensor_i in range(data.shape[1] - 1):
          rand = np.random.randint(2)
          # teach from forward as well as from known tensor alternatively
          if rand == 1:
              next_tensor, hidden_state = self.forward(
                  tensor_target[:, tensor_i, :].unsqueeze(dim=1),
                  hidden_state,
              )
          else:
              next_tensor, hidden_state = self.forward(
                  next_tensor.detach(), hidden_state
              )
  
          loss += criterion(
              next_tensor,
              tensor_target[:, tensor_i + 1, :].unsqueeze(dim=1),
          )
  
      return next_tensor, hidden_state, loss
  
file_path: lightwood/lightwood/encoder/time_series/helpers/rnn_helpers.py
imports: import torch
  import torch.nn
  import numpy
  from lightwood.helpers.torch import LightwoodAutocast
label_desc: Use image node_id 4 for calling the DecoderRNNNumerical obj's underlying member method code with example usage: obj.decode(data, initial_tensor, criterion, device, hidden_state, sos) and returns: next_tensor,  hidden_state,  loss