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
#include "
record.h
"
10
11
using namespace
std
;
12
using
Brush::Node
;
13
using
Brush::DataType
;
14
15
namespace
Brush
{
namespace
Simpl
{
16
class
Constants_simplifier
17
{
18
public
:
20
template
<ProgramType P>
21
Program<P>
simplify_tree
(
22
Program<P>
& program,
const
SearchSpace
&ss,
const
Dataset
&d,
23
SimplificationRecords
* records =
nullptr
)
24
{
25
using
RetType =
26
typename
std::conditional_t<P ==
PT::Regressor
, ArrayXf,
27
std::conditional_t<P ==
PT::Representer
, ArrayXXf, ArrayXf
28
>>;
29
30
// create a copy of the tree
31
Program<P>
simplified_program(program);
32
33
// iterate over the tree, trying to replace each node with a constant, and keeping the change if the pred does not change.
34
TreeIter
spot = simplified_program.
Tree
.begin();
35
while
(spot != simplified_program.
Tree
.end())
36
{
37
Node
n = spot.node->data;
38
39
// This is avoiding using booleans.
40
// non-wheightable nodes are not simplified. TODO: revisit this and see if they should (then implement it)
41
if
(
Isnt<NodeType::Terminal, NodeType::Constant, NodeType::MeanLabel>
(n.
node_type
)
42
&& n.
get_prob_change
()>0
43
&&
IsWeighable
(n.
ret_type
)
44
)
45
{
46
// TODO: check if holds alternative and use this information, instead of making it templated. Also, return void.
47
// get new_pred with predictions after simplification
48
VectorXf branch_pred;
49
if
constexpr
(P==
ProgramType::Regressor
|| P==
ProgramType::BinaryClassifier
)
50
{
51
RetType pred = (*spot.node).predict<RetType>(d);
52
branch_pred = pred.template cast<float>();
53
}
54
else
if
constexpr
(P==
ProgramType::MulticlassClassifier
)
55
{
56
// A multiclass tree contains ArrayF logit branches
57
// below its MatrixF Softmax root. Evaluate each
58
// node using its own return type; treating a logit
59
// branch as MatrixF requests an invalid dispatch
60
// callable (for example Sum(MatrixF)).
61
if
(n.
ret_type
==
DataType::MatrixF
)
62
{
63
ArrayXXf out = (*spot.node).
template
predict<ArrayXXf>(d);
64
auto
argmax =
Function<NodeType::ArgMax>
{};
65
branch_pred = ArrayXf(argmax(out).
template
cast<float>());
66
}
67
else
68
{
69
branch_pred = (*spot.node).
template
predict<ArrayXf>(d);
70
}
71
}
72
else
73
{
74
HANDLE_ERROR_THROW
(
"No predict available for the class."
);
75
}
76
77
if
(
variance
(branch_pred) < 1e-6)
78
{
79
// get constant equivalent to its argtype (all data types should have
80
// a constant defined in the search space for its given type). It will be
81
// the last node of the terminal map for the given type
82
Node
cte = ss.
terminal_map
.at(n.
ret_type
).at(
83
ss.
terminal_map
.at(n.
ret_type
).size()-1);
84
85
cte.
W
= branch_pred.mean();
86
87
string
original;
88
if
(records)
89
original = spot.node->get_model();
90
91
simplified_program.
Tree
.erase_children(spot);
92
spot = simplified_program.
Tree
.replace(spot, cte);
93
94
if
(records)
95
records->push_back({0, 0,
"constants"
,
96
DataTypeName
.at(n.
ret_type
),
97
original, spot.node->get_model(), 0.0f});
98
}
99
}
100
++spot;
101
}
102
program.
Tree
= simplified_program.
Tree
;
103
return
simplified_program;
104
}
105
106
Constants_simplifier
();
107
~Constants_simplifier
();
108
private
:
109
110
};
111
}
// Simply
112
}
// Brush
113
114
#endif
Brush::Data::Dataset
holds variable type data.
Definition
data.h:51
Brush::Simpl::Constants_simplifier
Definition
constants.h:17
Brush::Simpl::Constants_simplifier::Constants_simplifier
Constants_simplifier()
Definition
constants.cpp:8
Brush::Simpl::Constants_simplifier::simplify_tree
Program< P > simplify_tree(Program< P > &program, const SearchSpace &ss, const Dataset &d, SimplificationRecords *records=nullptr)
Definition
constants.h:21
HANDLE_ERROR_THROW
#define HANDLE_ERROR_THROW(err)
Definition
error.h:27
init.h
Brush::Simpl
Definition
constants.cpp:4
Brush::Simpl::SimplificationRecords
std::vector< SimplificationRecord > SimplificationRecords
Definition
record.h:22
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::DataType::MatrixF
@ MatrixF
Definition
types.h:149
Brush::TreeIter
tree< Node >::pre_order_iterator TreeIter
Definition
search_space.h:45
Brush::DataTypeName
map< DataType, string > DataTypeName
Definition
data.cpp:14
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
record.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:280
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.18.0