From 1b1f426c2ce03f171577a6eaa59e4c005b3874ac Mon Sep 17 00:00:00 2001 From: Tong Guo <779222056@qq.com> Date: Tue, 29 Oct 2019 11:31:39 +0800 Subject: [PATCH 1/2] Update andor_predictor.py --- models/andor_predictor.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/models/andor_predictor.py b/models/andor_predictor.py index 5b0fc0a..e530491 100644 --- a/models/andor_predictor.py +++ b/models/andor_predictor.py @@ -14,11 +14,11 @@ 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) @@ -47,7 +47,10 @@ def forward(self, q_emb_var, q_len, hs_emb_var, hs_len): att_np_q = np.ones((B, max_q_len)) att_val_q = torch.from_numpy(att_np_q).float() - att_val_q = Variable(att_val_q.cuda()) + if self.gpu: + att_val_q = Variable(att_val_q.cuda()) + else: + att_val_q = Variable(att_val_q) for idx, num in enumerate(q_len): if num < max_q_len: att_val_q[idx, num:] = -100 @@ -57,7 +60,10 @@ def forward(self, q_emb_var, q_len, hs_emb_var, hs_len): # Same as the above, compute SQL history embedding weighted by column attentions att_np_h = np.ones((B, max_hs_len)) att_val_h = torch.from_numpy(att_np_h).float() - att_val_h = Variable(att_val_h.cuda()) + if self.gpu: + att_val_h = Variable(att_val_h.cuda()) + else: + att_val_h = Variable(att_val_h) for idx, num in enumerate(hs_len): if num < max_hs_len: att_val_h[idx, num:] = -100 From 513c8eab05472f3c0294df1dfabe52d17030351a Mon Sep 17 00:00:00 2001 From: Tong Guo <779222056@qq.com> Date: Tue, 29 Oct 2019 15:02:05 +0800 Subject: [PATCH 2/2] Update andor_predictor.py --- models/andor_predictor.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/models/andor_predictor.py b/models/andor_predictor.py index e530491..f1f54ca 100644 --- a/models/andor_predictor.py +++ b/models/andor_predictor.py @@ -28,7 +28,7 @@ def __init__(self, N_word, N_h, N_depth, gpu, use_hs): self.ao_out_hs = nn.Linear(N_h, N_h) self.ao_out = nn.Sequential(nn.Tanh(), nn.Linear(N_h, 2)) #for and/or - 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() @@ -78,7 +78,11 @@ def forward(self, q_emb_var, q_len, hs_emb_var, hs_len): def loss(self, score, truth): loss = 0 data = torch.from_numpy(np.array(truth)) - truth_var = Variable(data.cuda()) + data = torch._cast_Long(data) + if self.gpu: + truth_var = Variable(data.cuda()) + else: + truth_var = Variable(data) loss = self.CE(score, truth_var) return loss