38 this->
epT = 0.01*this->
n;
43 for (
const auto& p : program)
45 cout <<
"( " << p->name;
49 for (
int i = 0;
i < dNode->
arity.at(
'f');
i++) {
50 cout <<
"," << dNode->
W.at(
i);
65 float current_loss, current_val_loss;
66 vector<vector<float>> best_weights;
90 logger.
log(
"=========================",4);
91 logger.
log(
"Iteration,Train Loss,Val Loss,Weights",4);
92 logger.
log(
"=========================",4);
93 for (
int x = 0; x < this->
iters; x++)
103 vector<Trace> stack_trace =
forward_prop(ind, batch_data,
107 auto ml = std::make_shared<ML>(params.
ml,
true,
111 shared_ptr<CLabels> yhat = ml->fit(Phi,
112 batch_data.
y,params,pass,ind.
dtypes);
115 if (!pass || stack_trace.size() ==0 )
118 vector<float> Beta = ml->get_weights();
120 current_loss = this->
cost_func(batch_data.
y, yhat,
125 for (
int i = 0;
i < stack_trace.size(); ++
i)
127 while (!ind.
program.at(roots.at(s))->isNodeDx()) ++s;
137 roots.at(s), Beta.at(s),
142 MatrixXf Phival = ind.
out((*BP_data.
v));
143 logger.
log(
"checking validation fitness",3);
147 shared_ptr<CLabels> y_val = ml->predict(Phival);
148 current_val_loss = this->
cost_func(BP_data.
v->
y, y_val,
150 if (x==0 || current_val_loss < min_loss)
152 min_loss = current_val_loss;
162 if (missteps == patience
170 float alpha = float(x)/float(
iters);
172 this->
epk = (1 - alpha)*this->
epk + alpha*this->
epT;
178 << current_loss <<
","
179 << current_val_loss <<
",";
184 logger.
log(
"=========================",4);
185 logger.
log(
"done=====================",4);
186 logger.
log(
"=========================",4);
196 vector<Trace> stack_trace;
205 vector<ArrayXf>& derivatives)
208 if(!executing.empty()) {
210 BP_NODE bp_node = pop<BP_NODE>(&executing);
212 while (bp_node.
deriv_list.empty() && !executing.empty()) {
213 bp_node = pop<BP_NODE>(&executing);
216 pop<ArrayXf>(&derivatives);
217 if (executing.empty()) {
225 bp_program.push_back(bp_node.
n);
227 derivatives.push_back(pop_front<ArrayXf>(&(bp_node.
deriv_list)));
229 executing.push_back(bp_node);
236 float Beta, shared_ptr<CLabels>& yhat,
241 vector<ArrayXf> derivatives;
246 derivatives.push_back(this->
d_cost_func(d.
y, yhat, sw).array() * Beta);
250 vector<BP_NODE> executing;
255 vector<Node*> bp_program = program.
get_data(start, end);
257 while (bp_program.size() > 0) {
259 Node* node = pop<Node*>(&bp_program);
266 vector<ArrayXf> n_derivatives;
272 for (
int i = 0;
i < node->
arity.at(
'f');
i++) {
276 dNode->
update(derivatives, stack, this->
epk, this->
a);
280 for (
int i = 0;
i < dNode->
arity.at(
'f');
i++) {
281 pop<ArrayXf>(&stack.
f);
283 for (
int i = 0;
i < dNode->
arity.at(
'b');
i++) {
284 pop<ArrayXb>(&stack.
b);
286 for (
int i = 0;
i < dNode->
arity.at(
'c');
i++) {
287 pop<ArrayXi>(&stack.
c);
289 if (!n_derivatives.empty()) {
290 derivatives.push_back(pop_front<ArrayXf>(&n_derivatives));
293 executing.push_back({dNode, n_derivatives});
303 if (!derivatives.empty())
304 pop<ArrayXf>(&derivatives);
318 for (
unsigned i = 0;
i < bp_program.size(); ++
i)
319 bp_program.at(
i) =
nullptr;
void train_test_split(bool shuffle, float split)
splits data into training and validation folds.
data holding X, y, and Z data
void get_batch(Data &db, int batch_size) const
select random subset of data for training weights.
void run(Individual &ind, const Data &d, const Parameters ¶ms)
adapt weights
std::map< string, callback > score_hash
vector< Trace > forward_prop(Individual &ind, const Data &d, MatrixXf &Phi, const Parameters ¶ms)
Return the f_stack.
void backprop(Trace &f_stack, NodeVector &program, int start, int end, float Beta, shared_ptr< CLabels > &yhat, const Data &d, vector< float > sw)
Compute gradients and update weights.
std::map< string, callback > d_score_hash
void next_branch(vector< BP_NODE > &executing, vector< Node * > &bp_program, vector< ArrayXf > &derivatives)
Updates stacks to have proper value on top.
void print_weights(NodeVector &program)
AutoBackProp(string scorer, int iters=1000, float n=0.1, float a=0.9)
individual programs in the population
MatrixXf out(const Data &d, bool predict=false)
calculate program output matrix Phi
vector< char > dtypes
the data types of each column of the
NodeVector program
executable data structure
MatrixXf out_trace(const Data &d, vector< Trace > &stack_trace)
calculate program output while maintaining stack trace
void update(vector< ArrayXf > &gradients, Trace &state, float n, float a)
void derivative(vector< ArrayXf > &gradients, Trace &state, int loc)
Represents nodes in a program.
std::map< char, unsigned int > arity
arity of the operator
virtual bool isNodeDx()
check of node type
string log(string m, int v, string sep="\n") const
print message with verbosity control.
std::map< string, std::pair< vector< ArrayXf >, vector< ArrayXf > > > LongData
VectorXf multi_log_loss(const VectorXf &y, const ArrayXXf &confidences, const vector< float > &class_weights)
multinomial log loss
VectorXf d_squared_difference(const VectorXf &y, const VectorXf &yhat)
VectorXf log_loss(const VectorXf &y, const VectorXf &yhat, const vector< float > &class_weights)
VectorXf squared_difference(const VectorXf &y, const VectorXf &yhat)
VectorXf d_log_loss(const VectorXf &y, const VectorXf &yhat, const vector< float > &class_weights)
VectorXf d_multi_log_loss(const VectorXf &y, shared_ptr< CLabels > &labels, const vector< float > &class_weights)
derivative of multinomial log loss
ArrayXb isinf(const ArrayXf &x)
returns true for elements of x that are infinite
ArrayXb isnan(const ArrayXf &x)
returns true for elements of x that are NaN
std::string to_string(const T &value)
template function to convert objects to string for logging
used for tracing stack outputs for backprop algorithm.
vector< ArrayXf > deriv_list
holds the hyperparameters for Feat.
bool classification
flag to conduct classification rather than
vector< float > class_weights
weights for each class
unsigned int n_classes
number of classes for classification
string ml
machine learner used with Feat
an extension of a vector of unique pointers to nodes
vector< size_t > roots() const
returns indices of root nodes
vector< Node * > get_data(int start=0, int end=0)
returns vector of raw pointers to nodes in [start,end], or all if both are zero
void set_weights(vector< vector< float >> &weights)
vector< vector< float > > get_weights()
size_t subtree(size_t i, char otype='0', string indent="> ") const