Scine::Swoose  2.1.0
This is the SCINE module Swoose.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Pages
MolecularMachineLearningModel.h
Go to the documentation of this file.
1 
8 #ifndef SWOOSE_MOLECULARMACHINELEARNINGMODEL_H
9 #define SWOOSE_MOLECULARMACHINELEARNINGMODEL_H
10 
12 #include <Eigen/Dense>
13 #include <vector>
14 
15 namespace Scine {
16 
17 // Forward declarations
18 namespace Utils {
19 class MolecularTrajectory;
20 class AtomCollection;
21 } // namespace Utils
22 
23 namespace Swoose {
24 using ForcesCollection = Eigen::Matrix<double, Eigen::Dynamic, 3, Eigen::RowMajor>;
25 
26 namespace MachineLearning {
27 
33  public:
43  void setReferenceData(const Utils::MolecularTrajectory& structures, const std::vector<ForcesCollection>& refForces);
47  void trainEnergyModel();
51  void trainForcesModel();
55  double predictEnergy(const Utils::AtomCollection& structure);
59  ForcesCollection predictForces(const Utils::AtomCollection& structure);
65  std::pair<double, double> evaluateEnergyModel(int k);
75  std::pair<double, double> evaluateForcesModel(int k, bool pooledVariance = true);
84 
85  private:
86  // Sets the default values of the model hyperparameters for all of the predictors.
87  void setDefaultHyperparameters();
88  // Trains a single forces model for a given atom index
89  void trainSingleForceModel(int atomIndex);
90  // Evaluates a single force model for a given atom index with k-fold cross-validation.
91  std::pair<double, double> evaluateSingleForceModel(int atomIndex, int k);
92  // Returns the energy feature matrix
93  Eigen::MatrixXd getEnergyFeatures();
94  // Returns the energy target vector
95  Eigen::VectorXd getEnergyTargets();
96  // Returns the feature matrix for the atomic force of the atom with index 'atomIndex'
97  Eigen::MatrixXd getSingleForceFeatures(int atomIndex);
98  // Returns the target matrix for the atomic force of the atom with index 'atomIndex'
99  Eigen::MatrixXd getSingleForceTargets(int atomIndex);
100  // The KRR model that learns and predicts the energy of the molecular system
102  // The KRR models that learn and predict the atomic forces of the molecular system.
103  // It exists one model for each atom.
104  std::vector<Utils::MachineLearning::KernelRidgeRegression> forcePredictors_;
105  // The Coulomb matrix features for all structures (reference data)
106  std::vector<Eigen::VectorXd> coulombMatrixFeatures_;
107  // The features for learning atomic forces for all structures
108  std::vector<std::vector<Eigen::VectorXd>> forcesFeatures_;
109  // Reference energy targets
110  std::vector<double> refEnergies_;
111  // Reference atomic forces targets
112  std::vector<ForcesCollection> refForces_;
113 };
114 
115 } // namespace MachineLearning
116 } // namespace Swoose
117 } // namespace Scine
118 
119 #endif // SWOOSE_MOLECULARMACHINELEARNINGMODEL_H
void setReferenceData(const Utils::MolecularTrajectory &structures, const std::vector< ForcesCollection > &refForces)
Sets the reference data.
Definition: MolecularMachineLearningModel.cpp:20
std::pair< double, double > evaluateForcesModel(int k, bool pooledVariance=true)
Validates the model for the atomic forces via k-fold cross validation.
Definition: MolecularMachineLearningModel.cpp:134
void trainEnergyModel()
Trains the energy model.
Definition: MolecularMachineLearningModel.cpp:73
The combination of the machine learning models for molecular energies and atomic forces.
Definition: MolecularMachineLearningModel.h:32
std::pair< double, double > evaluateEnergyModel(int k)
Validates the energy model via k-fold cross validation.
Definition: MolecularMachineLearningModel.cpp:123
Utils::MachineLearning::KernelRidgeRegression & energyPredictor()
Accessor for the underlying energy model.
Definition: MolecularMachineLearningModel.cpp:202
ForcesCollection predictForces(const Utils::AtomCollection &structure)
Predicts the forces for a given structure.
Definition: MolecularMachineLearningModel.cpp:111
double predictEnergy(const Utils::AtomCollection &structure)
Predicts the energy for a given structure.
Definition: MolecularMachineLearningModel.cpp:105
void trainForcesModel()
Trains the forces model.
Definition: MolecularMachineLearningModel.cpp:84
Utils::MachineLearning::KernelRidgeRegression & forcePredictor(int atomIndex)
Accessor for the underlying force model of the atom with index &#39;atomIndex&#39;.
Definition: MolecularMachineLearningModel.cpp:206