docstr: Add embeddings to all sentences in the given list of sentences. If embeddings are already added, update
  only if embeddings are non-static.
docstr_tok: ["Add","embeddings","to","all","sentences","in","the","given","list","of","sentences",".","If","embeddings","are","already","added",",","update","only","if","embeddings","are","non-static","."]
code: def embed(self, sentences: Union[List[Sentence], Sentence]):
      """Add embeddings to all sentences in the given list of sentences. If embeddings are already added, update
      only if embeddings are non-static.
      """
      if type(sentences) is Sentence:
          sentences = [sentences]
  
      self.rnn.zero_grad()
  
      sentences.sort(key=lambda x: len(x), reverse=True)
  
      self.embeddings.embed(sentences)
  
      # first, sort sentences by number of tokens
      longest_token_sequence_in_batch: int = len(sentences[0])
  
      all_sentence_tensors = []
      lengths: List[int] = []
  
      # go through each sentence in batch
      for _i, sentence in enumerate(sentences):
          lengths.append(len(sentence.tokens))
  
          word_embeddings = []
  
          for token, _token_idx in zip(
              sentence.tokens, range(len(sentence.tokens))
          ):
              word_embeddings.append(token.get_embedding().unsqueeze(0))
  
          # PADDING: pad shorter sentences out
          for _add in range(
              longest_token_sequence_in_batch - len(sentence.tokens)
          ):
              word_embeddings.append(
                  torch.zeros(
                      self.length_of_all_token_embeddings,
                      dtype=torch.float,
                  )
                  .unsqueeze(0)
                  .to(flair.device)
              )
  
          word_embeddings_tensor = torch.cat(word_embeddings, 0).to(
              flair.device
          )
  
          sentence_states = word_embeddings_tensor
  
          # ADD TO SENTENCE LIST: add the representation
          all_sentence_tensors.append(sentence_states.unsqueeze(1))
  
      # --------------------------------------------------------------------
      # GET REPRESENTATION FOR ENTIRE BATCH
      # --------------------------------------------------------------------
      sentence_tensor = torch.cat(all_sentence_tensors, 1)
  
      # --------------------------------------------------------------------
      # FF PART
      # --------------------------------------------------------------------
      # use word dropout if set
      if self.use_word_dropout:
          sentence_tensor = self.word_dropout(sentence_tensor)
  
      if self.reproject_words:
          sentence_tensor = self.word_reprojection_map(sentence_tensor)
  
      sentence_tensor = self.dropout(sentence_tensor)
  
      packed = torch.nn.utils.rnn.pack_padded_sequence(
          sentence_tensor, lengths
      )
  
      self.rnn.flatten_parameters()
  
      lstm_out, hidden = self.rnn(packed)
  
      outputs, output_lengths = torch.nn.utils.rnn.pad_packed_sequence(
          lstm_out
      )
  
      outputs = self.dropout(outputs)
  
      # --------------------------------------------------------------------
      # EXTRACT EMBEDDINGS FROM LSTM
      # --------------------------------------------------------------------
      for sentence_no, length in enumerate(lengths):
          last_rep = outputs[length - 1, sentence_no]
  
          embedding = last_rep
          if self.bidirectional:
              first_rep = outputs[0, sentence_no]
              embedding = torch.cat([first_rep, last_rep], 0)
  
          sentence = sentences[sentence_no]
          sentence.set_embedding(self.name, embedding)
  
imports: import logging
  import re
  from pathlib import Path
  from typing import List, Optional, Union
  import torch
  from deprecated.sphinx import deprecated
  import flair
  from flair.data import Sentence, Token
  from flair.embeddings.document import DocumentEmbeddings
  from flair.embeddings.token import StackedEmbeddings, TokenEmbeddings
  from flair.file_utils import cached_path
  from flair.nn import LockedDropout, WordDropout
label_desc: Use image node_id 3 for calling the DocumentLSTMEmbeddings obj's underlying member method code with example usage: obj.embed(sentences) without return types