| 101 | |
| 102 | |
| 103 | class MultiInferCNNModel(torch.nn.Module): |
| 104 | def __init__(self, gen_emb, domain_emb, args): |
| 105 | super(MultiInferCNNModel, self).__init__() |
| 106 | self.args = args |
| 107 | self.gen_embedding = torch.nn.Embedding(gen_emb.shape[0], gen_emb.shape[1]) |
| 108 | self.gen_embedding.weight.data.copy_(gen_emb) |
| 109 | self.gen_embedding.weight.requires_grad = False |
| 110 | |
| 111 | self.domain_embedding = torch.nn.Embedding(domain_emb.shape[0], domain_emb.shape[1]) |
| 112 | self.domain_embedding.weight.data.copy_(domain_emb) |
| 113 | self.domain_embedding.weight.requires_grad = False |
| 114 | |
| 115 | self.attention_layer = SelfAttention(args) |
| 116 | |
| 117 | self.conv1 = torch.nn.Conv1d(gen_emb.shape[1] + domain_emb.shape[1], 128, 5, padding=2) |
| 118 | self.conv2 = torch.nn.Conv1d(gen_emb.shape[1] + domain_emb.shape[1], 128, 3, padding=1) |
| 119 | self.dropout = torch.nn.Dropout(0.5) |
| 120 | |
| 121 | self.conv3 = torch.nn.Conv1d(256, 256, 5, padding=2) |
| 122 | self.conv4 = torch.nn.Conv1d(256, 256, 5, padding=2) |
| 123 | self.conv5 = torch.nn.Conv1d(256, 256, 5, padding=2) |
| 124 | |
| 125 | self.feature_linear = torch.nn.Linear(args.cnn_dim*2 + args.class_num*3, args.cnn_dim*2) |
| 126 | self.cls_linear = torch.nn.Linear(256*2, args.class_num) |
| 127 | |
| 128 | def multi_hops(self, features, lengths, mask, k): |
| 129 | '''generate mtraix mask''' |
| 130 | max_length = features.shape[1] |
| 131 | mask = mask[:, :max_length] |
| 132 | mask_a = mask.unsqueeze(1).expand([-1, max_length, -1]) |
| 133 | mask_b = mask.unsqueeze(2).expand([-1, -1, max_length]) |
| 134 | mask = mask_a * mask_b |
| 135 | mask = torch.triu(mask).unsqueeze(3).expand([-1, -1, -1, self.args.class_num]) |
| 136 | |
| 137 | '''save all logits''' |
| 138 | logits_list = [] |
| 139 | logits = self.cls_linear(features) |
| 140 | logits_list.append(logits) |
| 141 | |
| 142 | for i in range(k): |
| 143 | #probs = torch.softmax(logits, dim=3) |
| 144 | probs = logits |
| 145 | logits = probs * mask |
| 146 | |
| 147 | logits_a = torch.max(logits, dim=1)[0] |
| 148 | logits_b = torch.max(logits, dim=2)[0] |
| 149 | logits = torch.cat([logits_a.unsqueeze(3), logits_b.unsqueeze(3)], dim=3) |
| 150 | logits = torch.max(logits, dim=3)[0] |
| 151 | |
| 152 | logits = logits.unsqueeze(2).expand([-1,-1, max_length, -1]) |
| 153 | logits_T = logits.transpose(1, 2) |
| 154 | logits = torch.cat([logits, logits_T], dim=3) |
| 155 | |
| 156 | new_features = torch.cat([features, logits, probs], dim=3) |
| 157 | features = self.feature_linear(new_features) |
| 158 | logits = self.cls_linear(features) |
| 159 | logits_list.append(logits) |
| 160 | return logits_list |