示例#1
0
    def _build_conversation_layer(self):
        self.register_buffer(
            'START', torch.tensor([self.start_token_idx], dtype=torch.long))
        self.conv_encoder = TransformerEncoder(
            n_heads=self.n_heads,
            n_layers=self.n_layers,
            embedding_size=self.token_emb_dim,
            ffn_size=self.ffn_size,
            vocabulary_size=self.vocab_size,
            embedding=self.token_embedding,
            dropout=self.dropout,
            attention_dropout=self.attention_dropout,
            relu_dropout=self.relu_dropout,
            padding_idx=self.pad_token_idx,
            learn_positional_embeddings=self.learn_positional_embeddings,
            embeddings_scale=self.embeddings_scale,
            reduction=self.reduction,
            n_positions=self.n_positions,
        )

        self.conv_entity_norm = nn.Linear(self.kg_emb_dim, self.ffn_size)
        self.conv_entity_attn_norm = nn.Linear(self.kg_emb_dim, self.ffn_size)
        self.conv_word_norm = nn.Linear(self.kg_emb_dim, self.ffn_size)
        self.conv_word_attn_norm = nn.Linear(self.kg_emb_dim, self.ffn_size)

        self.copy_norm = nn.Linear(self.ffn_size * 3, self.token_emb_dim)
        self.copy_output = nn.Linear(self.token_emb_dim, self.vocab_size)
        copy_mask = np.load(os.path.join(self.dpath,
                                         "copy_mask.npy")).astype(bool)
        if self.replace_token:
            if self.replace_token_idx < len(copy_mask):
                copy_mask[self.replace_token_idx] = False
            else:
                copy_mask = np.insert(copy_mask, len(copy_mask), False)
        self.copy_mask = torch.as_tensor(copy_mask).to(self.device)

        self.conv_decoder = TransformerDecoderKG(
            self.n_heads,
            self.n_layers,
            self.token_emb_dim,
            self.ffn_size,
            self.vocab_size,
            embedding=self.token_embedding,
            dropout=self.dropout,
            attention_dropout=self.attention_dropout,
            relu_dropout=self.relu_dropout,
            embeddings_scale=self.embeddings_scale,
            learn_positional_embeddings=self.learn_positional_embeddings,
            padding_idx=self.pad_token_idx,
            n_positions=self.n_positions)
        self.conv_loss = nn.CrossEntropyLoss(ignore_index=self.pad_token_idx)

        logger.debug('[Finish build conv layer]')
示例#2
0
文件: kbrd.py 项目: xiyou1024/CRSLab
 def _build_conversation_layer(self):
     self.register_buffer(
         'START', torch.tensor([self.start_token_idx], dtype=torch.long))
     self.dialog_encoder = TransformerEncoder(
         self.n_heads, self.n_layers, self.token_emb_dim, self.ffn_size,
         self.vocab_size, self.token_embedding, self.dropout,
         self.attention_dropout, self.relu_dropout, self.pad_token_idx,
         self.learn_positional_embeddings, self.embeddings_scale,
         self.reduction, self.n_positions)
     self.decoder = TransformerDecoder(
         self.n_heads, self.n_layers, self.token_emb_dim, self.ffn_size,
         self.vocab_size, self.token_embedding, self.dropout,
         self.attention_dropout, self.relu_dropout, self.embeddings_scale,
         self.learn_positional_embeddings, self.pad_token_idx,
         self.n_positions)
     self.user_proj_1 = nn.Linear(self.user_emb_dim, self.user_proj_dim)
     self.user_proj_2 = nn.Linear(self.user_proj_dim, self.vocab_size)
     self.conv_loss = nn.CrossEntropyLoss(ignore_index=self.pad_token_idx)
     logger.debug('[Build conversation layer]')
示例#3
0
    def _build_conversation_layer(self):
        self.register_buffer(
            'START', torch.tensor([self.start_token_idx], dtype=torch.long))
        self.conv_encoder = TransformerEncoder(
            n_heads=self.n_heads,
            n_layers=self.n_layers,
            embedding_size=self.token_emb_dim,
            ffn_size=self.ffn_size,
            vocabulary_size=self.vocab_size,
            embedding=self.token_embedding,
            dropout=self.dropout,
            attention_dropout=self.attention_dropout,
            relu_dropout=self.relu_dropout,
            padding_idx=self.pad_token_idx,
            learn_positional_embeddings=self.learn_positional_embeddings,
            embeddings_scale=self.embeddings_scale,
            reduction=self.reduction,
            n_positions=self.n_positions,
        )

        self.conv_decoder = TransformerDecoder(
            self.n_heads,
            self.n_layers,
            self.token_emb_dim,
            self.ffn_size,
            self.vocab_size,
            embedding=self.token_embedding,
            dropout=self.dropout,
            attention_dropout=self.attention_dropout,
            relu_dropout=self.relu_dropout,
            embeddings_scale=self.embeddings_scale,
            learn_positional_embeddings=self.learn_positional_embeddings,
            padding_idx=self.pad_token_idx,
            n_positions=self.n_positions)

        self.conv_loss = nn.CrossEntropyLoss(ignore_index=self.pad_token_idx)

        logger.debug('[Finish build conv layer]')