class_docstr: Time series encoder. This module can learn features for any `order_by` temporal column, both with and without accompanying target data.
  
  The backbone of this encoder is either a recurrent neural network or a transformer; both structured in an encoder-decoder fashion.
class_docstr_tok: ["Time","series","encoder",".","This","module","can","learn","features","for","any","`","order_by","`","temporal","column",",","both","with","and","without","accompanying","target","data",".","The","backbone","of","this","encoder","is","either","a","recurrent","neural","network","or","a","transformer",";","both","structured","in","an","encoder-decoder","fashion","."]
docstr: This method must be executed after initializing, else types are unassigned
docstr_tok: ["This","method","must","be","executed","after","initializing",",","else","types","are","unassigned"]
code: def setup_nn(self, ts_analysis, dependencies=None):
      """This method must be executed after initializing, else types are unassigned"""
      if self.original_type in (dtype.datetime, dtype.date):
          self._normalizer = DatetimeNormalizerEncoder(sinusoidal=True)
          self._n_dims *= (
              len(self._normalizer.fields) * 2
          )  # sinusoidal datetime components
      elif self.original_type in (dtype.float, dtype.integer):
          self._normalizer = MinMaxNormalizer()
  
      total_dims = self._n_dims
      dec_hsize = self.output_size
  
      if dependencies:
          for dep_name, dep in dependencies.items():
              self.dependencies.append(dep_name)
  
              if dep_name in self.grouped_by:
                  continue  # we only use group column for indexing and selecting rows
  
              assert dep["original_type"] in (
                  dtype.categorical,
                  dtype.binary,
                  dtype.cat_tsarray,
                  dtype.integer,
                  dtype.float,
                  dtype.num_tsarray,
              )
  
              if f"__mdb_ts_previous_{self.target}" == dep_name:
                  self.dep_norms[dep_name] = ts_analysis[
                      "target_normalizers"
                  ]
                  self._group_combinations = ts_analysis[
                      "group_combinations"
                  ]
                  self._target_type = dep["original_type"]
  
              # if TS analysis yields no normalizers for this dependency, we create a generic one based on its dtype
              else:
                  if dep["original_type"] in (
                      dtype.categorical,
                      dtype.binary,
                  ):
                      self.dep_norms[dep_name][
                          "__default"
                      ] = CatNormalizer()
                  else:
                      self.dep_norms[dep_name][
                          "__default"
                      ] = MinMaxNormalizer()
  
                  self.dep_norms[dep_name]["__default"].prepare(
                      dep["data"]
                  )
                  self._group_combinations = {"__default": None}
  
              # add descriptor size to the total encoder output dimensionality
              if dep["original_type"] in (
                  dtype.categorical,
                  dtype.binary,
              ):
                  total_dims += len(
                      self.dep_norms[dep_name][
                          "__default"
                      ].scaler.categories_[0]
                  )
              elif dep["original_type"] in (
                  dtype.integer,
                  dtype.float,
                  dtype.num_tsarray,
                  dtype.cat_tsarray,
              ):
                  total_dims += 1
  
      if self.encoder_class == EncoderRNNNumerical:
          self._enc_criterion = nn.MSELoss()
          self._dec_criterion = self._enc_criterion
          self._encoder = self.encoder_class(
              input_size=total_dims, hidden_size=self.output_size
          ).to(self.device)
      elif self.encoder_class == TransformerEncoder:
          self._enc_criterion = self._masked_criterion
          self._dec_criterion = nn.MSELoss()
          self._base_criterion = nn.MSELoss(reduction="none")
          if self._transformer_hidden_size is None:
              self._transformer_hidden_size = (
                  total_dims * 2
              )  # arbitrary
  
          self._encoder = self.encoder_class(
              ninp=total_dims,
              nhead=gcd(dec_hsize, total_dims),
              nhid=self._transformer_hidden_size,
              nlayers=1,
          ).to(self.device)
      else:
          raise Exception(
              f"Time series encoder class not supported: {self.encoder_class}"
          )
  
      self._decoder = DecoderRNNNumerical(
          output_size=total_dims, hidden_size=dec_hsize
      ).to(self.device)
      self._parameters = list(self._encoder.parameters()) + list(
          self._decoder.parameters()
      )
      self._optimizer = optim.AdamW(
          self._parameters, lr=self._learning_rate, weight_decay=1e-4
      )
      self._n_dims = total_dims
      self._is_setup = True
  
imports: import time
  from math import gcd
  from typing import List
  from copy import deepcopy
  import numpy
  import pandas
  import torch
  import torch.nn
  from torch import optim
  from type_infer.dtype import dtype
  from lightwood.helpers.log import log
  from lightwood.encoder.base import BaseEncoder
  from lightwood.helpers.device import get_device_from_name
  from lightwood.helpers.torch import LightwoodAutocast
  from lightwood.encoder.datetime import DatetimeNormalizerEncoder
  from lightwood.encoder.time_series.helpers.rnn_helpers import EncoderRNNNumerical, DecoderRNNNumerical
  from lightwood.encoder.helpers import MinMaxNormalizer, CatNormalizer
  from lightwood.encoder.time_series.helpers.transformer_helpers import TransformerEncoder, get_chunk, len_to_mask
label_desc: Use image node_id 2 for calling the TimeSeriesEncoder obj's underlying member method code with example usage: obj.setup_nn(ts_analysis, dependencies) without return types