Merge pull request #17 from openwfm/restructure
[notebooks.git] / fmda / presentations / tensorboard_tutorial.ipynb
blobf2733843e946a5832dfdee8b5c6ef527717dd0b1
2  "cells": [
3   {
4    "cell_type": "markdown",
5    "id": "881b3085-13b7-420e-9fc5-1d51778a8933",
6    "metadata": {},
7    "source": [
8     "# Tensorboard Setup\n",
9     "\n",
10     "https://www.tensorflow.org/tensorboard/get_started\n",
11     "\n",
12     "Run all code cells below, then run on console:\n",
13     "\n",
14     "`tensorboard --logdir logs/train`"
15    ]
16   },
17   {
18    "cell_type": "code",
19    "execution_count": null,
20    "id": "730eaef2-51b5-4596-9454-2c0ef3706b86",
21    "metadata": {},
22    "outputs": [],
23    "source": [
24     "import sys\n",
25     "import os\n",
26     "sys.path.append(\"..\")"
27    ]
28   },
29   {
30    "cell_type": "code",
31    "execution_count": null,
32    "id": "4916811f-67b8-4abd-b38e-7f0645dd56f6",
33    "metadata": {},
34    "outputs": [],
35    "source": [
36     "# Setup\n",
37     "import reproducibility\n",
38     "\n",
39     "# both can change\n",
40     "# Environment\n",
41     "import numpy as np\n",
42     "import pandas as pd\n",
43     "import tensorflow as tf\n",
44     "from keras.models import Sequential\n",
45     "from keras.layers import Dense, SimpleRNN\n",
46     "# from keras.utils.vis_utils import plot_model\n",
47     "from keras.utils import plot_model\n",
48     "\n",
49     "from sklearn.preprocessing import MinMaxScaler\n",
50     "from sklearn.metrics import mean_squared_error\n",
51     "import math\n",
52     "import json\n",
53     "import matplotlib.pyplot as plt\n",
54     "import tensorflow as tf\n",
55     "import keras.backend as K\n",
56     "# from keras.utils.vis_utils import plot_model\n",
57     "from scipy.interpolate import LinearNDInterpolator, interpn\n",
58     "from scipy.optimize import root\n",
59     "import pickle, os\n",
60     "from utils import hash2\n",
61     "%matplotlib inline\n",
62     "\n",
63     "# Local modules for handling data and running moisture models\n",
64     "import data_funcs as datf\n",
65     "from data_funcs import format_raws, retrieve_raws, format_precip, fixnan, load_and_fix_data\n",
66     "from data_funcs import raws_data, synthetic_data, plot_data, check_data, rmse_data, to_json, from_json\n",
67     "import moisture_models as mod\n",
68     "from moisture_rnn import run_case, run_rnn, create_RNN_2, staircase, create_rnn_data, train_rnn, rnn_predict\n"
69    ]
70   },
71   {
72    "cell_type": "code",
73    "execution_count": null,
74    "id": "e49a4c0a-78d0-4a7e-a39e-012a882d78e8",
75    "metadata": {},
76    "outputs": [],
77    "source": [
78     "# Data\n",
79     "# Change directory for data read/write\n",
80     "\n",
81     "dict_file='../data/raws_CO_202306.pickle' # input path of FMDA dictionaries\n",
82     "reproducibility_file='../data/reproducibility_dict.pickle'\n",
83     "\n",
84     "# read test datasets\n",
85     "test_dict={}\n",
86     "test_dict.update(load_and_fix_data(dict_file))\n",
87     "print(test_dict.keys())\n",
88     "\n",
89     "repro_dict={}\n",
90     "repro_dict.update(load_and_fix_data(reproducibility_file))\n",
91     "print(repro_dict.keys())"
92    ]
93   },
94   {
95    "cell_type": "code",
96    "execution_count": null,
97    "id": "bc72419b-a2fa-4e54-addd-5778785d387b",
98    "metadata": {},
99    "outputs": [],
100    "source": [
101     "# Build Case Data\n",
102     "id = \"CPTC2_202306010000\"\n",
103     "case_data=test_dict[id]\n",
104     "case_data[\"hours\"]=len(case_data['fm'])\n",
105     "case_data[\"h2\"]=int(24*20)"
106    ]
107   },
108   {
109    "cell_type": "code",
110    "execution_count": null,
111    "id": "968595b9-905d-4fec-81f8-6a50aee6d0e2",
112    "metadata": {},
113    "outputs": [],
114    "source": [
115     "# Linear Activation Params\n",
116     "params={'id':1,\n",
117     "        'purpose':'test 1',\n",
118     "        'cases':'all',\n",
119     "        'scale':1,        # every feature in [0, scale]\n",
120     "        'rain_do':True,\n",
121     "        'verbose':False,\n",
122     "        'timesteps':5,\n",
123     "        'activation':['linear','linear'],\n",
124     "        'hidden_units':1,  \n",
125     "        'dense_units':1,    # do not change\n",
126     "        'dense_layers':1,   # do not change\n",
127     "        'centering':[0.0,0.0],  # should be activation at 0\n",
128     "        'DeltaE':[0,-1],    # bias correction\n",
129     "        'synthetic':False,  # run also synthetic cases\n",
130     "        'T1': 0.1,          # 1/fuel class (10)\n",
131     "        'fm_raise_vs_rain': 2.0,         # fm increase per mm rain \n",
132     "        'train_frac':0.5,  # time fraction to spend on training\n",
133     "        'epochs':1000,\n",
134     "        'verbose_fit':False,\n",
135     "        'verbose_weights':False,\n",
136     "}"
137    ]
138   },
139   {
140    "cell_type": "code",
141    "execution_count": null,
142    "id": "c38ee016-a418-456f-ab10-5cbe9ec0d7c3",
143    "metadata": {},
144    "outputs": [],
145    "source": [
146     "# Callback\n",
147     "log_dir = \"logs\"\n",
148     "tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir=log_dir, histogram_freq=1)"
149    ]
150   },
151   {
152    "cell_type": "code",
153    "execution_count": null,
154    "id": "9fb46a3c-0dbd-42da-87c7-8db947a53ae5",
155    "metadata": {},
156    "outputs": [],
157    "source": [
158     "reproducibility.set_seed() # Set seed for reproducibility\n",
159     "rnn_dat = create_rnn_data(case_data,params)\n",
160     "model1 = train_rnn(\n",
161     "    rnn_dat,\n",
162     "    params,\n",
163     "    rnn_dat['hours'],\n",
164     "    fit=True,\n",
165     "    callbacks = [tensorboard_callback]\n",
166     ")\n",
167     "case_data['m'] = rnn_predict(model1, params, rnn_dat)\n",
168     "fit_linear = case_data['m']"
169    ]
170   },
171   {
172    "cell_type": "code",
173    "execution_count": null,
174    "id": "a4335a23-6fab-4782-8b74-a282a5b9ab81",
175    "metadata": {},
176    "outputs": [],
177    "source": [
178     "plot_data(case_data,title2='Trained RNN Linear')"
179    ]
180   },
181   {
182    "cell_type": "markdown",
183    "id": "565e04ce-87d7-401d-81c8-d00941263d52",
184    "metadata": {},
185    "source": []
186   }
187  ],
188  "metadata": {
189   "kernelspec": {
190    "display_name": "Python 3 (ipykernel)",
191    "language": "python",
192    "name": "python3"
193   },
194   "language_info": {
195    "codemirror_mode": {
196     "name": "ipython",
197     "version": 3
198    },
199    "file_extension": ".py",
200    "mimetype": "text/x-python",
201    "name": "python",
202    "nbconvert_exporter": "python",
203    "pygments_lexer": "ipython3",
204    "version": "3.9.12"
205   }
206  },
207  "nbformat": 4,
208  "nbformat_minor": 5