{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# hyperparameter optimization with Model class for NNs\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from ai4water.functional import Model\nfrom ai4water.datasets import busan_beach\nfrom ai4water.utils.utils import get_version_info\nfrom ai4water.hyperopt import Categorical, Real, Integer\n\nfor k,v in get_version_info().items():\n    print(f\"{k} version: {v}\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "data = busan_beach()\nprint(data.shape)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "input_features = data.columns.tolist()[0:-1]\nprint(input_features)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "output_features = data.columns.tolist()[-1:]\nprint(output_features)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "build the model\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "lookback = 14\n\nmodel = Model(\n    model = {\"layers\": {\n        \"Input\": {\"shape\": (lookback, len(input_features))},\n        \"LSTM\": {\"units\": Integer(10, 20, name=\"units\"),\n                 \"activation\": Categorical([\"relu\", \"elu\", \"tanh\"], name=\"activation\")},\n        \"Dense\": 1\n    }},\n    lr=Real(0.00001, 0.01, name=\"lr\"),\n    batch_size=Categorical([4, 8, 12, 16, 24], name=\"batch_size\"),\n    train_fraction=1.0,\n    split_random=True,\n    epochs=50,\n    ts_args={\"lookback\": lookback},\n    input_features=input_features,\n    output_features=output_features,\n    x_transformation=\"zscore\",\n    y_transformation={\"method\": \"log\", \"replace_zeros\": True, \"treat_negatives\": True},\n)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "optimizer = model.optimize_hyperparameters(\n    data=data,\n    num_iterations=25,\n    process_results=False,\n    refit=False,\n)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "_ = optimizer.plot_importance(save=False)"
      ]
    }
  ],
  "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.9"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}