Brush C++ API
A flexible interpretable machine learning framework
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"
8#include "../util/utils.h"
9#include "record.h"
10
11using namespace std;
12using Brush::Node;
13using Brush::DataType;
14
15namespace Brush { namespace Simpl{
17 {
18 public:
20 template <ProgramType P>
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)
42 && n.get_prob_change()>0
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;
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)).
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",
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
108 private:
109
110 };
111} // Simply
112} // Brush
113
114#endif
holds variable type data.
Definition data.h:51
Program< P > simplify_tree(Program< P > &program, const SearchSpace &ss, const Dataset &d, SimplificationRecords *records=nullptr)
Definition constants.h:21
#define HANDLE_ERROR_THROW(err)
Definition error.h:27
std::vector< SimplificationRecord > SimplificationRecords
Definition record.h:22
float variance(const ArrayXf &v)
calculate variance
Definition utils.cpp:317
< nsga2 selection operator for getting the front
Definition bandit.cpp:3
auto Isnt(DataType dt) -> bool
Definition node.h:48
auto IsWeighable() noexcept -> bool
Definition node.h:51
DataType
data types.
Definition types.h:143
tree< Node >::pre_order_iterator TreeIter
map< DataType, string > DataTypeName
Definition data.cpp:14
STL namespace.
class holding the data for a node in a tree.
Definition node.h:89
float get_prob_change() const
Definition node.h:280
NodeType node_type
the node type
Definition node.h:94
DataType ret_type
return data type
Definition node.h:97
float W
the weights of the node. also used for splitting thresholds.
Definition node.h:122
An individual program, a.k.a. model.
Definition program.h:49
tree< Node > Tree
fitness
Definition program.h:72
Holds a search space, consisting of operations and terminals and functions, and methods to sample tha...
unordered_map< DataType, vector< Node > > terminal_map
Maps return types to terminals.