From 4395dfc7a0e2e2ae6211b79dc03c392037206bd7 Mon Sep 17 00:00:00 2001 From: Tong Guo <779222056@qq.com> Date: Tue, 29 Oct 2019 11:40:22 +0800 Subject: [PATCH 1/2] Update op_predictor.py --- models/op_predictor.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/models/op_predictor.py b/models/op_predictor.py index 9f7b260..cf882a2 100644 --- a/models/op_predictor.py +++ b/models/op_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) @@ -126,7 +126,10 @@ def loss(self, score, truth): # loss for the op number truth_num = [len(t)-1 for t in truth] #num_score 0 maps to 1 in truth 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(op_num_score, truth_num_var) # loss for op T = len(op_score[0]) @@ -134,7 +137,10 @@ def loss(self, score, truth): for b in range(B): truth_prob[b][truth[b]] = 1 data = torch.from_numpy(np.array(truth_prob)) - truth_var = Variable(data.cuda()) + if self.gpu: + truth_var = Variable(data.cuda()) + else: + truth_var = Variable(data) #loss += self.mlsml(op_score, truth_var) #loss += self.bce_logit(op_score, truth_var) pred_prob = self.sigm(op_score) From 804b16d0e75c619caf09c82a44899cc600721525 Mon Sep 17 00:00:00 2001 From: Tong Guo <779222056@qq.com> Date: Tue, 29 Oct 2019 14:56:51 +0800 Subject: [PATCH 2/2] Update op_predictor.py --- models/op_predictor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/op_predictor.py b/models/op_predictor.py index cf882a2..602be1c 100644 --- a/models/op_predictor.py +++ b/models/op_predictor.py @@ -40,7 +40,7 @@ def __init__(self, N_word, N_h, N_depth, gpu, use_hs): self.op_out_c = nn.Linear(N_h, N_h) self.op_out = nn.Sequential(nn.Tanh(), nn.Linear(N_h, 11)) #for 11 operators - 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()