27 #include <pybind11/numpy.h>
28 #include <pybind11/pybind11.h>
30 namespace py = pybind11;
61 std::ostringstream err;
62 std::string str =
monitor.cast<std::string>();
63 if (str !=
"train" && str !=
"val") {
64 err <<
"invalid metric to monitor: " << str << std::endl;
65 throw std::invalid_argument(err.str());
68 err <<
"save_freq cannot be negative" << std::endl;
69 throw std::invalid_argument(err.str());
81 std::ostringstream status;
84 py::print(status.str());
96 py::list data = metrics[
monitor];
97 py::list trials = metrics[
"trials"];
98 const double current_error = py::cast<double>(data[data.size() - 1]);
99 const int current_trial = py::cast<int>(trials[trials.size() - 1]);
Callback to save XCSF at some frequency.
bool save_best_only
Whether to only save the best population.
int save_trial
Trial number the last checkpoint was made.
void finish(struct XCSF *xcsf) override
Executes any tasks at the end of fitting.
CheckpointCallback(py::str monitor, std::string filename, bool save_best_only, int save_freq, bool verbose)
Constructs a new checkpoint callback.
bool run(struct XCSF *xcsf, py::dict metrics) override
Performs callback operations.
int save_freq
Trial frequency to (possibly) make checkpoints.
std::string filename
Name of the file to save XCSF.
bool verbose
Whether to display messages when an action is taken.
double best_error
Best error.
void save(struct XCSF *xcsf)
Saves the state of XCSF.
py::str monitor
Name of the metric to monitor.
Utilities for Python library.
std::string get_timestamp()
Returns a formatted string for displaying time.
size_t xcsf_save(const struct XCSF *xcsf, const char *filename)
Writes the current state of XCSF to a file.