def predict_action_probabilities(self, tracker: DialogueStateTracker, domain: Domain) -> List[float]: """Predict the next action the bot should take. Return the list of probabilities for the next actions. """ if self.model is None: return self._default_predictions(domain) # create model data from tracker data_X = self.featurizer.create_X([tracker], domain) model_data = self._create_model_data(data_X) output = self.model.predict(model_data) confidence = output["action_scores"].numpy() # remove batch dimension and take the last prediction in the sequence confidence = confidence[0, -1, :] if self.config[ LOSS_TYPE] == SOFTMAX and self.config[RANKING_LENGTH] > 0: confidence = train_utils.normalize(confidence, self.config[RANKING_LENGTH]) return confidence.tolist()
def predict_action_probabilities( self, tracker: DialogueStateTracker, domain: Domain, interpreter: NaturalLanguageInterpreter, **kwargs: Any, ) -> PolicyPrediction: """Predicts the next action the bot should take. See the docstring of the parent class `Policy` for more information. """ if self.model is None: return self._prediction(self._default_predictions(domain)) # create model data from tracker tracker_state_features = self.featurizer.create_state_features( [tracker], domain, interpreter) model_data = self._create_model_data(tracker_state_features) output = self.model.predict(model_data) confidence = output["action_scores"].numpy() # remove batch dimension and take the last prediction in the sequence confidence = confidence[0, -1, :] if self.config[ LOSS_TYPE] == SOFTMAX and self.config[RANKING_LENGTH] > 0: confidence = train_utils.normalize(confidence, self.config[RANKING_LENGTH]) return self._prediction(confidence.tolist())
def _predict_label(self, prob=None ) -> Tuple[Dict[Text, Any], List[Dict[Text, Any]]]: """Predicts the intent of the provided message.""" label = {"name": None, "confidence": 0.0} label_ranking = [] if prob is None: return label, label_ranking message_score = prob.flatten() label_ids = message_score.argsort()[::-1] message_score = train_utils.normalize( message_score, self.component_config[RANKING_LENGTH]) message_score[::-1].sort() message_score = message_score.tolist() # if X contains all zeros do not predict some label if label_ids.size > 0: label = { "name": self.index_label_id_mapping[label_ids[0]], "confidence": message_score[0], } if (self.component_config[RANKING_LENGTH] and 0 < self.component_config[RANKING_LENGTH] < LABEL_RANKING_LENGTH): output_length = self.component_config[RANKING_LENGTH] else: output_length = LABEL_RANKING_LENGTH ranking = list(zip(list(label_ids), message_score)) ranking = ranking[:output_length] label_ranking = [{ "name": self.index_label_id_mapping[label_idx], "confidence": score } for label_idx, score in ranking] return label, label_ranking
def predict_action_probabilities(self, tracker: "DialogueStateTracker", domain: "Domain") -> List[float]: """Predict the next action the bot should take. Return the list of probabilities for the next actions. """ if self.session is None: logger.error("There is no trained tf.session: " "component is either not trained or " "didn't receive enough training data") return [0.0] * domain.num_actions tf_feed_dict = self.tf_feed_dict_for_prediction(tracker, domain) confidence = self.session.run(self.pred_confidence, feed_dict=tf_feed_dict) confidence = confidence[0, -1, :] if self.loss_type == "softmax" and self.ranking_length > 0: confidence = train_utils.normalize(confidence, self.ranking_length) return confidence.tolist()
def _calculate_message_sim( self, batch: Tuple[np.ndarray]) -> Tuple[np.ndarray, List[float]]: """Calculate message similarities""" message_sim = self.session.run( self.pred_confidence, feed_dict={ _x_in: _x for _x_in, _x in zip(self.batch_in, batch) if _x is not None }, ) message_sim = message_sim.flatten() # sim is a matrix label_ids = message_sim.argsort()[::-1] if self.loss_type == "softmax" and self.ranking_length > 0: message_sim = train_utils.normalize(message_sim, self.ranking_length) message_sim[::-1].sort() # transform sim to python list for JSON serializing return label_ids, message_sim.tolist()
def test_normalize(input_values, ranking_length, output_values): normalized_values = train_utils.normalize(np.array(input_values), ranking_length) assert np.allclose(normalized_values, np.array(output_values), atol=1e-5)