-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #95 from rodrigo-arenas/0.9.0
Adaptive Learning
- Loading branch information
Showing
27 changed files
with
1,012 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,3 +16,4 @@ numpydoc | |
nbsphinx | ||
tensorflow>=2.0.0 | ||
tqdm>=4.61.1 | ||
tk |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
Schedules | ||
--------- | ||
|
||
.. currentmodule:: sklearn_genetic.schedules | ||
|
||
.. autosummary:: | ||
base.BaseAdapter | ||
ExponentialAdapter | ||
InverseAdapter | ||
PotentialAdapter | ||
|
||
.. autoclass:: sklearn_genetic.schedules.base.BaseAdapter | ||
:members: | ||
:undoc-members: False | ||
|
||
.. autoclass:: ExponentialAdapter | ||
:members: | ||
:undoc-members: False | ||
|
||
.. autoclass:: InverseAdapter | ||
:members: | ||
:undoc-members: False | ||
|
||
.. autoclass:: PotentialAdapter | ||
:members: | ||
:undoc-members: False |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"source": [ | ||
"# Digits Adaptive Learning\n", | ||
"\n", | ||
"In this example, we want to create a decay strategy for the mutation probability,\n", | ||
"and an ascend strategy for the crossover probability,\n", | ||
"lets call them $p_{mt}(t; \\alpha)$ and $p_{cr}(t; \\alpha)$ respectively;\n", | ||
"this will enable the optimizer to explore more diverse solutions in the first iterations.\n", | ||
"Take into account that on this scenario, we must be careful on choosing $\\alpha, p_0, p_f$,\n", | ||
"this is because the evolutionary implementation requires that:\n", | ||
"\n", | ||
"\n", | ||
"\n", | ||
"$p_{mt}(t; \\alpha) + p_{cr}(t; \\alpha) <= 1; \\forall t $\n", | ||
"\n", | ||
"The same idea can be used for hypeparameter tuning or feature selection.\n" | ||
], | ||
"metadata": { | ||
"collapsed": false | ||
} | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"source": [ | ||
"### Import the data and split it in train and test sets" | ||
], | ||
"metadata": { | ||
"collapsed": false | ||
} | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"collapsed": true | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"from sklearn_genetic import GASearchCV\n", | ||
"from sklearn_genetic import ExponentialAdapter\n", | ||
"from sklearn_genetic.space import Continuous, Categorical, Integer\n", | ||
"from sklearn.ensemble import RandomForestClassifier\n", | ||
"from sklearn.model_selection import train_test_split, StratifiedKFold\n", | ||
"from sklearn.datasets import load_digits\n", | ||
"from sklearn.metrics import accuracy_score\n", | ||
"\n", | ||
"data = load_digits()\n", | ||
"n_samples = len(data.images)\n", | ||
"X = data.images.reshape((n_samples, -1))\n", | ||
"y = data['target']\n", | ||
"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"source": [ | ||
"### Create the adaptive strategy" | ||
], | ||
"metadata": { | ||
"collapsed": false | ||
} | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"outputs": [], | ||
"source": [ | ||
"mutation_adapter = ExponentialAdapter(initial_value=0.8, end_value=0.2, adaptive_rate=0.1)\n", | ||
"crossover_adapter = ExponentialAdapter(initial_value=0.2, end_value=0.8, adaptive_rate=0.1)" | ||
], | ||
"metadata": { | ||
"collapsed": false, | ||
"pycharm": { | ||
"name": "#%%\n" | ||
} | ||
} | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"source": [ | ||
"### Define the classifier to tune" | ||
], | ||
"metadata": { | ||
"collapsed": false | ||
} | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"outputs": [], | ||
"source": [ | ||
"clf = RandomForestClassifier()\n", | ||
"param_grid = {'min_weight_fraction_leaf': Continuous(0.01, 0.5, distribution='log-uniform'),\n", | ||
" 'bootstrap': Categorical([True, False]),\n", | ||
" 'max_depth': Integer(2, 30),\n", | ||
" 'max_leaf_nodes': Integer(2, 35),\n", | ||
" 'n_estimators': Integer(100, 300)}\n", | ||
"\n", | ||
"cv = StratifiedKFold(n_splits=3, shuffle=True)\n", | ||
"\n", | ||
"evolved_estimator = GASearchCV(estimator=clf,\n", | ||
" cv=cv,\n", | ||
" scoring='accuracy',\n", | ||
" population_size=20,\n", | ||
" generations=25,\n", | ||
" mutation_probability=mutation_adapter,\n", | ||
" crossover_probability=crossover_adapter,\n", | ||
" param_grid=param_grid,\n", | ||
" n_jobs=-1)" | ||
], | ||
"metadata": { | ||
"collapsed": false, | ||
"pycharm": { | ||
"name": "#%%\n" | ||
} | ||
} | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"source": [ | ||
"### Fit the model and see some results" | ||
], | ||
"metadata": { | ||
"collapsed": false | ||
} | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"outputs": [], | ||
"source": [ | ||
"# Train and optimize the estimator\n", | ||
"evolved_estimator.fit(X_train, y_train)\n", | ||
"# Best parameters found\n", | ||
"print(evolved_estimator.best_params_)\n", | ||
"# Use the model fitted with the best parameters\n", | ||
"y_predict_ga = evolved_estimator.predict(X_test)\n", | ||
"print(accuracy_score(y_test, y_predict_ga))\n", | ||
"\n", | ||
"# Saved metadata for further analysis\n", | ||
"print(\"Stats achieved in each generation: \", evolved_estimator.history)\n", | ||
"print(\"Best k solutions: \", evolved_estimator.hof)" | ||
], | ||
"metadata": { | ||
"collapsed": false, | ||
"pycharm": { | ||
"name": "#%%\n" | ||
} | ||
} | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"outputs": [], | ||
"source": [], | ||
"metadata": { | ||
"collapsed": false, | ||
"pycharm": { | ||
"name": "#%%\n" | ||
} | ||
} | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 2 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython2", | ||
"version": "2.7.6" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.