def __init__(self, size: int, self_attn: torch.nn.Module,
              feed_forward: torch.nn.Module, dropout: float) -> None:
     super().__init__()
     self.self_attn = self_attn
     self.feed_forward = feed_forward
     self.sublayer = util.clone(SublayerConnection(size, dropout), 2)
     self.size = size
示例#2
0
 def __init__(self,
              vocab: Vocabulary,
              encoder: Seq2SeqEncoder,
              yingshe_encoder: Seq2SeqEncoder,
              attention_decoder: MatrixAttention,
              regularizer: Optional[RegularizerApplicator] = None) -> None:
     super().__init__(vocab, regularizer)
     self.encoder = encoder
     self.decoder_start_list = clone(attention_decoder,
                                     len(defaults.relation2id))
     self.decoder_end_list = clone(attention_decoder,
                                   len(defaults.relation2id))
     self.yingshe_encoder_list = clone(yingshe_encoder, 2)
     self.valid_metric = SanyuanzuMetric()
     #TODO()如果验证还是很低,则考虑添加一个新的metric来查看预测的准确率
     self.flag = True
示例#3
0
 def __init__(
     self, layer: torch.nn.Module, num_layers: int, return_all_layers: bool = False
 ) -> None:
     super().__init__()
     self.layers = util.clone(layer, num_layers)
     self.norm = LayerNorm(layer.size)
     self.return_all_layers = return_all_layers
 def __init__(self, num_heads: int, input_dim: int) -> None:
     super(MultiHeadedAttentionWithAttention, self).__init__()
     assert input_dim % num_heads == 0, "input_dim must be a multiple of num_heads"
     # assume d_v equals d_k
     self.d_k = input_dim // num_heads
     self.num_heads = num_heads
     # These linear layers project from d_model to h*d_k
     self.linears = util.clone(nn.Linear(input_dim, input_dim, bias=False),
                               4)
示例#5
0
 def __init__(
         self,
         vocab: Vocabulary,
         encoder: Seq2SeqEncoder,
         one_decoder: Seq2SeqEncoder,
         many_decoder: Seq2SeqEncoder,
         span_extractor: SpanExtractor,  # to determine
         mode: str,
         regularizer: Optional[RegularizerApplicator] = None) -> None:
     super().__init__(vocab, regularizer)
     self.encoder = encoder
     self.one_decoder_list = clone(one_decoder, 2)
     self.many_decoder_list = clone(many_decoder, 2)
     self.span_extractor = span_extractor
     self.valid_metric = SanyuanzuMetric()
     assert mode in ['s2po', 'o2ps']
     self.mode = mode
     self.flag = True
示例#6
0
 def __init__(self, num_heads: int, input_dim: int, dropout: float = 0.1) -> None:
     super().__init__()
     assert input_dim % num_heads == 0, "input_dim must be a multiple of num_heads"
     # We assume d_v always equals d_k
     self.d_k = input_dim // num_heads
     self.num_heads = num_heads
     # These linear layers are
     #  [query_projection, key_projection, value_projection, concatenated_heads_projection]
     self.linears = util.clone(torch.nn.Linear(input_dim, input_dim), 4)
     self.dropout = torch.nn.Dropout(p=dropout)
 def __init__(self, num_heads: int, input_dim: int, dropout: float = 0.1) -> None:
     super().__init__()
     assert input_dim % num_heads == 0, "input_dim must be a multiple of num_heads"
     # We assume d_v always equals d_k
     self.d_k = input_dim // num_heads
     self.num_heads = num_heads
     # These linear layers are
     #  [query_projection, key_projection, value_projection, concatenated_heads_projection]
     self.linears = util.clone(torch.nn.Linear(input_dim, input_dim), 4)
     self.dropout = torch.nn.Dropout(p=dropout)
 def __init__(self,
              size: int,
              self_attn: torch.nn.Module,
              feed_forward: torch.nn.Module,
              dropout: float) -> None:
     super().__init__()
     self.self_attn = self_attn
     self.feed_forward = feed_forward
     self.sublayer = util.clone(SublayerConnection(size, dropout), 2)
     self.size = size
示例#9
0
 def __init__(
         self,
         vocab: Vocabulary,
         encoder: Seq2SeqEncoder,
         first_decoder: Seq2SeqEncoder,
         second_decoder: Seq2SeqEncoder,
         third_decoder: Seq2VecEncoder,
         span_extractor: SpanExtractor,  # to determine
         mode: str,
         regularizer: Optional[RegularizerApplicator] = None) -> None:
     super().__init__(vocab, regularizer)
     self.encoder = encoder
     self.first_decoder_list = clone(first_decoder, 2)
     self.second_decoder_list = clone(second_decoder, 2)
     self.thrid_decoder = third_decoder  # 仅作关系判别
     self.span_extractor_list = clone(span_extractor, 2)
     self.valid_metric = SanyuanzuMetric()
     assert mode in ['sop', 'osp']
     self.mode = mode
     self.flag = True
示例#10
0
 def __init__(self, self_attn: MultiHeadedAttentionWithAttention,
              context_attn: MultiHeadedAttentionWithAttention,
              query_attn: MultiHeadedAttentionWithAttention,
              feed_forward: F, d_model: int, dropout_rate: float) -> None:
     super(RWDecoderLayer, self).__init__()
     self.self_attn = self_attn
     self.context_attn = context_attn
     self.query_attn = query_attn
     self.linear = nn.Linear(2 * d_model, d_model, bias=False)
     self.feed_forward = feed_forward
     self.norms = util.clone(nn.LayerNorm(d_model, eps=1e-6), 3)
     self.dropout = nn.Dropout(dropout_rate)
 def __init__(
     self,
     size: int,
     self_attn: MultiHeadedAttention,
     src_attn: MultiHeadedAttention,
     feed_forward: F,
     dropout: float,
 ) -> None:
     super().__init__()
     self.size = size
     self.self_attn = self_attn
     self.src_attn = src_attn
     self.feed_forward = feed_forward
     self.sublayer = nn_util.clone(SublayerConnection(size, dropout), 3)
示例#12
0
 def __init__(self,
              num_heads: int,
              input_dim: int,
              width: int,
              left_to_right: bool,
              dropout: float = 0.1) -> None:
     super().__init__()
     assert input_dim % num_heads == 0, "input_dim must be a multiple of num_heads"
     # We assume d_v always equals d_k
     self.d_k = input_dim // num_heads
     self.num_heads = num_heads
     self.width = width
     self.left_to_right = left_to_right
     # These linear layers are
     #  [query_projection, key_projection, value_projection, concatenated_heads_projection]
     self.linears = clone(torch.nn.Linear(input_dim, input_dim), 4)
     self.rel_pos_embeddings = torch.nn.Parameter(
         torch.randn(num_heads, width + 1, self.d_k) / np.sqrt(self.d_k))
     self.dropout = torch.nn.Dropout(p=dropout)
示例#13
0
    def __init__(self, word_embeddings: TextFieldEmbedder,
                 encoder: Seq2VecEncoder, vocab: Vocabulary) -> None:
        # We have to pass the vocabulary to the constructor.
        super().__init__(vocab)
        self.word_embeddings = word_embeddings

        # Our model has different encoders for each of the fields (passage,
        # answer and question). These could theoretically be different for each
        # field, but for now we're using the same. Hence, we clone the provided
        # encoder.
        self.p_encoder, self.q_encoder, self.a_encoder = util.clone(encoder, 3)

        # We're using a hidden layer to build the output from each encoder.
        # As this can't really change, it's not passed as input.
        # The size has to be the size of concatenating the encoder outputs,
        # since that's how we're combining them in the computation. As they're
        # the same, just multiply the first encoder output by 3.
        # The output of the model (which is the output of this layer) has to
        # have size equal to the number of classes.
        hidden_dim = self.p_encoder.get_output_dim() * 4
        self.hidden2logit = torch.nn.Linear(
            in_features=hidden_dim, out_features=vocab.get_vocab_size('label'))
 def __init__(self, layer: nn.Module, num_layers: int) -> None:
     super().__init__()
     self.layers = nn_util.clone(layer, num_layers)
     self.norm = LayerNorm(layer.size)
 def __init__(self, layer: torch.nn.Module, num_layers: int) -> None:
     super().__init__()
     self._layers = util.clone(layer, num_layers)
     self._norm = LayerNorm(layer._size)
示例#16
0
 def __init__(self, layer: nn.Module, num_layers: int) -> None:
     super(RWDecoder, self).__init__()
     self.layers = util.clone(layer, num_layers)
 def __init__(self, layer: torch.nn.Module, num_layers: int, return_all_layers: bool = False) -> None:
     super().__init__()
     self.layers = util.clone(layer, num_layers)
     self.norm = LayerNorm(layer.size)
     self.return_all_layers = return_all_layers