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_TT_LEARN_THETA_H
4#define cMHN_TT_LEARN_THETA_H
5
6#include <iostream>
7#include <map>
8#include <string>
9#include <tuple>
10
12#include <cmhn/tt/als.hpp>
13#include <cmhn/tt/mamen.hpp>
15#include <cmhn/tt/utility.hpp>
19
20#include <prc.hpp>
21
22namespace cMHN::TT
23{
56 template<pRC::Size RP, pRC::Size RQ, class T, pRC::Size D, class S>
57 std::tuple<pRC::Tensor<T, D, D>, std::map<std::string, std::string>,
58 std::map<std::string, double>>
59 learnTheta(pRC::Tensor<T, D, D> const &theta, std::string const &header,
60 std::string const &output, std::map<S, T> const &pD,
62 T const &toleranceOptimizer, T const &toleranceSolverP,
63 T const &toleranceSolverQ)
64 {
65 using ModeSizes = decltype(getModeSizes<D>());
66
67 auto tempTheta = theta;
68
69 T score = pRC::zero();
70
71 pRC::Index at_iter = 0;
72
74
75 std::map<std::string, double> logInfoNumbers{{"Score", score()},
76 {"Iterations", at_iter},
77 {"Time", pRC::getTimeInSeconds()() - startTime()},
78 {"Lambda", Regulator.lambda()()}};
79
80 std::map<std::string, std::string> logInfoNames{
81 {"Score Name", Score.name()}, {"Regulator Name", Regulator.name()}};
82
83 writeTheta(output, header, tempTheta, logInfoNames, logInfoNumbers);
84
85 std::cout << "cMHN learning started (TT):" << std::endl;
86 std::cout << "\tScore Name:\t" << logInfoNames["Score Name"]
87 << std::endl;
88 std::cout << "\tRegulator Name:\t" << logInfoNames["Regulator Name"]
89 << std::endl
90 << std::endl;
91
92 // use a random initial pInit
93 pRC::SeedSequence seq(8, 16);
94 pRC::RandomEngine rng(seq);
96 auto pInit = round<decltype(getRanks<D, RP>())>(
98 decltype(getRanks<D, RP>())>>(rng, dist));
99 pInit = pInit /
100 scalarProduct(pInit,
102
104 tempTheta,
105 [&output, &at_iter, &score, &pD, &Score, &Regulator, &pInit,
106 &toleranceOptimizer, &toleranceSolverP, &toleranceSolverQ](
107 pRC::Tensor<T, D, D> const &tempTheta, pRC::Tensor<T, D, D> &g)
108 {
109 MHNOperator<T, D> op(tempTheta);
110
111 std::tie(score, g) =
112 ::cMHN::calculateScoreAndGradient<RP, RQ>(op, pD, Score,
113 Regulator, pInit, toleranceSolverP, toleranceSolverQ);
114
115 // signs change b/c optimizers minimize the score
116 g = -g;
117 return -score;
118 },
119 [&output, &header, &score, &at_iter, &startTime, &logInfoNames,
120 &logInfoNumbers](auto const &tempTheta)
121 {
122 at_iter++;
123 logInfoNumbers["Iterations"] = at_iter;
124 logInfoNumbers["Score"] = score();
125 logInfoNumbers["Time"] =
126 pRC::getTimeInSeconds()() - startTime();
127
128 std::cout << "cMHN learning in progress (TT):" << std::endl;
129 std::cout << std::defaultfloat;
130 std::cout << "\tIteration:\t" << logInfoNumbers["Iterations"]
131 << std::endl;
132 std::cout << std::scientific;
133 std::cout << "\tLambda:\t\t" << logInfoNumbers["Lambda"]
134 << std::endl;
135 std::cout << "\tScore:\t\t" << logInfoNumbers["Score"]
136 << std::endl;
137 std::cout << "\tTime:\t\t" << logInfoNumbers["Time"]
138 << std::endl;
139 std::cout << std::defaultfloat;
140
141 writeTheta(output, header, tempTheta, logInfoNames,
142 logInfoNumbers);
143 },
144 toleranceOptimizer);
145
146 return std::make_tuple(tempTheta, logInfoNames, logInfoNumbers);
147 }
148}
149
150#endif // cMHN_TT_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 TT calculations)
Definition mhn_operator.hpp:24
Definition type_traits.hpp:57
Definition seq.hpp:13
Definition type_traits.hpp:17
Definition tensor.hpp:28
Definition threefry.hpp:24
Definition als.hpp:12
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 using the T...
Definition learn_theta.hpp:59
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 constexpr auto zero()
Definition zero.hpp:12
static constexpr auto unit()
Definition unit.hpp:12
static Float< 64 > getTimeInSeconds()
Definition stopwatch.hpp:23