class_docstr: 用于读取预训练的 embedding, 读取结果可直接载入为模型参数。
class_docstr_tok: ["\u7528\u4e8e\u8bfb\u53d6\u9884\u8bad\u7ec3\u7684","embedding",",","\u8bfb\u53d6\u7ed3\u679c\u53ef\u76f4\u63a5\u8f7d\u5165\u4e3a\u6a21\u578b\u53c2\u6570\u3002"]
docstr: 从 ``embed_filepath`` 这个预训练的词向量中抽取出 ``vocab`` 这个词表的词的 embedding。 :class:`EmbedLoader` 将自动判断 ``embed_filepath``
  是 **word2vec** （第一行只有两个元素） 还是 **glove** 格式的数据。
  
  :param embed_filepath: 预训练的 embedding 的路径。
  :param vocab: 词表 :class:`~fastNLP.core.Vocabulary` 类型，读取出现在 ``vocab`` 中的词的 embedding。
      没有出现在 ``vocab`` 中的词的 embedding 将通过找到的词的 embedding 的 *正态分布* 采样出来，以使得整个 Embedding 是同分布的。
  :param dtype: 读出的 embedding 的类型
  :param padding: 词表中 *padding* 的 token
  :param unknown: 词表中 *unknown* 的 token
  :param normalize: 是否将每个 vector 归一化到 norm 为 1
  :param error: 可以为以下值之一： ``['ignore', 'strict']`` 。如果为  ``ignore`` ，错误将自动跳过；如果是 ``strict`` ，错误将抛出。
      这里主要可能出错的地方在于词表有空行或者词表出现了维度不一致。
  :param init_method: 用于初始化 embedding 的函数。该函数接受一个 :class:`numpy.ndarray` 类型，返回 :class:`numpy.ndarray`。
  :return: 返回类型为 :class:`numpy.ndarray`，形状为 ``[len(vocab), dimension]``，其中 *dimension*由预训练的 embedding 决定。
docstr_tok: ["\u4ece","``","embed_filepath","``","\u8fd9\u4e2a\u9884\u8bad\u7ec3\u7684\u8bcd\u5411\u91cf\u4e2d\u62bd\u53d6\u51fa","``","vocab","``","\u8fd9\u4e2a\u8bcd\u8868\u7684\u8bcd\u7684","embedding\u3002",":","class",":","`","EmbedLoader","`","\u5c06\u81ea\u52a8\u5224\u65ad","``","embed_filepath","``","\u662f","*","*","word2vec","*","*","\uff08\u7b2c\u4e00\u884c\u53ea\u6709\u4e24\u4e2a\u5143\u7d20\uff09","\u8fd8\u662f","*","*","glove","*","*","\u683c\u5f0f\u7684\u6570\u636e\u3002",":","param","embed_filepath",":","\u9884\u8bad\u7ec3\u7684","embedding","\u7684\u8def\u5f84\u3002",":","param","vocab",":","\u8bcd\u8868",":","class",":","`","~fastNLP.core.Vocabulary","`","\u7c7b\u578b\uff0c\u8bfb\u53d6\u51fa\u73b0\u5728","``","vocab","``","\u4e2d\u7684\u8bcd\u7684","embedding\u3002","\u6ca1\u6709\u51fa\u73b0\u5728","``","vocab","``","\u4e2d\u7684\u8bcd\u7684","embedding","\u5c06\u901a\u8fc7\u627e\u5230\u7684\u8bcd\u7684","embedding","\u7684","*","\u6b63\u6001\u5206\u5e03","*","\u91c7\u6837\u51fa\u6765\uff0c\u4ee5\u4f7f\u5f97\u6574\u4e2a","Embedding","\u662f\u540c\u5206\u5e03\u7684\u3002",":","param","dtype",":","\u8bfb\u51fa\u7684","embedding","\u7684\u7c7b\u578b",":","param","padding",":","\u8bcd\u8868\u4e2d","*","padding","*","\u7684","token",":","param","unknown",":","\u8bcd\u8868\u4e2d","*","unknown","*","\u7684","token",":","param","normalize",":","\u662f\u5426\u5c06\u6bcf\u4e2a","vector","\u5f52\u4e00\u5316\u5230","norm","\u4e3a","1",":","param","error",":","\u53ef\u4ee5\u4e3a\u4ee5\u4e0b\u503c\u4e4b\u4e00\uff1a","``","[","'ignore","'",",","'strict","'","]","``","\u3002\u5982\u679c\u4e3a","``","ignore","``","\uff0c\u9519\u8bef\u5c06\u81ea\u52a8\u8df3\u8fc7\uff1b\u5982\u679c\u662f","``","strict","``","\uff0c\u9519\u8bef\u5c06\u629b\u51fa\u3002","\u8fd9\u91cc\u4e3b\u8981\u53ef\u80fd\u51fa\u9519\u7684\u5730\u65b9\u5728\u4e8e\u8bcd\u8868\u6709\u7a7a\u884c\u6216\u8005\u8bcd\u8868\u51fa\u73b0\u4e86\u7ef4\u5ea6\u4e0d\u4e00\u81f4\u3002",":","param","init_method",":","\u7528\u4e8e\u521d\u59cb\u5316","embedding","\u7684\u51fd\u6570\u3002\u8be5\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a",":","class",":","`","numpy.ndarray","`","\u7c7b\u578b\uff0c\u8fd4\u56de",":","class",":","`","numpy.ndarray","`","\u3002",":","return",":","\u8fd4\u56de\u7c7b\u578b\u4e3a",":","class",":","`","numpy.ndarray","`","\uff0c\u5f62\u72b6\u4e3a","``","[","len","(","vocab",")",",","dimension","]","``","\uff0c\u5176\u4e2d","*","dimension","*","\u7531\u9884\u8bad\u7ec3\u7684","embedding","\u51b3\u5b9a\u3002"]
code: def load_with_vocab(
      embed_filepath: str,
      vocab,
      dtype=np.float32,
      padding: str = "<pad>",
      unknown: str = "<unk>",
      normalize: bool = True,
      error: str = "ignore",
      init_method: Callable = None,
  ):
      r"""
      从 ``embed_filepath`` 这个预训练的词向量中抽取出 ``vocab`` 这个词表的词的 embedding。 :class:`EmbedLoader` 将自动判断 ``embed_filepath``
      是 **word2vec** （第一行只有两个元素） 还是 **glove** 格式的数据。
  
      :param embed_filepath: 预训练的 embedding 的路径。
      :param vocab: 词表 :class:`~fastNLP.core.Vocabulary` 类型，读取出现在 ``vocab`` 中的词的 embedding。
          没有出现在 ``vocab`` 中的词的 embedding 将通过找到的词的 embedding 的 *正态分布* 采样出来，以使得整个 Embedding 是同分布的。
      :param dtype: 读出的 embedding 的类型
      :param padding: 词表中 *padding* 的 token
      :param unknown: 词表中 *unknown* 的 token
      :param normalize: 是否将每个 vector 归一化到 norm 为 1
      :param error: 可以为以下值之一： ``['ignore', 'strict']`` 。如果为  ``ignore`` ，错误将自动跳过；如果是 ``strict`` ，错误将抛出。
          这里主要可能出错的地方在于词表有空行或者词表出现了维度不一致。
      :param init_method: 用于初始化 embedding 的函数。该函数接受一个 :class:`numpy.ndarray` 类型，返回 :class:`numpy.ndarray`。
      :return: 返回类型为 :class:`numpy.ndarray`，形状为 ``[len(vocab), dimension]``，其中 *dimension*由预训练的 embedding 决定。
      """
      assert isinstance(
          vocab, Vocabulary
      ), "Only fastNLP.Vocabulary is supported."
      if not os.path.exists(embed_filepath):
          raise FileNotFoundError(
              "`{}` does not exist.".format(embed_filepath)
          )
      with open(embed_filepath, "r", encoding="utf-8") as f:
          hit_flags = np.zeros(len(vocab), dtype=bool)
          line = f.readline().strip()
          parts = line.split()
          start_idx = 0
          if len(parts) == 2:
              dim = int(parts[1])
              start_idx += 1
          else:
              dim = len(parts) - 1
              f.seek(0)
          matrix = np.random.randn(len(vocab), dim).astype(dtype)
          if init_method:
              matrix = init_method(matrix)
          for idx, line in enumerate(f, start_idx):
              try:
                  parts = line.strip().split()
                  word = "".join(parts[:-dim])
                  nums = parts[-dim:]
                  # 对齐unk与pad
                  if word == padding and vocab.padding is not None:
                      word = vocab.padding
                  elif word == unknown and vocab.unknown is not None:
                      word = vocab.unknown
                  if word in vocab:
                      index = vocab.to_index(word)
                      matrix[index] = np.fromstring(
                          " ".join(nums),
                          sep=" ",
                          dtype=dtype,
                          count=dim,
                      )
                      hit_flags[index] = True
              except Exception as e:
                  if error == "ignore":
                      logger.warning(
                          "Error occurred at the {} line.".format(idx)
                      )
                  else:
                      logging.error(
                          "Error occurred at the {} line.".format(idx)
                      )
                      raise e
          total_hits = sum(hit_flags)
          logging.info(
              "Found {} out of {} words in the pre-training embedding.".format(
                  total_hits, len(vocab)
              )
          )
          if init_method is None:
              found_vectors = matrix[hit_flags]
              if len(found_vectors) != 0:
                  mean = np.mean(found_vectors, axis=0, keepdims=True)
                  std = np.std(found_vectors, axis=0, keepdims=True)
                  unfound_vec_num = len(vocab) - total_hits
                  r_vecs = (
                      np.random.randn(unfound_vec_num, dim).astype(
                          dtype
                      )
                      * std
                      + mean
                  )
                  matrix[hit_flags == False] = r_vecs
  
          if normalize:
              matrix /= np.linalg.norm(matrix, axis=1, keepdims=True)
  
          return matrix
  
file_path: fastNLP/fastNLP/io/embed_loader.py
imports: import logging
  import os
  from typing import Callable
  import numpy
  from fastNLP.core.utils.utils import Option
  from fastNLP.core.vocabulary import Vocabulary
  from fastNLP.core.log import logger
label_desc: Use image node_id 2 for calling the EmbedLoader obj's underlying member method code with example usage: obj.load_with_vocab(embed_filepath, vocab, dtype, padding, unknown, normalize, error, init_method) and returns: matrix