| 134 | return output |
| 135 | |
| 136 | class bComments(torch.nn.Module): |
| 137 | def __init__(self,bert_model,fea_dim): |
| 138 | super(bComments, self).__init__() |
| 139 | self.comment_dim = 768 |
| 140 | self.bert = BertModel.from_pretrained(bert_model).requires_grad_(False) |
| 141 | self.attention = Attention(dim=128,heads=4) |
| 142 | self.linear_comment = nn.Sequential(torch.nn.Linear(self.comment_dim, fea_dim),torch.nn.ReLU()) |
| 143 | self.classifier = nn.Linear(fea_dim,2) |
| 144 | |
| 145 | def forward(self, **kwargs): |
| 146 | comments_inputid = kwargs['comments_inputid'] |
| 147 | comments_mask=kwargs['comments_mask'] |
| 148 | comments_feature=[] |
| 149 | for i in range(comments_inputid.shape[0]): |
| 150 | bert_fea=self.bert(comments_inputid[i], attention_mask=comments_mask[i])[1] |
| 151 | comments_feature.append(bert_fea) |
| 152 | comments_feature=torch.stack(comments_feature) |
| 153 | fea_comments=self.linear_comment(comments_feature) |
| 154 | print (fea_comments.shape) |
| 155 | fea_comments = self.attention(fea_comments) |
| 156 | fea_comments = torch.mean(fea_comments, -2) |
| 157 | output = self.classifier(fea_comments) |
| 158 | return output |
| 159 | |
| 160 | |