{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "import warnings\n",
    "warnings.filterwarnings('ignore')\n",
    "import os \n",
    "os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' \n",
    "import numpy as np\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "\n",
    "import tensorflow as tf\n",
    "tf.compat.v1.disable_eager_execution() \n",
    "import tensorflow_probability as tfp\n",
    "import tensorflow.python.util.deprecation as deprecation\n",
    "deprecation._PRINT_DEPRECATION_WARNINGS = False\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "plt.style.use('seaborn')\n",
    "\n",
    "from data_loader import load_data\n",
    "from data_preprocesser import preprocess_data\n",
    "from realnvp import RealNVP \n",
    "from experiment import Experiment"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Data successfully loaded...\n",
      "\n",
      "Data successfully preprocessed...\n",
      "\n",
      "TensorFlow version: 2.5.0\n",
      "Number of dimensions: 37\n",
      "Learning rate: 0.0001\n",
      "Number of masked dimensions: 36\n",
      "\n",
      "Successfully created model...\n",
      "\n",
      "Optimizer and loss successfully defined...\n",
      "\n",
      "Iteration 0: 60.184722900390625\n",
      "Iteration 10000: 50.05358123779297\n",
      "Iteration 20000: 49.8468017578125\n",
      "Iteration 30000: 49.18869400024414\n",
      "Iteration 40000: 50.25196838378906\n",
      "Iteration 50000: 52.78778839111328\n",
      "Iteration 60000: 58.92238998413086\n",
      "Iteration 70000: 51.21335983276367\n",
      "Iteration 80000: 55.31623458862305\n",
      "Iteration 90000: 57.86042785644531\n",
      "Training finished...\n",
      "\n",
      "Displaying results...\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 40416 characters]\n",
      "text/plain": [
       "<Figure size 720x360 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Optimizer and loss successfully defined...\n",
      "\n",
      "Iteration 0: 60.184722900390625\n",
      "Iteration 10000: 50.05423355102539\n",
      "Iteration 20000: 49.84687042236328\n",
      "Iteration 30000: 49.18657684326172\n",
      "Iteration 40000: 50.251670837402344\n",
      "Iteration 50000: 52.787811279296875\n",
      "Iteration 60000: 58.922340393066406\n",
      "Iteration 70000: 51.213382720947266\n",
      "Iteration 80000: 55.316341400146484\n"
     ]
    }
   ],
   "source": [
    "def train(session, loss, optimizer, steps=int(1e5)):\n",
    "    \n",
    "    \"\"\" optimize for all dimensions \"\"\"\n",
    "    \n",
    "    recorded_steps = []\n",
    "    recorded_losses = []\n",
    "    for i in range(steps):\n",
    "        _, loss_per_iteration = session.run([optimizer, loss])\n",
    "        if i % 100 == 0:\n",
    "            recorded_steps.append(i)\n",
    "            recorded_losses.append(loss_per_iteration)\n",
    "        if i % int(1e4) == 0:\n",
    "            print('Iteration {iteration}: {loss}'.format(iteration=i,loss=loss_per_iteration))\n",
    "    return recorded_losses\n",
    "\n",
    "def plot_results(recorded_losses):\n",
    "    \n",
    "    \"\"\" plot loss \"\"\"\n",
    "    print('Displaying results...')\n",
    "    fig = plt.figure(figsize=(10,5))\n",
    "    x = np.arange(len(recorded_losses))\n",
    "    y = recorded_losses\n",
    "    m, b = np.polyfit(x, y, 1) \n",
    "    plt.scatter(x, y, s=10, alpha=0.3)\n",
    "    plt.plot(x, m*x+b, c=\"r\")\n",
    "    plt.title('Loss per 100 iteration')\n",
    "    plt.xlabel('Iteration')\n",
    "    plt.ylabel('Loss')\n",
    "    plt.tight_layout()\n",
    "    plt.show()\n",
    "\n",
    "def main():\n",
    "    \n",
    "    \"\"\" load data \"\"\"\n",
    "\n",
    "    filename = 'prostate.xls'\n",
    "    directory = '/Users/kaanguney.keklikci/Data/'\n",
    "\n",
    "    loader = load_data(filename, directory)\n",
    "    loader.create_directory(directory)\n",
    "    data = loader.read_data(directory, filename)\n",
    "    print('Data successfully loaded...\\n')\n",
    "    \n",
    "    \"\"\" preprocess data \"\"\"\n",
    "\n",
    "    fillna_vals = ['sz', 'sg', 'wt']\n",
    "    dropna_vals = ['ekg', 'age']\n",
    "    drop_vals = ['patno', 'sdate']\n",
    "\n",
    "    preprocesser = preprocess_data(StandardScaler(), fillna_vals, dropna_vals, drop_vals)\n",
    "    data = preprocesser.dropna_features(data)\n",
    "    data = preprocesser.impute(data)\n",
    "    data = preprocesser.drop_features(data)\n",
    "    data = preprocesser.encode_categorical(data)\n",
    "    data = preprocesser.scale(data)\n",
    "    print('Data successfully preprocessed...\\n')\n",
    "    \n",
    "    \"\"\" set MAF parameters \"\"\"\n",
    "\n",
    "    batch_size = 32\n",
    "    dtype = np.float32\n",
    "    tf_version = tf.__version__\n",
    "    params = 2\n",
    "    hidden_units = [512,512]\n",
    "    base_dist = tfp.distributions.Normal(loc=0., scale=1.)\n",
    "    dims = data.shape[1]\n",
    "    learning_rate = 1e-4\n",
    "    steps = 1e4\n",
    "    \n",
    "    \"\"\" initialize samples \"\"\"\n",
    "\n",
    "    realnvp = RealNVP(dtype, tf_version, batch_size, params, hidden_units, \n",
    "                      base_dist, dims, shift_only=True, \n",
    "                      is_constant_jacobian=True, masked_dimension_count=dims-1)\n",
    "\n",
    "    dims = realnvp.get_dims(data)\n",
    "    samples = realnvp.create_tensor(data)\n",
    "    print(f'TensorFlow version: {realnvp.tf_version}')\n",
    "    print(f'Number of dimensions: {realnvp.dims}')\n",
    "    print(f'Learning rate: {learning_rate}')\n",
    "    print(f'Number of masked dimensions: {realnvp.masked_dimension_count}\\n')\n",
    "    \n",
    "    \"\"\" initialize RealNVP \"\"\"\n",
    "\n",
    "    realnvp = realnvp.make_realnvp(data)\n",
    "    print('Successfully created model...\\n')\n",
    "    \n",
    "    \"\"\" initialize loss and optimizer \"\"\"\n",
    "\n",
    "    loss = -tf.reduce_mean(realnvp.log_prob(samples))\n",
    "    optimizer = tf.compat.v1.train.AdamOptimizer(learning_rate).minimize(loss)\n",
    "    \n",
    "    experiment = Experiment(optimizer, learning_rate, loss, steps)\n",
    "    \n",
    "    keywords = ['adam', 'rmsprop', 'sgd']\n",
    "    \n",
    "    for keyword in keywords:\n",
    "\n",
    "        session = tf.compat.v1.Session()\n",
    "        tf.compat.v1.set_random_seed(42)\n",
    "        experiment.change_optimizer(learning_rate, loss, keyword=keyword)\n",
    "        optimizer = experiment.get_optimizer()\n",
    "        session.run(tf.compat.v1.global_variables_initializer())\n",
    "        print('Optimizer and loss successfully defined...\\n')\n",
    "\n",
    "        \"\"\" start training \"\"\"\n",
    "        recorded_losses = train(session, loss, optimizer)\n",
    "        print('Training finished...\\n')\n",
    "\n",
    "        \"\"\" display results \"\"\"\n",
    "        plot_results(recorded_losses)\n",
    "    \n",
    "    \n",
    "if __name__ == \"__main__\":\n",
    "    main()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
