def forward(self, x, xr): ''' the forward function :param x: the first input :param xr: the second input :return: the output ''' # convolution x = self.Conv_classifer(x) if self.maxpooling_dir: # global average polling of cnn features map in direction of channels x = F.adaptive_avg_pool2d(x, (x.size()[1], 1)) else: # global max polling of cnn features map in direction of words x = F.adaptive_max_pool2d(x, (x.size()[2], 1)) x = x.view(x.size()[0], -1) if self.padding: x = nn.ConstantPad1d((0, self.w_size - x.size()[1]), 0)(x) if self.xr: x = torch.cat((x, xr), 1) x = self.FC_classifier(x) return x
def forward(self, x, xr): activity_layers, _ = self.bert_model(x) if not isinstance(activity_layers, torch.Tensor): activity_layers = activity_layers[-1] activity_layers = activity_layers.permute(0, 2, 1) y1 = self.cnn1(activity_layers[:, :, :], xr) activity_layers = torch.stack(activity_layers) activity_layers = activity_layers.permute(1, 3, 2, 0) activity_layers = activity_layers[:, :, 0, :] activity_layers = activity_layers.new_tensor() y2 = self.cnn2(activity_layers[:, :, :], xr) y = self.fc(torch.cat([y1, y2], dim=0)) return y
def forward(self, x, xr): conv1 = self.Conv_classifer(x) # x = F.adaptive_avg_pool2d(x, (x.size()[1], 1)) x1 = F.adaptive_max_pool2d(conv1, (conv1.size()[1], 1)) x1 = x1.view(x1.size()[0], -1) conv2 = self.Conv_classifer2(conv1) x2 = F.adaptive_max_pool2d(conv2, (conv2.size()[1], 1)) x2 = x2.view(x2.size()[0], -1) x = torch.cat((x1, x2, xr), 1) x = self.FC_classifier(x) return x
def forward(self, x, xr): activity_layers, _ = self.bert_model(x) a1, e1, a2, e2, a3 = [], [], [], [], [] for i in range(xr.size()[0]): e1start, e1end, e2start, e2end = xr[i, 0], xr[i, 1], xr[i, 2], xr[i, 3] a1.append(activity_layers[i, :e1start + 1, :]) e1.append(activity_layers[i, e1start:e1end + 1, :]) a2.append(activity_layers[i, e1end - 1:e2start + 1, :]) e2.append(activity_layers[i, e2start:e2end + 1, :]) a3.append(activity_layers[i, e2end:, :]) if a2[-1].size()[0] == 0: a2[-1] = torch.FloatTensor([[0.] * (768 * 1)] * 3).cuda() a1 = torch.nn.utils.rnn.pad_sequence(a1) e1 = torch.nn.utils.rnn.pad_sequence(e1) a2 = torch.nn.utils.rnn.pad_sequence(a2) e2 = torch.nn.utils.rnn.pad_sequence(e2) a3 = torch.nn.utils.rnn.pad_sequence(a3) a1 = a1.permute(1, 2, 0) e1 = e1.permute(1, 2, 0) a2 = a2.permute(1, 2, 0) e2 = e2.permute(1, 2, 0) a3 = a3.permute(1, 2, 0) y1 = self.cnn1(a1, xr) y2 = self.cnn2(e1, xr) y3 = self.cnn3(a2, xr) y4 = self.cnn4(e2, xr) y5 = self.cnn5(a3, xr) y = self.fc(torch.cat([y1, y2, y3, y4, y5], dim=1)) return y
def forward(self, x, xr): # print(xr) activity_layers, _ = self.bert_model(x) #activity_layers, _, a = self.bert_model(x) if not isinstance(activity_layers, torch.Tensor): activity_layers = activity_layers[-1] # activity_layers=activity_layers.permute(0,2,1) # print(activity_layers.size()) #activity_layers = a[-1] # print(out.size()) a1, e1, a2, e2, a3 = [], [], [], [], [] # activity_layers=torch.cat([a[-1],a[-2]],dim=2) for i in range(xr.size()[0]): e1start, e1end, e2start, e2end = xr[i, 0], xr[i, 1], xr[i, 2], xr[i, 3] # print(e1start,e1end,e2start,e2end) a1.append(activity_layers[i, :e1start + 1, :]) e1.append(activity_layers[i, e1start:e1end + 1, :]) a2.append(activity_layers[i, e1end - 1:e2start + 1, :]) e2.append(activity_layers[i, e2start:e2end + 1, :]) a3.append(activity_layers[i, e2end:, :]) if a2[-1].size()[0] == 0: a2[-1] = torch.FloatTensor([[0.] * (768 * 1)] * 3).cuda() #a2[-1] = torch.FloatTensor([[0.] * (768 * 1)] * 3) ''' print(a1[-1].size()) print(e1[-1].size()) print(a2[-1].size()) print(e2[-1].size()) print(a3[-1].size())''' # activity_layers[i,:,:e1]=activity_layers[i,:,0:e1]*0 # activity_layers[i,:,e2:]=activity_layers[i,:,e2:]*0 # print(activity_layers[i,:,e1:e2].size()) a1 = torch.nn.utils.rnn.pad_sequence(a1) e1 = torch.nn.utils.rnn.pad_sequence(e1) a2 = torch.nn.utils.rnn.pad_sequence(a2) e2 = torch.nn.utils.rnn.pad_sequence(e2) a3 = torch.nn.utils.rnn.pad_sequence(a3) # e=torch.cat(e,dim=0) # print(e.size()) # activity_layers=activity_layers.permute(0,2,1) a1 = a1.permute(1, 2, 0) e1 = e1.permute(1, 2, 0) a2 = a2.permute(1, 2, 0) e2 = e2.permute(1, 2, 0) a3 = a3.permute(1, 2, 0) # activity_layers=torch.cat([activity_layers[xr[i,0],xr[i,1]] for i in range(32)]) # print(activity_layers.size()) y1 = self.cnn1(a1, xr) y2 = self.cnn2(e1, xr) y3 = self.cnn3(a2, xr) y4 = self.cnn4(e2, xr) y5 = self.cnn5(a3, xr) y = self.fc(torch.cat([y1, y2, y3, y4, y5], dim=1)) # y=self.cnn(activity_layers[:,:,:].detach(),out) return y