def test(self, vocab_size, use_onto_lstm, S_ind_test=None, C_ind_test=None, hierarchical=False, base=2, oov_list=None):
X_test = C_ind_test[:,:-1] if use_onto_lstm else S_ind_test[:,:-1] # remove the last words' hyps in all sentences
Y_inds_test = S_ind_test[:,1:]
if hierarchical:
test_targets = self._factor_target_indices(Y_inds_test, vocab_size, base=base)
else:
test_targets = [self._make_one_hot(Y_inds_test, vocab_size)]
print >>sys.stderr, "Evaluating model on test data"
test_loss = self.model.evaluate(X_test, test_targets)
print >>sys.stderr, "Test loss: %.4f"%test_loss
if oov_list is not None:
oov_inds = [self.dp.word_index[w] for w in oov_list]
non_oov_Y_inds = numpy.copy(Y_inds_test)
for ind in oov_inds:
non_oov_Y_inds[non_oov_Y_inds == ind] = 0
non_oov_test_targets = self._factor_target_indices(non_oov_Y_inds, vocab_size, base=base)
non_oov_test_loss = self.model.evaluate(X_test, non_oov_test_targets)
print >>sys.stderr, "Non-oov test loss: %.4f"%non_oov_test_loss
factored_test_preds = [-((numpy.log(pred) * target).sum(axis=-1)) for pred, target in zip(self.model.predict(X_test), test_targets)]
test_preds = sum(factored_test_preds)
#non_null_probs = []
#for test_pred, inds in zip(test_preds, Y_inds_test):
# wanted_probs = []
# for tp, ind in zip(test_pred, inds):
# if ind != 0:
# wanted_probs.append(tp)
# non_null_probs.append(wanted_probs)
#return non_null_probs
return test_preds
评论列表
文章目录