def predict(self, predict_data_rows: Iterable[str]) -> List[ModelPredictionResults]: predict_input_reader = self._create_data_reader(estimator_action=EstimatorAction.Predict) input_iterator = predict_input_reader.process_and_iterate_input_from_data_lines(predict_data_rows) all_model_prediction_results = [] for input_row in input_iterator: # perform the actual prediction and get raw results. input_for_predict = input_row[0][:4] # we want only the relevant input vectors (w.o. the targets). prediction_results = self.keras_model_predict_function(input_for_predict) # make `input_row` and `prediction_results` easy to read (by accessing named fields). prediction_results = KerasPredictionModelOutput( *common.squeeze_single_batch_dimension_for_np_arrays(prediction_results)) input_row = _KerasModelInputTensorsFormer( estimator_action=EstimatorAction.Predict).from_model_input_form(input_row) input_row = ReaderInputTensors(*common.squeeze_single_batch_dimension_for_np_arrays(input_row)) # calculate the attention weight for each context attention_per_context = self._get_attention_weight_per_context( path_source_strings=input_row.path_source_token_strings, path_strings=input_row.path_strings, path_target_strings=input_row.path_target_token_strings, attention_weights=prediction_results.attention_weights ) # store the calculated prediction results in the wanted format. model_prediction_results = ModelPredictionResults( original_name=common.binary_to_string(input_row.target_string.item()), topk_predicted_words=common.binary_to_string_list(prediction_results.topk_predicted_words), topk_predicted_words_scores=prediction_results.topk_predicted_words_scores, attention_per_context=attention_per_context, code_vector=prediction_results.code_vectors) all_model_prediction_results.append(model_prediction_results) return all_model_prediction_results
def from_model_input_form(self, input_row) -> ReaderInputTensors: return ReaderInputTensors( target_index=input_row[0], path_source_token_indices=input_row[1], path_indices=input_row[2], path_target_token_indices=input_row[3], context_valid_mask=input_row[4] )
def from_model_input_form(self, input_row) -> ReaderInputTensors: inputs, targets = input_row return ReaderInputTensors( path_source_token_indices=inputs[0], path_indices=inputs[1], path_target_token_indices=inputs[2], context_valid_mask=inputs[3], target_index=targets if self.estimator_action.is_train else targets['target_index'], target_string=targets['target_string'] if not self.estimator_action.is_train else None, path_source_token_strings=inputs[4] if self.estimator_action.is_predict else None, path_strings=inputs[5] if self.estimator_action.is_predict else None, path_target_token_strings=inputs[6] if self.estimator_action.is_predict else None )
def from_model_input_form(self, input_row) -> ReaderInputTensors: inputs, targets = input_row return ReaderInputTensors( path_source_token_indices=inputs[0], path_indices=inputs[1], path_target_token_indices=inputs[2], context_valid_mask=inputs[3], #I'm changing this for the same reason of the changes in previous function. #target_index=targets if self.estimator_action.is_train else targets['target_index'], #target_string=targets['target_string'] if not self.estimator_action.is_train else None, target_index=targets, target_string=None, path_source_token_strings=inputs[4] if self.estimator_action.is_predict else None, path_strings=inputs[5] if self.estimator_action.is_predict else None, path_target_token_strings=inputs[6] if self.estimator_action.is_predict else None)