diff --git a/automation/output_demo_notebook.ipynb b/automation/output_demo_notebook.ipynb index 800fbd6..97d9adb 100644 --- a/automation/output_demo_notebook.ipynb +++ b/automation/output_demo_notebook.ipynb @@ -3,40 +3,16 @@ { "cell_type": "code", "execution_count": 1, - "id": "0f294200", + "id": "d3c389e5", "metadata": { "execution": { - "iopub.execute_input": "2024-04-01T09:08:15.715634Z", - "iopub.status.busy": "2024-04-01T09:08:15.715197Z", - "iopub.status.idle": "2024-04-01T09:10:04.815215Z", - "shell.execute_reply": "2024-04-01T09:10:04.814527Z" + "iopub.execute_input": "2024-04-01T10:22:02.057930Z", + "iopub.status.busy": "2024-04-01T10:22:02.057496Z", + "iopub.status.idle": "2024-04-01T10:23:48.054641Z", + "shell.execute_reply": "2024-04-01T10:23:48.053980Z" } }, "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "2024-04-01 09:08:36.236889: I tensorflow/core/platform/cpu_feature_guard.cc:210] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.\n", - "To enable the following instructions: AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "2024-04-01 09:08:37.120837: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "WARNING:tensorflow:From /opt/hostedtoolcache/Python/3.11.8/x64/lib/python3.11/site-packages/tensorflow/python/compat/v2_compat.py:98: disable_resource_variables (from tensorflow.python.ops.resource_variables_toggle) is deprecated and will be removed in a future version.\n", - "Instructions for updating:\n", - "non-resource variables are not supported in the long term\n" - ] - }, { "name": "stderr", "output_type": "stream", @@ -92,6 +68,21 @@ "pip install 'aif360[LawSchoolGPA]'\n" ] }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "2024-04-01 10:22:43.682702: I tensorflow/core/platform/cpu_feature_guard.cc:210] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.\n", + "To enable the following instructions: AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "2024-04-01 10:22:44.657335: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT\n" + ] + }, { "name": "stderr", "output_type": "stream", @@ -150,12 +141,7 @@ "import time\n", "\n", "# Add a sleep statement to wait for imports to be fully loaded\n", - "time.sleep(20) \n", - "\n", - "# Create a TensorFlow session\n", - "import tensorflow.compat.v1 as tf\n", - "tf.disable_v2_behavior()\n", - "sess = tf.Session()\n", + "time.sleep(20) # You can adjust the sleep duration based on your needs\n", "\n", "from aif360.datasets import GermanDataset\n", "time.sleep(20)\n", @@ -167,82 +153,32 @@ "from IPython.display import Markdown, display\n", "time.sleep(20)\n", "\n", - "# Function to initialize the Reweighing algorithm\n", - "def initialize_reweighing(unprivileged_groups, privileged_groups):\n", - " return Reweighing(unprivileged_groups=unprivileged_groups,\n", - " privileged_groups=privileged_groups)\n", - "\n", - "# Function to initialize the AdversarialDebiasing algorithm\n", - "def initialize_adversarial_debiasing(unprivileged_groups, privileged_groups, adversary_loss_weight, num_epochs, batch_size, classifier_num_hidden_units, debias, sess):\n", - " scope_name = \"my_scope\"\n", - " return AdversarialDebiasing(unprivileged_groups=unprivileged_groups,\n", - " privileged_groups=privileged_groups,\n", - " scope_name=scope_name,\n", - " sess=sess,\n", - " seed=None,\n", - " adversary_loss_weight=adversary_loss_weight,\n", - " num_epochs=num_epochs,\n", - " batch_size=batch_size,\n", - " classifier_num_hidden_units=classifier_num_hidden_units,\n", - " debias=debias)\n", - "\n", - "# Function to initialize the OptimPreproc algorithm\n", - "def initialize_optim_preproc(unprivileged_groups, privileged_groups, optimizer, optim_options, verbose):\n", - " return OptimPreproc(optimizer=optimizer,\n", - " optim_options=optim_options,\n", - " unprivileged_groups=unprivileged_groups,\n", - " privileged_groups=privileged_groups,\n", - " verbose=verbose,\n", - " seed=None)\n", - "\n", - "# Load dataset\n", "dataset_orig = GermanDataset(\n", - " protected_attribute_names=['age'],\n", - " privileged_classes=[lambda x: x >= 25],\n", - " features_to_drop=['personal_status', 'sex']\n", + " protected_attribute_names=['age'], # this dataset also contains protected\n", + " # attribute for \"sex\" which we do not\n", + " # consider in this evaluation\n", + " privileged_classes=[lambda x: x >= 25], # age >=25 is considered privileged\n", + " features_to_drop=['personal_status', 'sex'] # ignore sex-related attributes\n", ")\n", "\n", - "# Split dataset\n", "dataset_orig_train, dataset_orig_test = dataset_orig.split([0.7], shuffle=True)\n", "\n", - "# Define privileged and unprivileged groups\n", "privileged_groups = [{'age': 1}]\n", "unprivileged_groups = [{'age': 0}]\n", "\n", - "# Define metric\n", "metric_orig_train = BinaryLabelDatasetMetric(dataset_orig_train, \n", - " unprivileged_groups=unprivileged_groups,\n", - " privileged_groups=privileged_groups)\n", + " unprivileged_groups=unprivileged_groups,\n", + " privileged_groups=privileged_groups)\n", "display(Markdown(\"#### Original training dataset\"))\n", "print(\"Difference in mean outcomes between unprivileged and privileged groups = %f\" % metric_orig_train.mean_difference())\n", "\n", - "# Initialize ALG as None initially\n", - "ALG = None\n", - "\n", - "# User-inputted algorithm\n", - "algorithm_choice = \"Reweighing\"\n", - "\n", - "# Initialize algorithm based on user input \n", - "if algorithm_choice == \"Reweighing\":\n", - " ALG = initialize_reweighing(unprivileged_groups, privileged_groups)\n", - "elif algorithm_choice == \"AdversarialDebiasing\":\n", - " ALG = initialize_adversarial_debiasing(unprivileged_groups, privileged_groups, \n", - " adversary_loss_weight=0.1, num_epochs=50, batch_size=128, \n", - " classifier_num_hidden_units=200, debias=True, sess=sess)\n", - "elif algorithm_choice == \"OptimPreproc\":\n", - " ALG = initialize_optim_preproc(unprivileged_groups, privileged_groups, \n", - " optimizer='my_optimizer', optim_options={'option1': 'value1', 'option2': 'value2'}, \n", - " verbose=False)\n", + "RW = Reweighing(unprivileged_groups=unprivileged_groups,\n", + " privileged_groups=privileged_groups)\n", + "dataset_transf_train = RW.fit_transform(dataset_orig_train)\n", "\n", - "# Fit and transform dataset\n", - "dataset_transf_train = ALG.fit_transform(dataset_orig_train)\n", - "\n", - "# Compute metric on transformed dataset\n", "metric_transf_train = BinaryLabelDatasetMetric(dataset_transf_train, \n", - " unprivileged_groups=unprivileged_groups,\n", - " privileged_groups=privileged_groups)\n", - "\n", - "# Display results\n", + " unprivileged_groups=unprivileged_groups,\n", + " privileged_groups=privileged_groups)\n", "display(Markdown(\"#### Transformed training dataset\"))\n", "print(\"Difference in mean outcomes between unprivileged and privileged groups = %f\" % metric_transf_train.mean_difference())\n" ]