Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug fixes #298

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
# cache-env: true
-
name: add docs environment dependencies
uses: mamba-org/provision-with-micromamba@main
uses: mamba-org/setup-micromamba@v1
with:
environment-file: environment.yml
cache-env: true
Expand Down
1 change: 1 addition & 0 deletions ci/ci-environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ dependencies:
- sphinx-material
- recommonmark
- nbsphinx
- lxml_html_clean
- matplotlib
- jupyter
- seaborn
Expand Down
1 change: 1 addition & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
'sphinx_math_dollar',
# 'recommonmark',
'nbsphinx',
'lxml_html_clean',
# "sphinx.ext.viewcode",
# External stuff
]
Expand Down
1 change: 1 addition & 0 deletions docs/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ myst-parser
nbsphinx
sphinx-material
sphinx-math-dollar
lxml_html_clean
9 changes: 5 additions & 4 deletions feat/feat.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ def __init__(self,
softmax_norm=False,
save_pop=0,
normalize=True,
val_from_arch=True,
val_from_arch=False,
corr_delete_mutate=False,
simplify=0.0,
protected_groups="",
Expand Down Expand Up @@ -316,8 +316,8 @@ def predict_archive(self,X,Z=None,front=False):
archive = self.cfeat_.get_archive(front)
preds = []
for ind in archive:
if ind['id'] == 9234:
print('individual:',json.dumps(ind,indent=2))
# if ind['id'] == 9234:
# print('individual:',json.dumps(ind,indent=2))
tmp = {}
tmp['id'] = ind['id']
tmp['y_pred'] = self.cfeat_.predict_archive(ind['id'], X)
Expand Down Expand Up @@ -399,6 +399,7 @@ def get_representation(self): return self.cfeat_.get_representation()
def get_model(self, sort=True): return self.cfeat_.get_model(sort)
def get_coefs(self): return self.cfeat_.get_coefs()
def get_n_params(self): return self.cfeat_.get_n_params()
def get_complexity(self): return self.cfeat_.get_complexity()
def get_dim(self): return self.cfeat_.get_dim()
def get_n_nodes(self): return self.cfeat_.get_n_nodes()

Expand Down Expand Up @@ -432,7 +433,7 @@ def fit(self,X,y,zfile=None,zids=None):
])):
raise ValueError('y must be a contiguous set of labels from ',
'0 to n_classes. y contains the values {}'.format(
np.unique(np.asarray(y)))
self.classes_)
)

super().fit(X,y)
Expand Down
6 changes: 6 additions & 0 deletions src/eval/metrics.cc
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ namespace FT

return loss;
}

/// 1 - balanced accuracy
float bal_zero_one_loss(const VectorXf& y, const VectorXf& yhat,
VectorXf& loss, const vector<float>& class_weights)
Expand Down Expand Up @@ -406,6 +407,7 @@ namespace FT
// set loss vectors if third argument supplied
loss = (yhat.cast<int>().array() != y.cast<int>().array()).cast<float>();

// 1 - accuracy (so it becomes a minimization problem)
return 1.0 - class_accuracies.mean();
}

Expand Down Expand Up @@ -435,7 +437,11 @@ namespace FT
float zero_one_loss(const VectorXf& y, const VectorXf& yhat, VectorXf& loss,
const vector<float>& class_weights)
{
// Feat's update_best and sel/surv steps always handles scores as
// minimization problems, so we need to invert the loss here. That's
// why we account for mismatches instead of correct classifications:
loss = (yhat.cast<int>().array() != y.cast<int>().array()).cast<float>();

//TODO: weight loss by sample weights
return loss.mean();
}
Expand Down
22 changes: 14 additions & 8 deletions src/feat.cc
Original file line number Diff line number Diff line change
Expand Up @@ -567,8 +567,12 @@ int Feat::get_n_params(){ return best_ind.get_n_params(); }
int Feat::get_dim(){ return best_ind.get_dim(); }

///get dimensionality of best
int Feat::get_complexity(){ return best_ind.get_complexity(); }

int Feat::get_complexity(){
// Making sure it is calculated before returning it
if (best_ind.get_complexity()==0)
best_ind.set_complexity();
return best_ind.get_complexity();
}

/// return the number of nodes in the best model
int Feat::get_n_nodes(){ return best_ind.program.size(); }
Expand Down Expand Up @@ -707,15 +711,14 @@ void Feat::run_generation(unsigned int g,
pop.update(survivors);
logger.log("survivors:\n" + pop.print_eqns(), 3);

// we need to update best, so min_loss_v is updated inside stats
logger.log("update best...",2);
bool updated_best = update_best(d);

logger.log("calculate stats...",2);
calculate_stats(d);

if (params.max_stall > 0)
update_stall_count(stall_count, updated_best);

logger.log("update objectives...",2);
if ( (use_arch || params.verbosity>1) || !logfile.empty()) {
// set objectives to make sure they are reported in log/verbose/arch
#pragma omp parallel for
Expand All @@ -727,6 +730,9 @@ void Feat::run_generation(unsigned int g,
if (use_arch)
archive.update(pop,params);

logger.log("calculate stats...",2);
calculate_stats(d);

if(params.verbosity>1)
print_stats(log, fraction);
else if(params.verbosity == 1)
Expand Down Expand Up @@ -1293,7 +1299,7 @@ ArrayXXf Feat::predict_proba(MatrixXf& X)
}


bool Feat::update_best(const DataRef& d, bool validation)
bool Feat::update_best(const DataRef& d, bool val)
{
float bs;
bs = this->min_loss_v;
Expand Down Expand Up @@ -1463,7 +1469,7 @@ void Feat::print_stats(std::ofstream& log, float fraction)
<< stats.min_loss.back() << " ("
<< stats.med_loss.back() << ")\n"
<< "Val Loss (Med): "
<< this->min_loss_v << " (" << stats.med_loss_v.back() << ")\n"
<< stats.min_loss_v.back() << " (" << stats.med_loss_v.back() << ")\n"
<< "Median Size (Max): "
<< stats.med_size.back() << " (" << max_size << ")\n"
<< "Time (s): " << timer << "\n";
Expand Down Expand Up @@ -1553,7 +1559,7 @@ void Feat::log_stats(std::ofstream& log)
log << params.current_gen << sep
<< timer.Elapsed().count() << sep
<< stats.min_loss.back() << sep
<< this->min_loss_v << sep
<< stats.min_loss_v.back() << sep
<< stats.med_loss.back() << sep
<< stats.med_loss_v.back() << sep
<< stats.med_size.back() << sep
Expand Down
4 changes: 2 additions & 2 deletions src/feat.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class Feat
// string logfile="", int max_time=-1, bool residual_xo = false,
// bool stagewise_xo = false, bool stagewise_tol = true,
// bool softmax_norm=false, int save_pop=0, bool normalize=true,
// bool val_from_arch=true, bool corr_delete_mutate=false,
// bool val_from_arch=false, bool corr_delete_mutate=false,
// float simplify=0.0, string protected_groups="",
// bool tune_initial=false, bool tune_final=true,
// string starting_pop="");
Expand Down Expand Up @@ -325,7 +325,7 @@ class Feat
int get_n_params();
///get dimensionality of best
int get_dim();
///get dimensionality of best
///get complexity of best
int get_complexity();
///return population as string
vector<nl::json> get_archive(bool front);
Expand Down
1 change: 1 addition & 0 deletions src/pybind.cc
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ PYBIND11_MODULE(_feat, m)
.def("load", &Feat::load)
.def("get_representation", &Feat::get_representation)
.def("get_n_params", &Feat::get_n_params)
.def("get_complexity", &Feat::get_complexity)
.def("get_dim", &Feat::get_dim)
.def("get_n_nodes", &Feat::get_n_nodes)
.def("get_model", &Feat::get_model, py::arg("sort") = true)
Expand Down
52 changes: 52 additions & 0 deletions tests/evaluationTests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,58 @@ TEST(Evaluation, mse)
ASSERT_TRUE(score == 28.5);
}

TEST(Evaluation, accuracy)
{
// test zero one loss

Feat ft = make_estimator(100, 10, "LinearRidgeRegression", false, 1, 666);

VectorXf yhat(10), y(10), res(10), loss(10);

y << 0.0,
1.0,
0.0,
0.0,
1.0,
0.0,
0.0,
1.0,
0.0,
0.0;

yhat << 0.0,
1.0,
1.0,
0.0,
0.0,
1.0,
1.0,
0.0,
0.0,
0.0;

res << 0.0,
0.0,
1.0,
0.0,
1.0,
1.0,
1.0,
1.0,
0.0,
0.0;

float score = zero_one_loss(y, yhat, loss, ft.params.class_weights);

if (loss != res)
{
std::cout << "loss:" << loss.transpose() << "\n";
std::cout << "res:" << res.transpose() << "\n";
}
ASSERT_TRUE(loss == res);
ASSERT_EQ(((int)(score*1000000)), 500000);
}

TEST(Evaluation, bal_accuracy)
{
// test balanced zero one loss
Expand Down
3 changes: 2 additions & 1 deletion tests/wrappertest.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ def test_sklearn_api(self):
'check_fit2d_1sample',
'check_fit2d_1feature',
'check_transformer_data_not_an_array',
'check_transformer_preserve_dtypes'
'check_transformer_preserve_dtypes',
'check_estimators_dtypes'
]
for est, check in check_generator2:
time_to_go=False
Expand Down
Loading