cMHN 1.1
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 &toleranceSolverP,
57 T const &toleranceSolverQ)
58 {
59 auto tempTheta = theta;
60
61 auto score = pRC::zero<T>();
62
63 pRC::Index at_iter = 0;
64
66
67 std::map<std::string, double> logInfoNumbers{{"Score", score()},
68 {"Iterations", at_iter},
69 {"Time", pRC::getTimeInSeconds()() - startTime()},
70 {"Lambda", Regulator.lambda()()}};
71
72 std::map<std::string, std::string> logInfoNames{
73 {"Score Name", Score.name()}, {"Regulator Name", Regulator.name()}};
74
75 writeTheta(output, header, tempTheta, logInfoNames, logInfoNumbers);
76
77 std::cout << "cMHN learning started (nonTT):" << std::endl;
78 std::cout << "\tScore Name:\t" << logInfoNames["Score Name"]
79 << std::endl;
80 std::cout << "\tRegulator Name:\t" << logInfoNames["Regulator Name"]
81 << std::endl
82 << std::endl;
83
84 // use a random initial pInit
85 pRC::SeedSequence seq(8, 16);
86 pRC::RandomEngine rng(seq);
88 auto pInit = eval(expand(pRC::makeConstantSequence<pRC::Size, D, 2>(),
89 [&](auto const... Ns)
90 {
91 return pRC::random<pRC::Tensor<T, Ns...>>(rng, dist);
92 }));
93 pInit = pInit / norm<1>(pInit);
94
96 tempTheta,
97 [&output, &at_iter, &score, &pD, &Score, &Regulator,
98 &toleranceOptimizer, &toleranceSolverP,
99 &toleranceSolverQ](auto const &tempTheta, auto &g)
100 {
101 MHNOperator<T, D> op(tempTheta);
102
103 std::tie(score, g) =
105
106 // sign change b/c optimizers minimize the score
107 g = -g;
108 return -score;
109 },
110 [&output, &header, &score, &at_iter, &startTime, &logInfoNames,
111 &logInfoNumbers](auto const &tempTheta)
112 {
113 at_iter++;
114 logInfoNumbers["Iterations"] = at_iter;
115 logInfoNumbers["Score"] = score();
116 logInfoNumbers["Time"] =
117 pRC::getTimeInSeconds()() - startTime();
118
119 std::cout << "cMHN learning in progress (nonTT):" << std::endl;
120 std::cout << std::defaultfloat;
121 std::cout << "\tIteration:\t" << logInfoNumbers["Iterations"]
122 << std::endl;
123 std::cout << std::scientific;
124 std::cout << "\tLambda:\t\t" << logInfoNumbers["Lambda"]
125 << std::endl;
126 std::cout << "\tScore:\t\t" << logInfoNumbers["Score"]
127 << std::endl;
128 std::cout << "\tTime:\t\t" << logInfoNumbers["Time"]
129 << std::endl;
130 std::cout << std::defaultfloat;
131
132 writeTheta(output, header, tempTheta, logInfoNames,
133 logInfoNumbers);
134 },
135 toleranceOptimizer);
136
137 return std::make_tuple(tempTheta, logInfoNames, logInfoNumbers);
138 }
139}
140
141#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:27
auto name() const
Definition score.hpp:54
Class storing an MHN operator represented by a theta matrix (for non TT calculations)
Definition mhn_operator.hpp:24
Definition type_traits.hpp:57
Definition seq.hpp:13
Definition tensor.hpp:28
Definition threefry.hpp:24
pRC::Float<> T
Definition externs_nonTT.hpp:1
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 &toleranceSolverP, 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-4)
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
static constexpr auto makeConstantSequence()
Definition sequence.hpp:402
static constexpr auto random(RandomEngine &rng, D &distribution)
Definition random.hpp:12
Size Index
Definition type_traits.hpp:21
static Float< 64 > getTimeInSeconds()
Definition stopwatch.hpp:23