docstr: Sequence Tagger class for predicting labels for single tokens. Can be parameterized by several attributes.
  
  In case of multitask learning, pass shared embeddings or shared rnn into respective attributes.
  
  Args:
      embeddings: Embeddings to use during training and prediction
      tag_dictionary: Dictionary containing all tags from corpus which can be predicted
      tag_type: type of tag which is going to be predicted in case a corpus has multiple annotations
      use_rnn: If true, use a RNN, else Linear layer.
      rnn: Takes a torch.nn.Module as parameter by which you can pass a shared RNN between different tasks.
      rnn_type: Specifies the RNN type to use, default is 'LSTM', can choose between 'GRU' and 'RNN' as well.
      hidden_size: Hidden size of RNN layer
      rnn_layers: number of RNN layers
      bidirectional: If True, RNN becomes bidirectional
      use_crf: If True, use a Conditional Random Field for prediction, else linear map to tag space.
      reproject_embeddings: If True, add a linear layer on top of embeddings, if you want to imitate fine tune non-trainable embeddings.
      dropout: If > 0, then use dropout.
      word_dropout: If > 0, then use word dropout.
      locked_dropout: If > 0, then use locked dropout.
      train_initial_hidden_state: if True, trains initial hidden state of RNN
      loss_weights: Dictionary of weights for labels for the loss function. If any label's weight is unspecified it will default to 1.0.
      init_from_state_dict: Indicator whether we are loading a model from state dict since we need to transform previous models' weights into CRF instance weights
      allow_unk_predictions: If True, allows spans to predict <unk> too.
      tag_format: the format to encode spans as tags, either "BIO" or "BIOES"
docstr_tok: ["Sequence","Tagger","class","for","predicting","labels","for","single","tokens",".","Can","be","parameterized","by","several","attributes",".","In","case","of","multitask","learning",",","pass","shared","embeddings","or","shared","rnn","into","respective","attributes",".","Args",":","embeddings",":","Embeddings","to","use","during","training","and","prediction","tag_dictionary",":","Dictionary","containing","all","tags","from","corpus","which","can","be","predicted","tag_type",":","type","of","tag","which","is","going","to","be","predicted","in","case","a","corpus","has","multiple","annotations","use_rnn",":","If","true",",","use","a","RNN",",","else","Linear","layer",".","rnn",":","Takes","a","torch.nn.Module","as","parameter","by","which","you","can","pass","a","shared","RNN","between","different","tasks",".","rnn_type",":","Specifies","the","RNN","type","to","use",",","default","is","'LSTM","'",",","can","choose","between","'GRU","'","and","'RNN","'","as","well",".","hidden_size",":","Hidden","size","of","RNN","layer","rnn_layers",":","number","of","RNN","layers","bidirectional",":","If","True",",","RNN","becomes","bidirectional","use_crf",":","If","True",",","use","a","Conditional","Random","Field","for","prediction",",","else","linear","map","to","tag","space",".","reproject_embeddings",":","If","True",",","add","a","linear","layer","on","top","of","embeddings",",","if","you","want","to","imitate","fine","tune","non-trainable","embeddings",".","dropout",":","If",">","0",",","then","use","dropout",".","word_dropout",":","If",">","0",",","then","use","word","dropout",".","locked_dropout",":","If",">","0",",","then","use","locked","dropout",".","train_initial_hidden_state",":","if","True",",","trains","initial","hidden","state","of","RNN","loss_weights",":","Dictionary","of","weights","for","labels","for","the","loss","function",".","If","any","label","'s","weight","is","unspecified","it","will","default","to","1.0.","init_from_state_dict",":","Indicator","whether","we","are","loading","a","model","from","state","dict","since","we","need","to","transform","previous","models","'","weights","into","CRF","instance","weights","allow_unk_predictions",":","If","True",",","allows","spans","to","predict","<","unk",">","too",".","tag_format",":","the","format","to","encode","spans","as","tags",",","either","``","BIO","''","or","``","BIOES","''"]
code: def __init__(
      self,
      embeddings: TokenEmbeddings,
      tag_dictionary: Dictionary,
      tag_type: str,
      use_rnn: bool = True,
      rnn: Optional[torch.nn.RNN] = None,
      rnn_type: str = "LSTM",
      tag_format: str = "BIOES",
      hidden_size: int = 256,
      rnn_layers: int = 1,
      bidirectional: bool = True,
      use_crf: bool = True,
      reproject_embeddings: bool = True,
      dropout: float = 0.0,
      word_dropout: float = 0.05,
      locked_dropout: float = 0.5,
      train_initial_hidden_state: bool = False,
      loss_weights: Optional[Dict[str, float]] = None,
      init_from_state_dict: bool = False,
      allow_unk_predictions: bool = False,
  ) -> None:
      """Sequence Tagger class for predicting labels for single tokens. Can be parameterized by several attributes.
  
      In case of multitask learning, pass shared embeddings or shared rnn into respective attributes.
  
      Args:
          embeddings: Embeddings to use during training and prediction
          tag_dictionary: Dictionary containing all tags from corpus which can be predicted
          tag_type: type of tag which is going to be predicted in case a corpus has multiple annotations
          use_rnn: If true, use a RNN, else Linear layer.
          rnn: Takes a torch.nn.Module as parameter by which you can pass a shared RNN between different tasks.
          rnn_type: Specifies the RNN type to use, default is 'LSTM', can choose between 'GRU' and 'RNN' as well.
          hidden_size: Hidden size of RNN layer
          rnn_layers: number of RNN layers
          bidirectional: If True, RNN becomes bidirectional
          use_crf: If True, use a Conditional Random Field for prediction, else linear map to tag space.
          reproject_embeddings: If True, add a linear layer on top of embeddings, if you want to imitate fine tune non-trainable embeddings.
          dropout: If > 0, then use dropout.
          word_dropout: If > 0, then use word dropout.
          locked_dropout: If > 0, then use locked dropout.
          train_initial_hidden_state: if True, trains initial hidden state of RNN
          loss_weights: Dictionary of weights for labels for the loss function. If any label's weight is unspecified it will default to 1.0.
          init_from_state_dict: Indicator whether we are loading a model from state dict since we need to transform previous models' weights into CRF instance weights
          allow_unk_predictions: If True, allows spans to predict <unk> too.
          tag_format: the format to encode spans as tags, either "BIO" or "BIOES"
      """
      super().__init__()
  
      # ----- Create the internal tag dictionary -----
      self.tag_type = tag_type
      self.tag_format = tag_format.upper()
      if init_from_state_dict:
          self.label_dictionary = tag_dictionary
      else:
          # span-labels need special encoding (BIO or BIOES)
          if tag_dictionary.span_labels:
              # the big question is whether the label dictionary should contain an UNK or not
              # without UNK, we cannot evaluate on data that contains labels not seen in test
              # with UNK, the model learns less well if there are no UNK examples
              self.label_dictionary = Dictionary(
                  add_unk=allow_unk_predictions
              )
              assert self.tag_format in ["BIOES", "BIO"]
              for label in tag_dictionary.get_items():
                  if label == "<unk>":
                      continue
                  self.label_dictionary.add_item("O")
                  if self.tag_format == "BIOES":
                      self.label_dictionary.add_item("S-" + label)
                      self.label_dictionary.add_item("B-" + label)
                      self.label_dictionary.add_item("E-" + label)
                      self.label_dictionary.add_item("I-" + label)
                  if self.tag_format == "BIO":
                      self.label_dictionary.add_item("B-" + label)
                      self.label_dictionary.add_item("I-" + label)
          else:
              self.label_dictionary = tag_dictionary
  
      # is this a span prediction problem?
      self.predict_spans = self._determine_if_span_prediction_problem(
          self.label_dictionary
      )
  
      self.tagset_size = len(self.label_dictionary)
      log.info(f"SequenceTagger predicts: {self.label_dictionary}")
  
      # ----- Embeddings -----
      self.embeddings = embeddings
      embedding_dim: int = embeddings.embedding_length
  
      # ----- Initial loss weights parameters -----
      self.weight_dict = loss_weights
      self.loss_weights = (
          self._init_loss_weights(loss_weights)
          if loss_weights
          else None
      )
  
      # ----- RNN specific parameters -----
      self.use_rnn = use_rnn
      self.rnn_type = rnn_type if not rnn else rnn._get_name()
      self.hidden_size = hidden_size if not rnn else rnn.hidden_size
      self.rnn_layers = rnn_layers if not rnn else rnn.num_layers
      self.bidirectional = (
          bidirectional if not rnn else rnn.bidirectional
      )
  
      # ----- Conditional Random Field parameters -----
      self.use_crf = use_crf
      # Previously trained models have been trained without an explicit CRF, thus it is required to check
      # whether we are loading a model from state dict in order to skip or add START and STOP token
      if (
          use_crf
          and not init_from_state_dict
          and not self.label_dictionary.start_stop_tags_are_set()
      ):
          self.label_dictionary.set_start_stop_tags()
          self.tagset_size += 2
  
      # ----- Dropout parameters -----
      # dropouts
  
      # remove word dropout if there is no contact over the sequence dimension.
      if not use_crf and not use_rnn:
          word_dropout = 0.0
  
      self.use_dropout: float = dropout
      self.use_word_dropout: float = word_dropout
      self.use_locked_dropout: float = locked_dropout
  
      if dropout > 0.0:
          self.dropout = torch.nn.Dropout(dropout)
  
      if word_dropout > 0.0:
          self.word_dropout = flair.nn.WordDropout(word_dropout)
  
      if locked_dropout > 0.0:
          self.locked_dropout = flair.nn.LockedDropout(locked_dropout)
  
      # ----- Model layers -----
      self.reproject_embeddings = reproject_embeddings
      if self.reproject_embeddings:
          self.embedding2nn = torch.nn.Linear(
              embedding_dim, embedding_dim
          )
  
      # ----- RNN layer -----
      if use_rnn:
          # If shared RNN provided, else create one for model
          self.rnn: torch.nn.RNN = (
              rnn
              if rnn
              else self.RNN(
                  rnn_type,
                  rnn_layers,
                  hidden_size,
                  bidirectional,
                  rnn_input_dim=embedding_dim,
              )
          )
  
          num_directions = 2 if self.bidirectional else 1
          hidden_output_dim = self.rnn.hidden_size * num_directions
  
          # Whether to train initial hidden state
          self.train_initial_hidden_state = train_initial_hidden_state
          if self.train_initial_hidden_state:
              (
                  self.hs_initializer,
                  self.lstm_init_h,
                  self.lstm_init_c,
              ) = self._init_initial_hidden_state(num_directions)
  
          # final linear map to tag space
          self.linear = torch.nn.Linear(
              hidden_output_dim, len(self.label_dictionary)
          )
      else:
          self.linear = torch.nn.Linear(
              embedding_dim, len(self.label_dictionary)
          )
          self.train_initial_hidden_state = False
  
      # the loss function is Viterbi if using CRF, else regular Cross Entropy Loss
      self.loss_function = (
          ViterbiLoss(self.label_dictionary)
          if use_crf
          else torch.nn.CrossEntropyLoss(
              weight=self.loss_weights, reduction="sum"
          )
      )
  
      # if using CRF, we also require a CRF and a Viterbi decoder
      if use_crf:
          self.crf = CRF(
              self.label_dictionary,
              self.tagset_size,
              init_from_state_dict,
          )
          self.viterbi_decoder = ViterbiDecoder(self.label_dictionary)
  
      self.to(flair.device)
  
imports: import logging
  import tempfile
  from pathlib import Path
  from typing import Any, Dict, List, Optional, Tuple, Union, cast
  from urllib.error import HTTPError
  import torch
  import torch.nn
  import torch.nn.functional
  from torch.nn.utils.rnn import pack_padded_sequence, pad_packed_sequence
  from tqdm import tqdm
  import flair.nn
  from flair.data import Dictionary, Label, Sentence, Span, get_spans_from_bio
  from flair.datasets import DataLoader, FlairDatapointDataset
  from flair.embeddings import TokenEmbeddings
  from flair.file_utils import cached_path, unzip_file
  from flair.models.sequence_tagger_utils.crf import CRF
  from flair.models.sequence_tagger_utils.viterbi import ViterbiDecoder, ViterbiLoss
  from flair.training_utils import store_embeddings
label_desc: Use image node_id 1 to create a new SequenceTagger object from inherited base classes: flair.nn.Classifier with example: obj = SequenceTagger(embeddings, tag_dictionary, tag_type, use_rnn, rnn, rnn_type, tag_format, hidden_size, rnn_layers, bidirectional, use_crf, reproject_embeddings, dropout, word_dropout, locked_dropout, train_initial_hidden_state, loss_weights, init_from_state_dict, allow_unk_predictions)