Skip to content

for cpu and for python3, andor_predictor.py #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions models/andor_predictor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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()
Expand All @@ -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
Expand All @@ -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
Expand All @@ -72,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
Expand Down