docstr: Args:
      token2id: dictionary of token->idx mapping
      max_length: max length of sentence, used for postion embedding
      hidden_size: hidden size
      word_size: size of word embedding
      position_size: size of position embedding
      blank_padding: padding for CNN
      word2vec: pretrained word2vec numpy
docstr_tok: ["Args",":","token2id",":","dictionary","of","token-",">","idx","mapping","max_length",":","max","length","of","sentence",",","used","for","postion","embedding","hidden_size",":","hidden","size","word_size",":","size","of","word","embedding","position_size",":","size","of","position","embedding","blank_padding",":","padding","for","CNN","word2vec",":","pretrained","word2vec","numpy"]
code: def __init__(
      self,
      token2id,
      max_length=128,
      hidden_size=230,
      word_size=50,
      position_size=5,
      blank_padding=True,
      word2vec=None,
      mask_entity=False,
  ):
      """
      Args:
          token2id: dictionary of token->idx mapping
          max_length: max length of sentence, used for postion embedding
          hidden_size: hidden size
          word_size: size of word embedding
          position_size: size of position embedding
          blank_padding: padding for CNN
          word2vec: pretrained word2vec numpy
      """
      # Hyperparameters
      super().__init__()
  
      self.token2id = token2id
      self.max_length = max_length
      self.num_token = len(token2id)
      self.num_position = max_length * 2
      self.mask_entity = mask_entity
  
      if word2vec is None:
          self.word_size = word_size
      else:
          self.word_size = word2vec.shape[-1]
  
      self.position_size = position_size
      self.hidden_size = hidden_size
      self.input_size = word_size + position_size * 2
      self.blank_padding = blank_padding
  
      if not "[UNK]" in self.token2id:
          self.token2id["[UNK]"] = len(self.token2id)
          self.num_token += 1
      if not "[PAD]" in self.token2id:
          self.token2id["[PAD]"] = len(self.token2id)
          self.num_token += 1
  
      # Word embedding
      self.word_embedding = nn.Embedding(self.num_token, self.word_size)
      if word2vec is not None:
          logging.info("Initializing word embedding with word2vec.")
          word2vec = torch.from_numpy(word2vec)
          if self.num_token == len(word2vec) + 2:
              unk = torch.randn(1, self.word_size) / math.sqrt(
                  self.word_size
              )
              blk = torch.zeros(1, self.word_size)
              self.word_embedding.weight.data.copy_(
                  torch.cat([word2vec, unk, blk], 0)
              )
          else:
              self.word_embedding.weight.data.copy_(word2vec)
  
      # Position Embedding
      self.pos1_embedding = nn.Embedding(
          2 * max_length, self.position_size, padding_idx=0
      )
      self.pos2_embedding = nn.Embedding(
          2 * max_length, self.position_size, padding_idx=0
      )
      self.tokenizer = WordTokenizer(
          vocab=self.token2id, unk_token="[UNK]"
      )
  
file_path: OpenNRE/opennre/encoder/base_encoder.py
imports: import math, logging
  import torch
  import torch.nn
  import torch.nn.functional
  import numpy
  from ..tokenization import WordTokenizer
label_desc: Use image node_id 1 to create a new BaseEncoder object from inherited base classes: nn with example: obj = BaseEncoder(token2id, max_length, hidden_size, word_size, position_size, blank_padding, word2vec, mask_entity)