text: # coding=utf-8
  # Copyright (C) 2019 Alibaba Group Holding Limited
  #
  # Licensed under the Apache License, Version 2.0 (the "License");
  # you may not use this file except in compliance with the License.
  # You may obtain a copy of the License at
  #
  #     http://www.apache.org/licenses/LICENSE-2.0
  #
  # Unless required by applicable law or agreed to in writing, software
  # distributed under the License is distributed on an "AS IS" BASIS,
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  # See the License for the specific language governing permissions and
  # limitations under the License.
  
  
  import torch
  import torch.nn as nn
  import torch.nn.functional as f
  
  
  class Embedding(nn.Module):
      def __init__(self, args):
          super().__init__()
          self.fix_embeddings = args.fix_embeddings
          self.embedding = nn.Embedding(args.num_vocab, args.embedding_dim, padding_idx=0)
          self.dropout = args.dropout
  
      def set_(self, value):
          self.embedding.weight.requires_grad = not self.fix_embeddings  # 是否使用固定词嵌入，关闭梯度
          self.embedding.load_state_dict({'weight': torch.tensor(value)})  # 载入现有词典
  
      def forward(self, x):
          x = self.embedding(x)
          x = f.dropout(x, self.dropout, self.training)
          return x
  