Commit e135452d authored by Uwe Köckemann's avatar Uwe Köckemann
Browse files

Minor fix, moved files down one level, experiments notebook fixed

parent 7a892284
Pipeline #623 failed with stages
in 0 seconds
......@@ -35,6 +35,6 @@ RUN sh cplex_studio1210.linux-x86-64.bin -f response.properties \
&& rm -rf /deps
WORKDIR /moving-target
RUN jupyter trust ./jupyter/Regression_Example.ipynb
RUN jupyter trust ./jupyter/*.ipynb
CMD ["./start.sh"]
\ No newline at end of file
* How to Install AIDDL
#+TITLE: AI Integration Languages: A Case Study in Constraint Machine Learning
Clone the repository:
To build the container open a terminal in this folder and run:
#+begin_src
git clone https://github.com/uwe-koeckemann/AIDDL.git
docker build -t moving-target-aiddl .
#+end_src
Enter folder and setup environment variables:
Then, to run the container:
#+begin_src
cd AIDDL
./environment.sh
docker run --rm --publish=8888:8888 moving-target-aiddl:latest
#+end_src
A local link to the jupyter notebook will appear in the console. Open the link
in a browser and select a notebook to try it. For now there is a single notebook
running linear regression through AIDDL.
#+TITLE: AI Integration Languages: A Case Study in Constraint Machine Learning
To build the container open a terminal in this folder and run:
#+begin_src
docker build -t moving-target-aiddl .
#+end_src
Then, to run the container:
#+begin_src
docker run --rm --publish=8888:8888 moving-target-aiddl:latest
#+end_src
A local link to the jupyter notebook will appear in the console. Open the link
in a browser and select a notebook to try it. For now there is a single notebook
running linear regression through AIDDL.
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"from abc import ABC, abstractmethod\n",
"\n",
"import math\n",
"import random as r\n",
"\n",
"import matplotlib.pyplot as plt\n",
"plt.style.use('seaborn-whitegrid')\n",
"import numpy as np\n",
"\n",
"from aiddl_core.representation.symbolic import Symbolic as Sym\n",
"from aiddl_core.representation.variable import Variable as Var\n",
"from aiddl_core.representation.integer import Integer\n",
"from aiddl_core.representation.real import Real\n",
"from aiddl_core.representation.infinity import Infinity\n",
"from aiddl_core.representation.tuple import Tuple\n",
"from aiddl_core.representation.list import List\n",
"from aiddl_core.representation.substitution import Substitution\n",
"from aiddl_core.container.container import Container\n",
"from aiddl_core.tools.combo_iterator import ComboIterator\n",
"from aiddl_core.function.default import get_default_function_registry\n",
"from aiddl_core.tools.logger import Logger\n",
"from aiddl_core.parser.parser import parse_term as parse\n",
"\n",
"from aiddl_network.grpc_function import GrpcFunction\n",
"from aiddl_network.aiddl_grpc_server import AiddlServicer\n",
"from aiddl_network.aiddl_grpc_server import LOADER_URI\n",
"\n",
"import os\n",
"import sys\n",
"module_path = os.path.abspath(os.path.join('../python/moving_target'))\n",
"if module_path not in sys.path:\n",
" sys.path.append(module_path)\n",
"\n",
"from moving_target_cplex import MovingTargetRegCplex\n",
"from moving_target_cplex import MovingTargetClsCplex\n",
"from moving_target_ortools import MovingTargetRegOT\n",
"from moving_target_ortools import MovingTargetClsOT\n",
"\n",
"from factory import get_problem\n",
"from preprocessing import OneHotEncoder\n",
"from tools import CsvLoader\n",
"from utils import didi_r\n",
"from scikit_learn_wrapper import SciKitLearnFunction\n",
"from scikit_learn_wrapper import splitAiddlMlProblem\n",
"\n",
"from sklearn.metrics import accuracy_score\n",
"from sklearn.metrics import mean_squared_error"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Load Data\n",
"\n",
"Create container and function registry and load some example data from a local file."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"scrolled": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Label: \"violentPerPop\"\n",
"Attributes: [\n",
" (\"pctHousOccup\" org.aiddl.term)\n",
" (\"pct12-21\" org.aiddl.term)\n",
" (\"pct16-24\" org.aiddl.term)\n",
" (\"pctWorkMom-6\" org.aiddl.term)\n",
" (\"pctLargHous\" org.aiddl.term)\n",
" (\"ownHousQrange\" org.aiddl.term)\n",
" (\"pct12-29\" org.aiddl.term)\n",
" (\"persPerOccupHous\" org.aiddl.term)\n",
" (\"persPerFam\" org.aiddl.term)\n",
" (\"rentQrange\" org.aiddl.term)\n",
" (\"pctLargHousFam\" org.aiddl.term)\n",
" (\"persPerOwnOccup\" org.aiddl.term)\n",
" (\"whitePerCap\" org.aiddl.term)\n",
" (\"pctEmployProfServ\" org.aiddl.term)\n",
" (\"pctFgnImmig-3\" org.aiddl.term)\n",
" (\"race\" org.aiddl.term)\n",
" (\"violentPerPop\" org.aiddl.term)\n",
" ]\n",
"\n",
"Data (first row): [0.9764342597107102 0.13917940466613032 0.10512037978975924 0.7447776280323449 0.0486362142622412 0.31993957703927495 0.21429739898576802 0.2271062271062273 0.25106382978723407 0.20921544209215442 0.05278678855794751 0.31046931407942224 0.2408048532587981 0.35711430855315746 0.14779575007928955 0.0 0.03989083587243134]\n"
]
}
],
"source": [
"C = Container()\n",
"F = get_default_function_registry(C)\n",
"\n",
"loader = CsvLoader()\n",
"\n",
"test_data = loader.apply(parse('(\"../resources/crime_test.csv\" \",\" \"violentPerPop\")'))\n",
"train_data = loader.apply(parse('(\"../resources/crime_train.csv\" \",\" \"violentPerPop\")'))\n",
"\n",
"x, y = splitAiddlMlProblem(train_data)\n",
"x_test, y_test = splitAiddlMlProblem(test_data)\n",
"\n",
"print(\"Label:\", train_data[Sym(\"label\")])\n",
"print(\"Attributes:\", Logger.pretty_print(train_data[Sym(\"attributes\")], 1))\n",
"print(\"Data (first row):\", train_data[Sym(\"data\")][0])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Without Moving Targets\n",
"\n",
"Fairness and scores without moving targets."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"DIDI (pre-learning): 0.18046885714845506\n",
"DIDI (post-learning): 0.22806466902039563\n",
"MSE (test data): 0.22806466902039563\n"
]
}
],
"source": [
"didi_init = didi_r(np.array(x_test), np.array(y_test), [len(x[0])-1])\n",
"\n",
"print(\"DIDI (pre-learning):\", didi_init)\n",
"\n",
"learner = parse('''{\n",
" py_module:sklearn.ensemble\n",
" py_class:GradientBoostingRegressor\n",
" n_estimators:50\n",
" min_samples_leaf:5\n",
"}''')\n",
"\n",
"f_ML = SciKitLearnFunction(learner)\n",
"F.add_function(Sym(\"mt.learner\"), f_ML)\n",
"f_ML.apply(train_data)\n",
"y_0 = f_ML.predict(x_test)\n",
"\n",
"didi_0 = didi_r(np.array(x_test), np.array(y_0), [len(x[0])-1])\n",
"mse_0 = mean_squared_error(y_0, y_test)\n",
"\n",
"print(\"DIDI (post-learning):\", didi_0)\n",
"print(\"MSE (test data):\", didi_0)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Moving Targets\n",
"\n",
"## Parameters"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"cfg = parse('''(\n",
" n:10 ;; Number of iterations\n",
" alpha:1.0 ;; Parameter alpha\n",
" beta:1.0 ;; Parameter beta\n",
" problem-type:regression ;; classification/regression?\n",
" constraint-solver:ortools ;; Select constraint solver (cplex/pysmt/ortools)\n",
" learner:mt.learner ;; URI of learner (registered above)\n",
")''')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Input Constraints"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"data = parse('''{\n",
" loss-function:MeanAbsoluteError ;; Select loss function\n",
" constraints:{\n",
" (didi-real \"violentPerPop\" [\"race\"] 0.01)\n",
" }\n",
"}''')\n",
"data = data.put_all(train_data)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Create Instance of Moving Targets\n",
"\n",
"Factory method is used to create a version of moving targets for the specified problem type and with the selected constraint solver."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"f_MT = get_problem(str(cfg.get(Sym(\"problem-type\"))), solver=str(cfg.get(Sym(\"constraint-solver\"))))(cfg, F, test_data=x_test)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Apply Moving Targets to Input"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Initializing model\n",
"('didi-real', 'violentPerPop', [15], 0.01)\n",
"Constraint added: ('didi-real', 'violentPerPop', [15], 0.01)\n",
"Constraint satisfaction: False\n",
"Solving\n",
"Constraint satisfaction: False\n",
"Solving\n",
"Constraint satisfaction: False\n",
"Solving\n",
"Constraint satisfaction: False\n",
"Solving\n",
"Constraint satisfaction: False\n",
"Solving\n",
"Constraint satisfaction: False\n",
"Solving\n",
"Constraint satisfaction: False\n",
"Solving\n",
"Constraint satisfaction: False\n",
"Solving\n",
"Constraint satisfaction: True\n",
"Solving\n",
"Constraint satisfaction: True\n",
"Solving\n"
]
}
],
"source": [
"y_k = f_MT.apply(data)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Print Results"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"n MSE DIDI\n",
"0 0.0076 0.2282\n",
"1 0.0090 0.0408\n",
"2 0.0093 0.0275\n",
"3 0.0095 0.0270\n",
"4 0.0098 0.0381\n",
"5 0.0096 0.0405\n",
"6 0.0097 0.0425\n",
"7 0.0100 0.0398\n",
"8 0.0277 0.0000\n",
"9 0.0277 0.0000\n"
]
}
],
"source": [
"i = 0\n",
"\n",
"print(\"n MSE DIDI\")\n",
"for y_k in f_MT.y_k_test_history:\n",
" mse_k = mean_squared_error(y_k, y_test)\n",
" didi_k = didi_r(np.array(x_test), y_k, [len(x[0])-1])\n",
" print(\"%d %.4f %.4f\" % (i, mse_k, didi_k))\n",
" i += 1"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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.7.10"
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": false,
"sideBar": false,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": false,
"toc_window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 4
}
This diff is collapsed.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment