cMHN 1.2
C++ library for learning MHNs with pRC
Loading...
Searching...
No Matches
learn_theta.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSD-2-Clause
2
3#ifndef cMHN_NONTT_LEARN_THETA_H
4#define cMHN_NONTT_LEARN_THETA_H
5
6#include <iostream>
7#include <map>
8#include <string>
9#include <tuple>
10
16
17#include <prc.hpp>
18
19namespace cMHN::nonTT
20{
50 template<class T, pRC::Size D, class S>
51 std::tuple<pRC::Tensor<T, D, D>, std::map<std::string, std::string>,
52 std::map<std::string, double>>
53 learnTheta(pRC::Tensor<T, D, D> const &theta, std::string const &header,
54 std::string const &output, std::map<S, T> const &pD,
56 T const &toleranceOptimizer, T const &toleranceSolverQ)
57 {
58 auto tempTheta = theta;
59
60 auto score = pRC::zero<T>();
61
62 pRC::Index at_iter = 0;
63
65
66 std::map<std::string, double> logInfoNumbers{{"Score", score()},
67 {"Iterations", at_iter},
68 {"Time", pRC::getTimeInSeconds()() - startTime()},
69 {"Lambda", Regulator.lambda()()}};
70
71 std::map<std::string, std::string> logInfoNames{
72 {"Score Name", Score.name()}, {"Regulator Name", Regulator.name()}};
73
74 writeTheta(output, header, tempTheta, logInfoNames, logInfoNumbers);
75
76 std::cout << "cMHN learning started (nonTT):" << std::endl;
77 std::cout << "\tScore Name:\t" << logInfoNames["Score Name"]
78 << std::endl;
79 std::cout << "\tRegulator Name:\t" << logInfoNames["Regulator Name"]
80 << std::endl
81 << std::endl;
82
83 tempTheta = pRC::optimize<>(
84 pRC::Optimizer::LBFGS<>(), tempTheta,
85 [&output, &at_iter, &score, &pD, &Score, &Regulator,
86 &toleranceOptimizer,
87 &toleranceSolverQ](auto const &tempTheta, auto &g)
88 {
89 MHNOperator<T, D> op(tempTheta);
90
91 std::tie(score, g) =
93
94 return score;
95 },
96 [&output, &header, &score, &at_iter, &startTime, &logInfoNames,
97 &logInfoNumbers](auto const &tempTheta)
98 {
99 at_iter++;
100 logInfoNumbers["Iterations"] = at_iter;
101 logInfoNumbers["Score"] = score();
102 logInfoNumbers["Time"] =
103 pRC::getTimeInSeconds()() - startTime();
104
105 std::cout << "cMHN learning in progress (nonTT):" << std::endl;
106 std::cout << std::defaultfloat;
107 std::cout << "\tIteration:\t" << logInfoNumbers["Iterations"]
108 << std::endl;
109 std::cout << std::scientific;
110 std::cout << "\tLambda:\t\t" << logInfoNumbers["Lambda"]
111 << std::endl;
112 std::cout << "\tScore:\t\t" << logInfoNumbers["Score"]
113 << std::endl;
114 std::cout << "\tTime:\t\t" << logInfoNumbers["Time"]
115 << std::endl;
116 std::cout << std::defaultfloat;
117
118 writeTheta(output, header, tempTheta, logInfoNames,
119 logInfoNumbers);
120 },
121 toleranceOptimizer);
122
123 return std::make_tuple(tempTheta, logInfoNames, logInfoNumbers);
124 }
125}
126
127#endif // cMHN_NONTT_LEARN_THETA_H
Class storing all relevant information for a regulator.
Definition regulator.hpp:30
auto & lambda()
Definition regulator.hpp:58
auto name() const
Definition regulator.hpp:68
Class storing all relevant information for a score.
Definition score.hpp:28
auto name() const
Definition score.hpp:55
Class storing an MHN operator represented by a theta matrix (for non TT calculations)
Definition mhn_operator.hpp:24
Definition value.hpp:12
Definition lbfgs.hpp:15
Definition tensor.hpp:25
Definition jacobi.hpp:11
std::tuple< pRC::Tensor< T, D, D >, std::map< std::string, std::string >, std::map< std::string, double > > learnTheta(pRC::Tensor< T, D, D > const &theta, std::string const &header, std::string const &output, std::map< S, T > const &pD, cMHN::Score< T > const &Score, cMHN::Regulator< T, D > const &Regulator, T const &toleranceOptimizer, T const &toleranceSolverQ)
Optimizes an MHN represented by a theta matrix to best describe a given data distribution.
Definition learn_theta.hpp:53
std::tuple< T, pRC::Tensor< T, D, D > > calculateScoreAndGradient(nonTT::MHNOperator< T, D > const &op, std::map< S, T > const &pD, cMHN::Score< T > const &Score, cMHN::Regulator< T, D > const &Regulator, T const &toleranceSolverQ=1e-8)
Calculate score and gradient of a theta matrix given some data distribution pD.
Definition calculate_score_and_gradient.hpp:35
static auto writeTheta(std::string const &filename, std::string const &header, pRC::Tensor< T, D, D > const &theta, std::map< std::string, std::string > const &logInfoNames={}, std::map< std::string, double > const &logInfoNumbers={})
Writes a theta matrix to file, including additional logging information at the bottom.
Definition write_theta.hpp:29
Size Index
Definition basics.hpp:32
static constexpr auto optimize(Optimizer &&optimizer, XX &&x, FF &&function, FC &&callback, VT const &tolerance=NumericLimits< Value< RemoveReference< XX > > >::tolerance())
Definition optimize.hpp:15
static Float< 64 > getTimeInSeconds()
Definition stopwatch.hpp:23
static constexpr auto zero()
Definition zero.hpp:12