From 945620c89bc378aa8e520fd7ed6fd790bcef5d99 Mon Sep 17 00:00:00 2001 From: Tong Guo <779222056@qq.com> Date: Tue, 29 Oct 2019 11:33:24 +0800 Subject: [PATCH 1/2] Update col_predictor.py --- models/col_predictor.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/models/col_predictor.py b/models/col_predictor.py index 10e188f..f3d8904 100644 --- a/models/col_predictor.py +++ b/models/col_predictor.py @@ -14,15 +14,15 @@ def __init__(self, N_word, N_h, N_depth, gpu, use_hs): self.gpu = gpu self.use_hs = use_hs - self.q_lstm = nn.LSTM(input_size=N_word, hidden_size=N_h/2, + self.q_lstm = nn.LSTM(input_size=N_word, hidden_size=int(N_h/2), num_layers=N_depth, batch_first=True, dropout=0.3, bidirectional=True) - self.hs_lstm = nn.LSTM(input_size=N_word, hidden_size=N_h/2, + self.hs_lstm = nn.LSTM(input_size=N_word, hidden_size=int(N_h/2), num_layers=N_depth, batch_first=True, dropout=0.3, bidirectional=True) - self.col_lstm = nn.LSTM(input_size=N_word, hidden_size=N_h/2, + self.col_lstm = nn.LSTM(input_size=N_word, hidden_size=int(N_h/2), num_layers=N_depth, batch_first=True, dropout=0.3, bidirectional=True) @@ -122,7 +122,11 @@ def loss(self, score, truth): #loss for the column number truth_num = [len(t) - 1 for t in truth] # double check truth format and for test cases data = torch.from_numpy(np.array(truth_num)) - truth_num_var = Variable(data.cuda()) + data = torch._cast_Long(data) + if self.gpu: + truth_num_var = Variable(data.cuda()) + else: + truth_num_var = Variable(data) loss += self.CE(col_num_score, truth_num_var) #loss for the key words T = len(col_score[0]) @@ -139,7 +143,10 @@ def loss(self, score, truth): data = torch.from_numpy(truth_prob) # print("data {}".format(data)) # print("data {}".format(data.cuda())) - truth_var = Variable(data.cuda()) + if self.gpu: + truth_var = Variable(data.cuda()) + else: + truth_var = Variable(data) #loss += self.mlsml(col_score, truth_var) #loss += self.bce_logit(col_score, truth_var) # double check no sigmoid pred_prob = self.sigm(col_score) From b90a73b36afcc38d09121b6098e1dbba1f85e93f Mon Sep 17 00:00:00 2001 From: Tong Guo <779222056@qq.com> Date: Tue, 29 Oct 2019 15:00:48 +0800 Subject: [PATCH 2/2] Update col_predictor.py --- models/col_predictor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/col_predictor.py b/models/col_predictor.py index f3d8904..ce4e150 100644 --- a/models/col_predictor.py +++ b/models/col_predictor.py @@ -39,7 +39,7 @@ def __init__(self, N_word, N_h, N_depth, gpu, use_hs): self.col_out_hs = nn.Linear(N_h, N_h) self.col_out = nn.Sequential(nn.Tanh(), nn.Linear(N_h, 1)) - self.softmax = nn.Softmax() #dim=1 + self.softmax = nn.Softmax(dim=1) #dim=1 self.CE = nn.CrossEntropyLoss() self.log_softmax = nn.LogSoftmax() self.mlsml = nn.MultiLabelSoftMarginLoss()