Ejemplo n.º 1
0
    def __init__(self, num_classes, hparams):
        super(G2PTransformer, self).__init__()
        self.bert = BertModel.from_pretrained('./bert/bert-base-chinese')
        self.poly_phoneme_classifier = Poly_Phoneme_Classifier(hparams)

        self.linear = nn.Linear(1024, num_classes)

        self.bert_embedding_features_dim = 768
        self.transformer_embedding_features_dim = 1324
        self.embedding_features_dim = 1024
        self.select_model_hidden_dim = 512

        self.linear_pre = nn.Sequential(
            nn.Linear(self.bert_embedding_features_dim, self.select_model_hidden_dim),
            parse_nk.LayerNormalization(self.select_model_hidden_dim),
            nn.ReLU(),
            nn.Linear(self.select_model_hidden_dim, self.transformer_embedding_features_dim),
            )

        self.linear_aft = nn.Sequential(
            nn.Linear(self.embedding_features_dim, self.select_model_hidden_dim),
            parse_nk.LayerNormalization(self.select_model_hidden_dim),
            nn.ReLU(),
            nn.Linear(self.select_model_hidden_dim, num_classes),
            )
Ejemplo n.º 2
0
class G2PTransformer(nn.Module):
    def __init__(self, num_classes):
        super(G2PTransformer, self).__init__()
        self.bert = BertModel.from_pretrained('./bert/bert-base-chinese')
        self.poly_phoneme_classifier = Poly_Phoneme_Classifier(hparams)

        self.linear = nn.Linear(1024, num_classes)

        self.bert_embedding_features_dim = 768
        self.transformer_embedding_features_dim = 1324
        self.embedding_features_dim = 1024
        self.select_model_hidden_dim = 512

        self.linear_pre = nn.Sequential(
            nn.Linear(self.bert_embedding_features_dim,
                      self.select_model_hidden_dim),
            parse_nk.LayerNormalization(self.select_model_hidden_dim),
            nn.ReLU(),
            nn.Linear(self.select_model_hidden_dim,
                      self.transformer_embedding_features_dim),
        )

        self.linear_aft = nn.Sequential(
            nn.Linear(self.embedding_features_dim,
                      self.select_model_hidden_dim),
            parse_nk.LayerNormalization(self.select_model_hidden_dim),
            nn.ReLU(),
            nn.Linear(self.select_model_hidden_dim, num_classes),
        )

    def forward(self, input_ids, attention_mask, poly_ids):
        batch_size = input_ids.size(0)
        inputs = {"input_ids": input_ids, "attention_mask": attention_mask}
        outputs = self.bert(**inputs)
        hidden = outputs[0][0]
        hidden = self.linear_pre(hidden)
        transformer_output = self.poly_phoneme_classifier.forward_train_polyphonic(
            hidden)
        poly_hidden = transformer_output[torch.arange(batch_size), poly_ids]
        logits = self.linear_aft(poly_hidden)
        return logits