Brush C++ API
A flexible interpretable machine learning framework
Toggle main menu visibility
Loading...
Searching...
No Matches
constants.h
Go to the documentation of this file.
1
#ifndef CONSTANTS_H
2
#define CONSTANTS_H
3
4
#include "
../init.h
"
5
#include "
../types.h
"
6
#include "
../program/program.h
"
7
#include "
../vary/search_space.h
"
8
#include "
../util/utils.h
"
9
10
using namespace
std
;
11
using
Brush::Node
;
12
using
Brush::DataType
;
13
14
namespace
Brush
{
namespace
Simpl
{
15
class
Constants_simplifier
16
{
17
public
:
18
template
<ProgramType P>
19
Program<P>
simplify_tree
(
20
Program<P>
& program,
const
SearchSpace
&ss,
const
Dataset
&d)
21
{
22
using
RetType =
23
typename
std::conditional_t<P ==
PT::Regressor
, ArrayXf,
24
std::conditional_t<P ==
PT::Representer
, ArrayXXf, ArrayXf
25
>>;
26
27
// create a copy of the tree
28
Program<P>
simplified_program(program);
29
30
// iterate over the tree, trying to replace each node with a constant, and keeping the change if the pred does not change.
31
TreeIter
spot = simplified_program.
Tree
.begin();
32
while
(spot != simplified_program.
Tree
.end())
33
{
34
Node
n = spot.node->data;
35
36
// This is avoiding using booleans.
37
// non-wheightable nodes are not simplified. TODO: revisit this and see if they should (then implement it)
38
if
(
Isnt<NodeType::Terminal, NodeType::Constant, NodeType::MeanLabel>
(n.
node_type
)
39
&& n.
get_prob_change
()>0
40
&&
IsWeighable
(n.
ret_type
)
41
)
42
{
43
// TODO: check if holds alternative and use this information, instead of making it templated. Also, return void.
44
// get new_pred with predictions after simplification
45
VectorXf branch_pred;
46
if
constexpr
(P==
ProgramType::Regressor
|| P==
ProgramType::BinaryClassifier
)
47
{
48
RetType pred = (*spot.node).predict<RetType>(d);
49
branch_pred = pred.template cast<float>();
50
}
51
else
if
constexpr
(P==
ProgramType::MulticlassClassifier
)
52
{
53
ArrayXXf out = (*spot.node).
template
predict<ArrayXXf>(d);
54
auto
argmax =
Function<NodeType::ArgMax>
{};
55
branch_pred = ArrayXf(argmax(out).
template
cast<float>());
56
}
57
else
58
{
59
HANDLE_ERROR_THROW
(
"No predict available for the class."
);
60
}
61
62
if
(
variance
(branch_pred) < 1e-6)
63
{
64
// get constant equivalent to its argtype (all data types should have
65
// a constant defined in the search space for its given type). It will be
66
// the last node of the terminal map for the given type
67
Node
cte = ss.
terminal_map
.at(n.
ret_type
).at(
68
ss.
terminal_map
.at(n.
ret_type
).size()-1);
69
70
cte.
W
= branch_pred.mean();
71
simplified_program.
Tree
.erase_children(spot);
72
spot = simplified_program.
Tree
.replace(spot, cte);
73
}
74
}
75
++spot;
76
}
77
program.
Tree
= simplified_program.
Tree
;
78
return
simplified_program;
79
}
80
81
Constants_simplifier
();
82
~Constants_simplifier
();
83
private
:
84
85
};
86
}
// Simply
87
}
// Brush
88
89
#endif
Brush::Data::Dataset
holds variable type data.
Definition
data.h:51
Brush::Simpl::Constants_simplifier::simplify_tree
Program< P > simplify_tree(Program< P > &program, const SearchSpace &ss, const Dataset &d)
Definition
constants.h:19
Brush::Simpl::Constants_simplifier::Constants_simplifier
Constants_simplifier()
Definition
constants.cpp:8
Brush::Simpl::Constants_simplifier::~Constants_simplifier
~Constants_simplifier()
Definition
constants.cpp:84
HANDLE_ERROR_THROW
#define HANDLE_ERROR_THROW(err)
Definition
error.h:27
init.h
Brush::Simpl
Definition
constants.cpp:4
Brush::Util::variance
float variance(const ArrayXf &v)
calculate variance
Definition
utils.cpp:317
Brush
< nsga2 selection operator for getting the front
Definition
bandit.cpp:3
Brush::Isnt
auto Isnt(DataType dt) -> bool
Definition
node.h:48
Brush::IsWeighable
auto IsWeighable() noexcept -> bool
Definition
node.h:51
Brush::DataType
DataType
data types.
Definition
types.h:143
Brush::TreeIter
tree< Node >::pre_order_iterator TreeIter
Definition
search_space.h:45
Brush::ProgramType::BinaryClassifier
@ BinaryClassifier
Definition
types.h:72
Brush::ProgramType::Regressor
@ Regressor
Definition
types.h:71
Brush::ProgramType::MulticlassClassifier
@ MulticlassClassifier
Definition
types.h:73
Brush::ProgramType::Representer
@ Representer
Definition
types.h:74
std
STL namespace.
program.h
search_space.h
Brush::Function
Definition
functions.h:48
Brush::Node
class holding the data for a node in a tree.
Definition
node.h:89
Brush::Node::get_prob_change
float get_prob_change() const
Definition
node.h:276
Brush::Node::node_type
NodeType node_type
the node type
Definition
node.h:94
Brush::Node::ret_type
DataType ret_type
return data type
Definition
node.h:97
Brush::Node::W
float W
the weights of the node. also used for splitting thresholds.
Definition
node.h:122
Brush::Program
An individual program, a.k.a. model.
Definition
program.h:49
Brush::Program::Tree
tree< Node > Tree
fitness
Definition
program.h:72
Brush::SearchSpace
Holds a search space, consisting of operations and terminals and functions, and methods to sample tha...
Definition
search_space.h:84
Brush::SearchSpace::terminal_map
unordered_map< DataType, vector< Node > > terminal_map
Maps return types to terminals.
Definition
search_space.h:115
types.h
utils.h
src
simplification
constants.h
Generated by
1.17.0