def predict(self, features, level): if level == 0: vec = vectorize(features, self.feature_template_level_0) pred_label = self.classifier_level_0.predict(vec) if level == 1: vec = vectorize(features, self.feature_template_level_1) pred_label = self.classifier_level_1.predict(vec) if level == 2: vec = vectorize(features, self.feature_template_level_2) pred_label = self.classifier_level_2.predict(vec) return self.idxrelation_map[pred_label[0]]
def gen_relation_train_data(self, level): for rst_tree in self.rst_tree_instances: for feats, relation in rst_tree.generate_relation_samples( self.brown_clusters, level): if level == 0: yield vectorize(feats, self.relation_feat_template_level_0 ), self.relation_map[relation] if level == 1: yield vectorize(feats, self.relation_feat_template_level_1 ), self.relation_map[relation] if level == 2: yield vectorize(feats, self.relation_feat_template_level_2 ), self.relation_map[relation]
def predict(self, features, dependency_feat, level): if level == 0: vec = vectorize(features, self.feature_template_level_0) # pred_label = self.classifier_level_0.predict(vec) pred_label = self.classifier_level_0.predict(np.concatenate([vec.toarray(), dependency_feat], axis=1)) if level == 1: vec = vectorize(features, self.feature_template_level_1) # pred_label = self.classifier_level_1.predict(vec) pred_label = self.classifier_level_1.predict(np.concatenate([vec.toarray(), dependency_feat], axis=1)) if level == 2: vec = vectorize(features, self.feature_template_level_2) # pred_label = self.classifier_level_2.predict(vec) pred_label = self.classifier_level_2.predict(np.concatenate([vec.toarray(), dependency_feat], axis=1)) return self.idxrelation_map[pred_label[0]]
def gen_action_train_data(self): tree = [] for rst_tree in self.rst_tree_instances: print("gen_action_train_data ...") for feats, dependency_feats, action in rst_tree.generate_action_samples( self.brown_clusters): yield vectorize(feats, self.action_feat_template ), self.action_map[action], dependency_feats[0] tree.append(rst_tree)
def gen_relation_train_data(self, level): # trees = pickle.load(open('../data/model/action/tree.bin','rb')) for rst_tree in self.rst_tree_instances: print("gen_relation_train_data ...") # for rst_tree in trees: for feats, dependency_feat, relation in rst_tree.generate_relation_samples( self.brown_clusters, level): if level == 0: yield vectorize( feats, self.relation_feat_template_level_0 ), self.relation_map[relation], dependency_feat[0] if level == 1: yield vectorize( feats, self.relation_feat_template_level_1 ), self.relation_map[relation], dependency_feat[0] if level == 2: yield vectorize( feats, self.relation_feat_template_level_2 ), self.relation_map[relation], dependency_feat[0]
def predict_probs(self, features, dependency_feat): """ predict labels and rank the decision label with their confidence value, output labels and probabilities """ vec = vectorize(features, self.feature_template) # print(vec.toarray().shape) # print(np.concatenate([vec.toarray(),dependency_feat],axis=1).shape) feats = np.concatenate([vec.toarray(), dependency_feat], axis=1) vals = self.classifier.decision_function(feats) action_vals = {} for idx in range(len(self.idxaction_map)): action_vals[self.idxaction_map[idx]] = vals[0, idx] sorted_actions = sorted(action_vals.items(), key=itemgetter(1), reverse=True) return sorted_actions
def predict_probs(self, features): """ predict labels and rank the decision label with their confidence value, output labels and probabilities """ vec = vectorize(features, self.feature_template) # vec = self.classifier.transform(vec) try: vals = self.classifier.decision_function(vec) except: # try: vals = self.classifier.predict_proba(vec.toarray()) # except: # raise NotImplementedError('Undefined classifier') action_vals = {} for idx in range(len(self.idxaction_map)): action_vals[self.idxaction_map[idx]] = vals[0, idx] sorted_actions = sorted(action_vals.items(), key=itemgetter(1), reverse=True) return sorted_actions
def gen_action_train_data(self): for rst_tree in self.rst_tree_instances: for feats, action in rst_tree.generate_action_samples( self.brown_clusters): yield vectorize( feats, self.action_feat_template), self.action_map[action]