From 757925aff04094c3f01117438452b5f1cca91b28 Mon Sep 17 00:00:00 2001 From: Tong Guo <779222056@qq.com> Date: Tue, 29 Oct 2019 11:37:19 +0800 Subject: [PATCH 1/3] Update keyword_predictor.py --- models/keyword_predictor.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/models/keyword_predictor.py b/models/keyword_predictor.py index 4040924..bbff2c2 100644 --- a/models/keyword_predictor.py +++ b/models/keyword_predictor.py @@ -16,15 +16,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.kw_lstm = nn.LSTM(input_size=N_word, hidden_size=N_h/2, + self.kw_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) @@ -110,7 +110,10 @@ def loss(self, score, truth): #loss for the key word number truth_num = [len(t) for t in truth] # double check to exclude select data = torch.from_numpy(np.array(truth_num)) - truth_num_var = Variable(data.cuda()) + if self.gpu: + truth_num_var = Variable(data.cuda()) + else: + truth_num_var = Variable(data) loss += self.CE(kw_num_score, truth_num_var) #loss for the key words T = len(kw_score[0]) @@ -118,7 +121,10 @@ def loss(self, score, truth): for b in range(B): truth_prob[b][truth[b]] = 1 data = torch.from_numpy(truth_prob) - truth_var = Variable(data.cuda()) + if self.gpu: + truth_var = Variable(data.cuda()) + else: + truth_var = Variable(data) #loss += self.mlsml(kw_score, truth_var) #loss += self.bce_logit(kw_score, truth_var) # double check no sigmoid for kw pred_prob = self.sigm(kw_score) From 458bfbb5233f3855a7e5b4651a83b4a55af5d026 Mon Sep 17 00:00:00 2001 From: Tong Guo <779222056@qq.com> Date: Tue, 29 Oct 2019 14:53:34 +0800 Subject: [PATCH 2/3] Update keyword_predictor.py --- models/keyword_predictor.py | 1 + 1 file changed, 1 insertion(+) diff --git a/models/keyword_predictor.py b/models/keyword_predictor.py index bbff2c2..697214d 100644 --- a/models/keyword_predictor.py +++ b/models/keyword_predictor.py @@ -110,6 +110,7 @@ def loss(self, score, truth): #loss for the key word number truth_num = [len(t) for t in truth] # double check to exclude select data = torch.from_numpy(np.array(truth_num)) + data = torch._cast_Long(data) if self.gpu: truth_num_var = Variable(data.cuda()) else: From 4f175e8ce7430565294a3f49dcd7938724cad7f8 Mon Sep 17 00:00:00 2001 From: Tong Guo <779222056@qq.com> Date: Tue, 29 Oct 2019 14:55:16 +0800 Subject: [PATCH 3/3] Update keyword_predictor.py --- models/keyword_predictor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/keyword_predictor.py b/models/keyword_predictor.py index 697214d..cfcf5b0 100644 --- a/models/keyword_predictor.py +++ b/models/keyword_predictor.py @@ -41,7 +41,7 @@ def __init__(self, N_word, N_h, N_depth, gpu, use_hs): self.kw_out_kw = nn.Linear(N_h, N_h) self.kw_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()