SSAGES  0.9.3
Software Suite for Advanced General Ensemble Simulations
ANN.h
1 
20 #pragma once
21 
22 #include "Method.h"
23 #include "Grids/Grid.h"
24 #include "nnet/nnet.h"
25 
26 namespace SSAGES
27 {
29 
35  class ANN : public Method
36  {
37  private:
39  Eigen::VectorXi topol_;
40 
43  unsigned int sweep_, nsweep_;
45 
47  unsigned int citers_;
48 
50  nnet::neural_net net_;
51 
54  double pweight_, weight_;
56 
59  double temp_, kbt_;
61 
64 
67 
70 
73  Eigen::MatrixXd hist_, bias_, rbias_;
75 
78  std::vector<double> lowerb_, upperb_;
80 
83  std::vector<double> lowerk_, upperk_;
85 
87  std::string outfile_;
88 
90  bool preloaded_;
91 
93  bool overwrite_;
94 
96  void TrainNetwork();
97 
99  void WriteBias();
100 
101  public:
103 
120  ANN(const MPI_Comm& world,
121  const MPI_Comm& comm,
122  const Eigen::VectorXi& topol,
123  Grid<Eigen::VectorXd>* fgrid,
124  Grid<unsigned int>* hgrid,
125  Grid<double>* ugrid,
126  const std::vector<double>& lowerb,
127  const std::vector<double>& upperb,
128  const std::vector<double>& lowerk,
129  const std::vector<double>& upperk,
130  double temperature,
131  double weight,
132  unsigned int nsweep
133  );
134 
136  void PreSimulation(Snapshot* snapshot, const class CVManager& cvmanager) override;
137 
139  void PostIntegration(Snapshot* snapshot, const class CVManager& cvmanager) override;
140 
142  void PostSimulation(Snapshot* snapshot, const class CVManager& cvmanager) override;
143 
145 
148  void SetPrevWeight(double h)
149  {
150  pweight_ = h;
151  }
152 
154 
157  void SetOutput(const std::string& outfile)
158  {
159  outfile_ = outfile;
160  }
161 
163 
166  void SetOutputOverwrite(bool overwrite)
167  {
168  overwrite_ = overwrite;
169  }
170 
172 
175  void SetConvergeIters(unsigned int citers)
176  {
177  citers_ = citers;
178  }
179 
181 
184  void SetMaxIters(unsigned int iters)
185  {
186  auto params = net_.get_train_params();
187  params.max_iter = iters;
188  net_.set_train_params(params);
189  }
190 
192 
195  void SetMinLoss(double loss)
196  {
197  auto params = net_.get_train_params();
198  params.min_loss = loss;
199  net_.set_train_params(params);
200  }
201 
203  void ReadBias(const std::string&, const std::string&);
204 
206  static ANN* Build(
207  const Json::Value& json,
208  const MPI_Comm& world,
209  const MPI_Comm& comm,
210  const std::string& path);
211 
212  ~ANN()
213  {
214  delete fgrid_;
215  delete hgrid_;
216  }
217  };
218 }
Artificial Neural Network Method.
Definition: ANN.h:36
std::vector< double > lowerb_
Definition: ANN.h:78
void SetMinLoss(double loss)
Set minimum loss function value (should be zero for production).
Definition: ANN.h:195
void PostSimulation(Snapshot *snapshot, const class CVManager &cvmanager) override
Method call post simulation.
Definition: ANN.cpp:194
Grid< unsigned int > * hgrid_
Histogram grid.
Definition: ANN.h:66
double temp_
Definition: ANN.h:59
std::vector< double > lowerk_
Definition: ANN.h:83
void SetConvergeIters(unsigned int citers)
Set number of iterations after which we turn on full weight.
Definition: ANN.h:175
void SetMaxIters(unsigned int iters)
Set maximum number of training iterations per sweep.
Definition: ANN.h:184
static ANN * Build(const Json::Value &json, const MPI_Comm &world, const MPI_Comm &comm, const std::string &path)
Build a derived method from JSON node.
Definition: ANN.cpp:287
void SetOutput(const std::string &outfile)
Set name of output file.
Definition: ANN.h:157
Eigen::MatrixXd hist_
Definition: ANN.h:73
bool preloaded_
Is the network preloaded?
Definition: ANN.h:90
void TrainNetwork()
Trains the neural network.
Definition: ANN.cpp:198
void PreSimulation(Snapshot *snapshot, const class CVManager &cvmanager) override
Method call prior to simulation initiation.
Definition: ANN.cpp:76
unsigned int sweep_
Definition: ANN.h:43
Grid< Eigen::VectorXd > * fgrid_
Force grid.
Definition: ANN.h:63
nnet::neural_net net_
Neural network.
Definition: ANN.h:50
ANN(const MPI_Comm &world, const MPI_Comm &comm, const Eigen::VectorXi &topol, Grid< Eigen::VectorXd > *fgrid, Grid< unsigned int > *hgrid, Grid< double > *ugrid, const std::vector< double > &lowerb, const std::vector< double > &upperb, const std::vector< double > &lowerk, const std::vector< double > &upperk, double temperature, double weight, unsigned int nsweep)
Constructor.
Definition: ANN.cpp:34
double pweight_
Definition: ANN.h:54
void ReadBias(const std::string &, const std::string &)
Load network state and bias from file.
Definition: ANN.cpp:264
void SetPrevWeight(double h)
Set previous history weight.
Definition: ANN.h:148
unsigned int citers_
Number of iterations after which we turn on full weight.
Definition: ANN.h:47
Eigen::VectorXi topol_
Neural network topology.
Definition: ANN.h:39
void WriteBias()
Writes out the bias to file.
Definition: ANN.cpp:245
void PostIntegration(Snapshot *snapshot, const class CVManager &cvmanager) override
Method call post integration.
Definition: ANN.cpp:113
std::string outfile_
Output filename.
Definition: ANN.h:87
Grid< double > * ugrid_
Unbiased histogram grid.
Definition: ANN.h:69
bool overwrite_
Overwrite outputs?
Definition: ANN.h:93
void SetOutputOverwrite(bool overwrite)
Set overwrite flag on output file.
Definition: ANN.h:166
Collective variable manager.
Definition: CVManager.h:43
Interface for Method implementations.
Definition: Method.h:44
Class containing a snapshot of the current simulation in time.
Definition: Snapshot.h:48