Stan  2.10.0
probability, sampling & optimization
do_bfgs_optimize.hpp
Go to the documentation of this file.
1 #ifndef STAN_SERVICES_OPTIMIZE_DO_BFGS_OPTIMIZE_HPP
2 #define STAN_SERVICES_OPTIMIZE_DO_BFGS_OPTIMIZE_HPP
3 
9 #include <fstream>
10 #include <iostream>
11 #include <iomanip>
12 #include <string>
13 #include <vector>
14 
15 namespace stan {
16  namespace services {
17  namespace optimize {
18 
19  template<typename Model, typename BFGSOptimizer, typename RNGT,
20  typename StartIterationCallback>
21  int do_bfgs_optimize(Model &model, BFGSOptimizer &bfgs,
22  RNGT &base_rng,
23  double &lp,
24  std::vector<double> &cont_vector,
25  std::vector<int> &disc_vector,
28  bool save_iterations,
29  int refresh,
30  StartIterationCallback& interrupt) {
31  lp = bfgs.logp();
32 
33  std::stringstream msg;
34  msg << "initial log joint probability = " << lp;
35  info(msg.str());
36 
37  if (save_iterations) {
38  io::write_iteration(model, base_rng,
39  lp, cont_vector, disc_vector,
40  info, output);
41  }
42 
43  int ret = 0;
44 
45  while (ret == 0) {
46  interrupt();
47  if (io::do_print(bfgs.iter_num(), 50*refresh)) {
48  info(" Iter "
49  " log prob "
50  " ||dx|| "
51  " ||grad|| "
52  " alpha "
53  " alpha0 "
54  " # evals "
55  " Notes ");
56  }
57 
58  ret = bfgs.step();
59  lp = bfgs.logp();
60  bfgs.params_r(cont_vector);
61 
62  if (io::do_print(bfgs.iter_num(),
63  ret != 0 || !bfgs.note().empty(), refresh)) {
64  msg.str("");
65  msg << " " << std::setw(7) << bfgs.iter_num() << " ";
66  msg << " " << std::setw(12) << std::setprecision(6)
67  << lp << " ";
68  msg << " " << std::setw(12) << std::setprecision(6)
69  << bfgs.prev_step_size() << " ";
70  msg << " " << std::setw(12) << std::setprecision(6)
71  << bfgs.curr_g().norm() << " ";
72  msg << " " << std::setw(10) << std::setprecision(4)
73  << bfgs.alpha() << " ";
74  msg << " " << std::setw(10) << std::setprecision(4)
75  << bfgs.alpha0() << " ";
76  msg << " " << std::setw(7)
77  << bfgs.grad_evals() << " ";
78  msg << " " << bfgs.note() << " ";
79  info(msg.str());
80  }
81 
82  if (save_iterations) {
83  io::write_iteration(model, base_rng,
84  lp, cont_vector, disc_vector,
85  info, output);
86  }
87  }
88 
89  int return_code;
90  if (ret >= 0) {
91  info("Optimization terminated normally: ");
92  return_code = stan::services::error_codes::OK;
93  } else {
94  info("Optimization terminated with error: ");
96  }
97  info(" " + bfgs.get_code_string(ret));
98 
99  return return_code;
100  }
101 
102  }
103  }
104 }
105 #endif
Probability, optimization and sampling library.
void write_iteration(Model &model, RNG &base_rng, double lp, std::vector< double > &cont_vector, std::vector< int > &disc_vector, interface_callbacks::writer::base_writer &message_writer, interface_callbacks::writer::base_writer &parameter_writer)
bool do_print(const int n, const bool special, const int refresh)
Indicates whether it should print on the current iteration.
Definition: do_print.hpp:25
base_writer is an abstract base class defining the interface for Stan writer callbacks.
Definition: base_writer.hpp:20
int do_bfgs_optimize(Model &model, BFGSOptimizer &bfgs, RNGT &base_rng, double &lp, std::vector< double > &cont_vector, std::vector< int > &disc_vector, interface_callbacks::writer::base_writer &output, interface_callbacks::writer::base_writer &info, bool save_iterations, int refresh, StartIterationCallback &interrupt)

     [ Stan Home Page ] © 2011–2016, Stan Development Team.