class_docstr: A wrapper to retrieve word embeddings out of a pretrained Transformer model
  from HuggingFace Transformers (e.g. BERT)
  
  Arguments
  ---------
  model: str|nn.Module
      the underlying model instance or the name of the model
      to download
  
  tokenizer: str|transformers.tokenization_utils_base.PreTrainedTokenizerBase
      a pretrained tokenizer - or the identifier to retrieve
      one from HuggingFace
  
  layers: int|list
      a list of layer indexes from which to construct an embedding or the number of layers
  
  device:
      a torch device identifier. If provided, the model
      will be transferred onto that device
  
  Example
  -------
  NOTE: Doctests are disabled because the dependency on the
  HuggingFace transformer library is optional.
  
  >>> from transformers import AutoTokenizer, AutoModel # doctest: +SKIP
  >>> from speechbrain.wordemb.transformer import TransformerWordEmbeddings
  >>> model_name = "bert-base-uncased" # doctest: +SKIP
  >>> tokenizer = AutoTokenizer.from_pretrained(
  ...    model_name, return_tensors='pt') # doctest: +SKIP
  >>> model = AutoModel.from_pretrained(
  ...     model_name,
  ...     output_hidden_states=True) # doctest: +SKIP
  >>> word_emb = TransformerWordEmbeddings(
  ...     model=model,
  ...     layers=4,
  ...     tokenizer=tokenizer
  ... ) # doctest: +SKIP
  >>> embedding = word_emb.embedding(
  ...     sentence="THIS IS A TEST SENTENCE",
  ...     word="TEST"
  ... ) # doctest: +SKIP
  >>> embedding[:8] # doctest: +SKIP
  tensor([ 3.4332, -3.6702,  0.5152, -1.9301,  0.9197,  2.1628, -0.2841, -0.3549])
  >>> embeddings = word_emb.embeddings("This is cool") # doctest: +SKIP
  >>> embeddings.shape # doctest: +SKIP
  torch.Size([3, 768])
  >>> embeddings[:, :3] # doctest: +SKIP
  tensor([[-2.9078,  1.2496,  0.7269],
      [-0.9940, -0.6960,  1.4350],
      [-1.2401, -3.8237,  0.2739]])
  >>> sentences = [
  ...     "This is the first test sentence",
  ...     "This is the second test sentence",
  ...     "A quick brown fox jumped over the lazy dog"
  ... ]
  >>> batch_embeddings = word_emb.batch_embeddings(sentences) # doctest: +SKIP
  >>> batch_embeddings.shape # doctest: +SKIP
  torch.Size([3, 9, 768])
  >>> batch_embeddings[:, :2, :3] # doctest: +SKIP
  tensor([[[-5.0935, -1.2838,  0.7868],
           [-4.6889, -2.1488,  2.1380]],
  
          [[-4.4993, -2.0178,  0.9369],
           [-4.1760, -2.4141,  1.9474]],
  
          [[-1.0065,  1.4227, -2.6671],
           [-0.3408, -0.6238,  0.1780]]])
class_docstr_tok: ["A","wrapper","to","retrieve","word","embeddings","out","of","a","pretrained","Transformer","model","from","HuggingFace","Transformers","(","e.g",".","BERT",")","Arguments","--","--","--","--","-","model",":","str|nn.Module","the","underlying","model","instance","or","the","name","of","the","model","to","download","tokenizer",":","str|transformers.tokenization_utils_base.PreTrainedTokenizerBase","a","pretrained","tokenizer","-","or","the","identifier","to","retrieve","one","from","HuggingFace","layers",":","int|list","a","list","of","layer","indexes","from","which","to","construct","an","embedding","or","the","number","of","layers","device",":","a","torch","device","identifier",".","If","provided",",","the","model","will","be","transferred","onto","that","device","Example","--","--","--","-","NOTE",":","Doctests","are","disabled","because","the","dependency","on","the","HuggingFace","transformer","library","is","optional",".",">",">",">","from","transformers","import","AutoTokenizer",",","AutoModel","#","doctest",":","+SKIP",">",">",">","from","speechbrain.wordemb.transformer","import","TransformerWordEmbeddings",">",">",">","model_name","=","``","bert-base-uncased","''","#","doctest",":","+SKIP",">",">",">","tokenizer","=","AutoTokenizer.from_pretrained","(","...","model_name",",","return_tensors='pt","'",")","#","doctest",":","+SKIP",">",">",">","model","=","AutoModel.from_pretrained","(","...","model_name",",","...","output_hidden_states=True",")","#","doctest",":","+SKIP",">",">",">","word_emb","=","TransformerWordEmbeddings","(","...","model=model",",","...","layers=4",",","...","tokenizer=tokenizer","...",")","#","doctest",":","+SKIP",">",">",">","embedding","=","word_emb.embedding","(","...","sentence=","''","THIS","IS","A","TEST","SENTENCE","''",",","...","word=","''","TEST","''","...",")","#","doctest",":","+SKIP",">",">",">","embedding","[",":8","]","#","doctest",":","+SKIP","tensor","(","[","3.4332",",","-3.6702",",","0.5152",",","-1.9301",",","0.9197",",","2.1628",",","-0.2841",",","-0.3549","]",")",">",">",">","embeddings","=","word_emb.embeddings","(","``","This","is","cool","''",")","#","doctest",":","+SKIP",">",">",">","embeddings.shape","#","doctest",":","+SKIP","torch.Size","(","[","3",",","768","]",")",">",">",">","embeddings","[",":",",",":3","]","#","doctest",":","+SKIP","tensor","(","[","[","-2.9078",",","1.2496",",","0.7269","]",",","[","-0.9940",",","-0.6960",",","1.4350","]",",","[","-1.2401",",","-3.8237",",","0.2739","]","]",")",">",">",">","sentences","=","[","...","``","This","is","the","first","test","sentence","''",",","...","``","This","is","the","second","test","sentence","''",",","...","``","A","quick","brown","fox","jumped","over","the","lazy","dog","''","...","]",">",">",">","batch_embeddings","=","word_emb.batch_embeddings","(","sentences",")","#","doctest",":","+SKIP",">",">",">","batch_embeddings.shape","#","doctest",":","+SKIP","torch.Size","(","[","3",",","9",",","768","]",")",">",">",">","batch_embeddings","[",":",",",":2",",",":3","]","#","doctest",":","+SKIP","tensor","(","[","[","[","-5.0935",",","-1.2838",",","0.7868","]",",","[","-4.6889",",","-2.1488",",","2.1380","]","]",",","[","[","-4.4993",",","-2.0178",",","0.9369","]",",","[","-4.1760",",","-2.4141",",","1.9474","]","]",",","[","[","-1.0065",",","1.4227",",","-2.6671","]",",","[","-0.3408",",","-0.6238",",","0.1780","]","]","]",")"]
docstr: Returns the model embeddings for all words
  in a sentence
  
  Arguments
  ---------
  sentence: str
      a sentence
  
  Returns
  -------
  emb: torch.Tensor
      a tensor of all word embeddings
docstr_tok: ["Returns","the","model","embeddings","for","all","words","in","a","sentence","Arguments","--","--","--","--","-","sentence",":","str","a","sentence","Returns","--","--","--","-","emb",":","torch.Tensor","a","tensor","of","all","word","embeddings"]
code: def embeddings(self, sentence):
      """
      Returns the model embeddings for all words
      in a sentence
  
      Arguments
      ---------
      sentence: str
          a sentence
  
      Returns
      -------
      emb: torch.Tensor
          a tensor of all word embeddings
  
      """
      encoded = self.tokenizer.encode_plus(
          sentence, return_tensors="pt"
      )
  
      with torch.no_grad():
          output = self.model(**self._to_device(encoded))
  
      token_ids_word = torch.tensor(
          [
              idx
              for idx, word_id in enumerate(encoded.word_ids())
              if word_id is not None
          ],
          device=self.device,
      )
      states = torch.stack(output.hidden_states)
      return self._get_hidden_states(states, token_ids_word)
  
imports: import torch
  import numpy
  from torch import nn
label_desc: Use image node_id 4 for calling the TransformerWordEmbeddings obj's underlying member method code with example usage: obj.embeddings(sentence) and returns: self