27 #ifndef SRC_METHODS_ML_BASED_METHOD_HPP_ 28 #define SRC_METHODS_ML_BASED_METHOD_HPP_ 54 template<
class UserNodeLabel,
class UserEdgeLabel>
82 enum MLMethod_ {DNN, SVM, ONE_CLASS_SVM};
88 Assignment_(std::size_t row_in_master, std::size_t col_in_master,
bool good_assignment,
const std::vector<double> & feature_vector);
90 Assignment_(
const std::string & line, std::size_t num_features);
92 Assignment_(
const Assignment_ & assignment);
94 std::size_t row_in_master()
const;
96 std::size_t col_in_master()
const;
98 std::string to_string()
const;
100 double * dnn_feature_vector();
102 struct svm_node * svm_feature_vector();
106 bool is_good_assignment()
const;
108 std::size_t num_features()
const;
112 std::size_t row_in_master_;
114 std::size_t col_in_master_;
118 std::vector<double> dnn_feature_vector_;
120 std::vector<struct svm_node> svm_feature_vector_;
127 std::vector<FANN::activation_function_enum> activation_candidates;
129 unsigned int min_num_hidden_layers;
131 unsigned int max_num_hidden_layers;
133 unsigned int min_num_neurons_per_layer;
135 unsigned int max_num_neurons_per_layer;
144 std::size_t load(
const std::string & filename);
146 void train(FANN::training_data & training_data,
const MLBasedMethod::DNNParams_ & params,
const std::string & filename, std::size_t num_threads);
148 double decision_value(
double * feature_vector);
152 float cross_validate_(FANN::training_data & training_data,
const MLBasedMethod::DNNParams_ & params,
unsigned int num_hidden_layers,
unsigned int num_neurons_per_layer, FANN::activation_function_enum hidden_activation);
154 float train_and_validate_(FANN::neural_net & neural_net, FANN::training_data & training_data, FANN::training_data & validation_data, std::size_t max_num_epochs);
156 FANN::neural_net neural_net_;
184 std::size_t load(
const std::string & filename);
186 void train(
struct svm_problem * training_data,
const MLBasedMethod::SVMParams_ & params, std::size_t num_features,
const std::string & filename, std::size_t num_threads);
188 double decision_value(
struct svm_node * feature_vector)
const;
192 struct svm_model * svm_model_;
204 std::size_t load(
const std::string & filename,
bool use_likelihood);
206 void train(
struct svm_problem * training_data,
bool use_likelihood, std::size_t num_features,
const std::string & filename);
208 double decision_value(
struct svm_node * feature_vector)
const;
212 struct svm_model * svm_model_;
218 double scale_factor_;
220 bool use_likelihood_;
222 void compute_rho_and_scale_factor_(std::size_t num_features);
225 std::pair<GEDGraph::GraphID, GEDGraph::GraphID> prediction_initialized_;
227 std::vector<Assignment_> assignments_;
229 MLMethod_ ml_method_;
233 std::string ground_truth_options_;
235 DNNParams_ dnn_params_;
239 FANN::training_data dnn_training_data_;
241 std::vector<double *> dnn_feature_vectors_;
243 std::vector<double *> dnn_types_;
245 SVMParams_ svm_params_;
249 struct svm_problem svm_training_data_;
251 std::vector<struct svm_node *> svm_feature_vectors_;
253 std::vector<double> svm_types_;
255 bool one_class_svm_use_likelihood_;
257 OneClassSVM_ one_class_svm_;
261 std::string outfile_;
263 std::string logfile_;
265 std::string training_infile_;
267 std::string training_outfile_;
269 std::string ground_truth_infile_;
271 std::string ground_truth_outfile_;
285 virtual bool lsape_parse_option_(
const std::string & option,
const std::string & value)
final;
293 bool initialized_for_prediction_(
const GEDGraph & g,
const GEDGraph & h)
const;
297 void load_or_generate_training_data_();
299 void save_training_data_();
301 bool load_config_file_()
const;
303 bool log_prediction_ratios_()
const;
305 bool compute_or_load_ground_truth_()
const;
307 double decision_value_(Assignment_ & assignmment);
336 virtual bool ml_parse_option_(
const std::string & option,
const std::string & arg);
virtual void lsape_pre_graph_init_(bool called_at_runtime) final
Initializes the method at runtime or during initialization before initializing the global variables f...
Contains the standardized input data along with basic functionality.
virtual bool lsape_parse_option_(const std::string &option, const std::string &value) final
Parses one option that is not among the ones shared by all derived classes of ged::LSAPEBasedMethod.
virtual std::string lsape_valid_options_string_() const final
Returns string of all valid options that are not among the ones shared by all derived classes of ged:...
virtual void lsape_set_default_options_() final
Sets all options that are not among the ones shared by all derived classes of ged::LSAPEBasedMethod t...
virtual bool ml_parse_option_(const std::string &option, const std::string &arg)
Parses one option that is not among the ones shared by all derived classes of ged::MLBasedMethod.
virtual std::string ml_valid_options_string_() const
Returns string of all valid options that are not among the ones shared by all derived classes of ged:...
Abstract class for the (suboptimal) computation of the graph edit distance.
std::size_t num_features_
The size of the feature vectors.
virtual std::size_t ml_get_num_features_()
Returns the number of features.
virtual void lsape_init_graph_(const GEDGraph &graph) final
Initializes global variables for one graph.
Abstract class for methods that use lossy transformations to LSAPE for approximating the graph edit d...
virtual void ml_init_feature_variables_(const GEDGraph &g, const GEDGraph &h, std::size_t num_threads)
Initializes variables that are used for populating the feature vectors of assignments between two inp...
virtual void lsape_init_() final
Initializes the method after initializing the global variables for the graphs.
virtual void lsape_populate_instance_(const GEDGraph &g, const GEDGraph &h, DMatrix &master_problem) final
Populates the LSAPE instance.
virtual void ml_init_graph_(const GEDGraph &graph)
Initializes global variables for one graph.
The normalized input graphs used by GEDLIB. All labels are integers.
virtual void ml_populate_deletion_feature_vector_(const GEDGraph &g, GEDGraph::NodeID i, std::vector< double > &feature_vector)
Computes deletion feature vector.
virtual void ml_init_()
Initializes the method after initializing the global variables for the graphs.
Global namespace for GEDLIB.
virtual void ml_set_default_options_()
Sets all options that are not among the ones shared by all derived classes of ged::MLBasedMethod to d...
virtual void ml_init_for_num_features_()
Initializes the derived class for running with feature vectors of size ged::MLBasedMethod::num_featur...
virtual void ml_populate_substitution_feature_vector_(const GEDGraph &g, const GEDGraph &h, GEDGraph::NodeID i, GEDGraph::NodeID k, std::vector< double > &feature_vector)
Computes substitution feature vector.
Abstract class for methods that transform GED to LSAPE by using a SVM or a DNN to predict the cost of...
virtual void lsape_default_post_graph_init_() final
Default initializes the method at runtime after initializing the global variables for the graphs...
std::size_t NodeID
Internally used vertex ID type.
double predict(const GEDGraph &g, const GEDGraph &h, const NodeMap::Assignment &assignment)
Predicts the type of a node assignment.
virtual void ml_populate_insertion_feature_vector_(const GEDGraph &h, GEDGraph::NodeID k, std::vector< double > &feature_vector)
Computes insertion feature vector.