Skip to content

Instantly share code, notes, and snippets.

@luowanqian
Last active May 4, 2019 08:03
Show Gist options
  • Save luowanqian/9069ed22703ae2b8a9c7ced06702ad09 to your computer and use it in GitHub Desktop.
Save luowanqian/9069ed22703ae2b8a9c7ced06702ad09 to your computer and use it in GitHub Desktop.
SMACv3包使用,项目主页:https://github.com/automl/SMAC3
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import logging\n",
"import numpy as np\n",
"from sklearn.svm import SVC\n",
"from sklearn.datasets import load_iris\n",
"from sklearn.model_selection import cross_val_score\n",
"\n",
"# Import ConfigSpace and different types of parameters\n",
"from smac.configspace import ConfigurationSpace\n",
"from ConfigSpace.hyperparameters import CategoricalHyperparameter\n",
"from ConfigSpace.hyperparameters import UniformFloatHyperparameter\n",
"from ConfigSpace.hyperparameters import UniformIntegerHyperparameter\n",
"from ConfigSpace.conditions import InCondition\n",
"\n",
"from smac.tae.execute_func import ExecuteTAFuncDict\n",
"from smac.scenario.scenario import Scenario\n",
"from smac.facade.smac_facade import SMAC"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Packages信息"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Scikit-learn version: 0.19.2\n",
"SMAC3 version: 0.8.0\n",
"ConfigSpace version: 0.4.10\n"
]
}
],
"source": [
"import sklearn as tmp_sklearn\n",
"import smac as tmp_smac\n",
"import ConfigSpace as tmp_cs\n",
"\n",
"print('Scikit-learn version: {}'.format(tmp_sklearn.__version__))\n",
"print('SMAC3 version: {}'.format(tmp_smac.__version__))\n",
"print('ConfigSpace version: {}'.format(tmp_cs.__version__))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 加载数据"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"iris = load_iris()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 创建参数空间"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[gamma | kernel in {'rbf', 'poly', 'sigmoid'},\n",
" gamma_value | gamma in {'value'}]"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cs = ConfigurationSpace()\n",
"\n",
"# parameters 'kernel' and 'C'\n",
"param_kernel = CategoricalHyperparameter(name='kernel', \n",
" choices=['linear', 'rbf', 'poly', 'sigmoid'],\n",
" default_value='rbf')\n",
"param_c = UniformFloatHyperparameter(name='C', lower=0.001,\n",
" upper=1000.0, default_value=1.0)\n",
"cs.add_hyperparameters([param_kernel, param_c])\n",
"\n",
"# parameters 'degree' and 'coef0'\n",
"param_degree = UniformIntegerHyperparameter(name='degree',\n",
" lower=1, upper=5,\n",
" default_value=3)\n",
"param_coef0 = UniformFloatHyperparameter(name='coef0',\n",
" lower=0.0, upper=10.0,\n",
" default_value=0.0)\n",
"cs.add_hyperparameters([param_degree, param_coef0])\n",
"cond_degree = InCondition(child=param_degree,\n",
" parent=param_kernel,\n",
" values=['poly'])\n",
"cond_coef0 = InCondition(child=param_coef0,\n",
" parent=param_kernel,\n",
" values=['poly', 'sigmoid'])\n",
"cs.add_conditions([cond_degree, cond_coef0])\n",
"\n",
"# parameters 'gamma'\n",
"param_gamma = CategoricalHyperparameter(name='gamma',\n",
" choices=['auto', 'value'],\n",
" default_value='auto')\n",
"param_gamma_value = UniformFloatHyperparameter(name='gamma_value',\n",
" lower=0.0001,\n",
" upper=8,\n",
" default_value=1.0)\n",
"cs.add_hyperparameters([param_gamma, param_gamma_value])\n",
"cond_gamma_value = InCondition(child=param_gamma_value,\n",
" parent=param_gamma,\n",
" values=['value'])\n",
"cond_gamma = InCondition(child=param_gamma,\n",
" parent=param_kernel,\n",
" values=['rbf', 'poly', 'sigmoid'])\n",
"cs.add_conditions([cond_gamma, cond_gamma_value])"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Configuration:\n",
" C, Value: 842.0606951038752\n",
" gamma, Value: 'value'\n",
" gamma_value, Value: 1.2857275855783623\n",
" kernel, Value: 'rbf'"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cs.sample_configuration()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 参数优化1"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"def svm_from_cfg(cfg):\n",
" \"\"\" Creates a SVM based on a configuration and evaluates it on the\n",
" iris-dataset using cross-validation.\n",
" Parameters:\n",
" -----------\n",
" cfg: Configuration (ConfigSpace.ConfigurationSpace.Configuration)\n",
" Configuration containing the parameters.\n",
" Configurations are indexable!\n",
" Returns:\n",
" --------\n",
" A crossvalidated mean score for the svm on the loaded data-set.\n",
" \"\"\"\n",
"\n",
" # For deactivated parameters, the configuration stores None-values.\n",
" # This is not accepted by the SVM, so we remove them.\n",
" cfg = {k : cfg[k] for k in cfg if cfg[k]}\n",
" \n",
" if 'gamma' in cfg:\n",
" cfg['gamma'] = cfg['gamma_value'] if cfg['gamma'] == 'value' else 'auto'\n",
" cfg.pop('gamma_value', None) # Remove 'gamma_value'\n",
" \n",
" clf = SVC(**cfg, random_state=42)\n",
" scores = cross_val_score(clf, iris.data, iris.target, cv=5)\n",
" \n",
" return 1 - np.mean(scores) # Minimize"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"__使用默认参数进行测试__"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Default Value: 0.019999999999999907\n"
]
}
],
"source": [
"def_value = svm_from_cfg(cs.get_default_configuration())\n",
"print(\"Default Value: {}\".format(def_value))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"__开始优化__"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Optimizing! Depending on your machine, this might take a few minutes.\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"WARNING:smac.intensification.intensification.Intensifier:Challenger was the same as the current incumbent; Skipping challenger\n",
"WARNING:smac.intensification.intensification.Intensifier:Challenger was the same as the current incumbent; Skipping challenger\n",
"WARNING:smac.intensification.intensification.Intensifier:Challenger was the same as the current incumbent; Skipping challenger\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Optimized Value: 0.013333333333333308\n",
"Optimaized Configuration:\n",
" C, Value: 0.9834683358573658\n",
" kernel, Value: 'linear'\n",
"\n"
]
}
],
"source": [
"# logging.basicConfig(level=logging.INFO) # logging.DEBUG for debug output\n",
"\n",
"# Scenario object\n",
"scenario = Scenario({'run_obj': 'quality', # we optimize quality (alternatively runtime)\n",
" 'runcount-limit': 200, # maximum function evaluations\n",
" 'cs': cs, # configuration space\n",
" 'deterministic': 'true'})\n",
"\n",
"# Optimize, using a SMAC-object\n",
"print('Optimizing! Depending on your machine, this might take a few minutes.')\n",
"smac = SMAC(scenario=scenario, rng=np.random.RandomState(42),\n",
" tae_runner=svm_from_cfg)\n",
"\n",
"incumbent = smac.optimize()\n",
"\n",
"inc_value = svm_from_cfg(incumbent)\n",
"\n",
"print('Optimized Value: {}'.format(inc_value))\n",
"print('Optimaized {}'.format(incumbent))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 参数优化2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"参考Issue: [target functions with additional input arguments](https://github.com/automl/SMAC3/issues/255) 可以传多个参数给目标函数"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"def svm_from_cfg2(cfg, dataset):\n",
" \"\"\" Creates a SVM based on a configuration and evaluates it on the\n",
" dataset using cross-validation.\n",
" Parameters:\n",
" -----------\n",
" cfg: Configuration (ConfigSpace.ConfigurationSpace.Configuration)\n",
" Configuration containing the parameters.\n",
" Configurations are indexable!\n",
" dataset: Dataset\n",
" Returns:\n",
" --------\n",
" A crossvalidated mean score for the svm on the loaded data-set.\n",
" \"\"\"\n",
"\n",
" # For deactivated parameters, the configuration stores None-values.\n",
" # This is not accepted by the SVM, so we remove them.\n",
" cfg = {k : cfg[k] for k in cfg if cfg[k]}\n",
" \n",
" if 'gamma' in cfg:\n",
" cfg['gamma'] = cfg['gamma_value'] if cfg['gamma'] == 'value' else 'auto'\n",
" cfg.pop('gamma_value', None) # Remove 'gamma_value'\n",
" \n",
" clf = SVC(**cfg, random_state=42)\n",
" scores = cross_val_score(clf, dataset.data, dataset.target, cv=5)\n",
" \n",
" return 1 - np.mean(scores) # Minimize"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"__使用默认参数进行测试__"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Default Value: 0.019999999999999907\n"
]
}
],
"source": [
"def_value = svm_from_cfg2(cs.get_default_configuration(), iris)\n",
"print(\"Default Value: {}\".format(def_value))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"__开始优化__"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Optimizing! Depending on your machine, this might take a few minutes.\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"WARNING:smac.intensification.intensification.Intensifier:Challenger was the same as the current incumbent; Skipping challenger\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Optimized Value: 0.013333333333333308\n",
"Optimaized Configuration:\n",
" C, Value: 2.5299110449032196\n",
" coef0, Value: 8.748561065676306\n",
" degree, Value: 1\n",
" gamma, Value: 'auto'\n",
" kernel, Value: 'poly'\n",
"\n"
]
}
],
"source": [
"# logging.basicConfig(level=logging.INFO) # logging.DEBUG for debug output\n",
"\n",
"# Scenario object\n",
"scenario = Scenario({'run_obj': 'quality', # we optimize quality (alternatively runtime)\n",
" 'runcount-limit': 200, # maximum function evaluations\n",
" 'cs': cs, # configuration space\n",
" 'deterministic': 'true'})\n",
"\n",
"# Optimize, using a SMAC-object\n",
"print('Optimizing! Depending on your machine, this might take a few minutes.')\n",
"smac = SMAC(scenario=scenario, rng=np.random.RandomState(42),\n",
" tae_runner=lambda c: svm_from_cfg2(c, dataset=iris))\n",
"\n",
"incumbent = smac.optimize()\n",
"\n",
"inc_value = svm_from_cfg(incumbent)\n",
"\n",
"print('Optimized Value: {}'.format(inc_value))\n",
"print('Optimaized {}'.format(incumbent))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 参考"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"1. [target functions with additional input arguments](https://github.com/automl/SMAC3/issues/255)\n",
"2. [SMAC Quick Start](https://automl.github.io/SMAC3/master/quickstart.html)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.8"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment