def predict(self, subset=1, verbose=1): def softmax(predicted_output): a = np.exp(predicted_output) b = np.sum(a, 1).reshape(-1, 1) return a / b prediction_model = self.prediction_model all_answer_options_intseq = self.data.cache.all_answer_options_intseq explain_intseq = self.data.exp_intseq questions_intseq = self.data.questions_intseq answers = self.data.cache.answers indices = self.data.indices int_ans = np.array( [self.data.convert_to_int(letter) for letter, ans in answers]) if subset == 1: train_indices, val_indices, test_indices = self.data.indices train_indices = train_indices[0:150] val_indices = val_indices[0:150] test_indices = test_indices[0:150] indices = [train_indices, val_indices, test_indices] prediction_model.compile(optimizer='adam', loss=_loss_tensor, metrics=[keras.metrics.categorical_accuracy]) all_answer_options_intseq = np.array(all_answer_options_intseq) acc = [] for i in range(3): ind = indices[i] input1 = explain_intseq[ind] input2 = questions_intseq[ind] input3 = all_answer_options_intseq[:, 0, :][ind] input4 = all_answer_options_intseq[:, 1, :][ind] input5 = all_answer_options_intseq[:, 2, :][ind] input6 = all_answer_options_intseq[:, 3, :][ind] predicted_output = prediction_model.predict( [input1, input2, input3, input4, input5, input6], batch_size=64, verbose=verbose) predicted_output_softmax = softmax(predicted_output) predicted_ans = np.argmax(predicted_output, axis=1) accuracy = np.mean(predicted_ans == int_ans[ind]) acc.append(accuracy) print('train,val,test accuracies: {:.2f}/{:.2f}/{:.2f}'.format( acc[0], acc[1], acc[2])) cache = Struct() cache.predicted_output = predicted_output cache.predicted_output_softmax = predicted_output_softmax cache.predicted_ans = predicted_ans cache.int_ans = int_ans self.predictions_cache = cache self.acc = acc return cache