Skip to content
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
18 changes: 12 additions & 6 deletions models/op_predictor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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()
Expand Down Expand Up @@ -126,15 +126,21 @@ 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])
truth_prob = np.zeros((B, T), dtype=np.float32)
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)
Expand Down