4 "cell_type": "markdown",
5 "id": "6b50356b-169f-471d-92e0-8d82a2911185",
8 "# Batch Reset Hyperparameter Tutorial"
12 "cell_type": "markdown",
13 "id": "f6b09663-b3d5-46a5-a214-22b02467afb4",
16 "When training data are very long, a stateful model is prone to instability since at the early iterations of training, an unreasonable hidden state is generated and propogated through many batches of training.\n",
18 "We introduce the hyperparameter `batch_reset`, which resets the hidden state after a fixed number of batches. Future work will make this a schedule where the number of batches before reset is increased as the network learns and will be less subject to exploding/vanishing gradients."
22 "cell_type": "markdown",
23 "id": "9d9f20b3-6cad-43f2-8eab-7b065b02891b",
26 "## Environment and Data Setup"
31 "execution_count": null,
32 "id": "23115780-950f-46ea-b1d8-72bd5f3ec3bd",
38 "import os.path as osp\n",
39 "import matplotlib.pyplot as plt\n",
41 "import numpy as np\n",
42 "import pandas as pd\n",
44 "sys.path.append('..')\n",
45 "import reproducibility\n",
46 "from utils import print_dict_summary\n",
47 "from data_funcs import rmse\n",
48 "from moisture_rnn import RNNParams, RNNData, RNN\n",
49 "from moisture_rnn_pkl import pkl2train\n",
50 "from utils import read_yml, read_pkl\n",
57 "execution_count": null,
58 "id": "40e68cdd-bb04-499c-8370-3cbdb3aebc46",
62 "dat = read_pkl(\"batch_reset_tutorial_case.pkl\")"
67 "execution_count": null,
68 "id": "25e64ffb-3b4c-44c7-9737-caf05303ad0d",
72 "params = read_yml(\"../params.yaml\", subkey=\"rnn\")\n",
73 "params = RNNParams(params)\n",
74 "params.update({'epochs': 10})"
79 "execution_count": null,
80 "id": "67b36002-7d78-4723-80d5-8f3a6ac5886d",
84 "rnn_dat = RNNData(dat, scaler = params['scaler'], features_list = params['features_list'])\n",
85 "rnn_dat.train_test_split(\n",
86 " time_fracs = [.9, .05, .05]\n",
88 "rnn_dat.scale_data()\n",
89 "rnn_dat.batch_reshape(timesteps = params['timesteps'], batch_size = params['batch_size'])"
93 "cell_type": "markdown",
94 "id": "10eb476a-5791-459d-8ecf-901a11fee1f2",
97 "## Train without Stateful"
102 "execution_count": null,
103 "id": "3e32c020-12e8-42f3-8278-3e15431b042c",
107 "params.update({'verbose_fit': True, 'stateful': False, 'batch_reset':9999})\n",
108 "reproducibility.set_seed(123)\n",
109 "rnn = RNN(params)\n",
111 " m, errs = rnn.run_model(rnn_dat)\n",
112 "except Exception as e:\n",
113 " print(\"*\"*50)\n",
114 " print(f\"Caught Error {e}\")\n",
120 "execution_count": null,
121 "id": "05ab6d32-6626-4a18-ab15-c1dddf8496ed",
125 "rnn.predict(rnn_dat.scale_all_X())[0:150]"
129 "cell_type": "markdown",
130 "id": "c1172389-8c3a-4838-9c99-8bb575ba2014",
133 "## Train with Stateful, without Batch Reset\n",
135 "We turn off the parameter by setting it to a huge value."
140 "execution_count": null,
141 "id": "81232bf4-1995-490c-a609-0f8b88cef65d",
145 "params.update({'verbose_fit': True, 'stateful': True, 'batch_schedule_type':None})\n",
146 "params.update({'epochs': 30})\n",
147 "reproducibility.set_seed(123)\n",
153 "execution_count": null,
154 "id": "65e4f4af-9b02-4329-b25e-b89e3c893ba7",
159 " m, errs = rnn.run_model(rnn_dat)\n",
160 "except Exception as e:\n",
161 " print(\"*\"*50)\n",
162 " print(f\"Caught Error {e}\")\n",
167 "cell_type": "markdown",
168 "id": "92058207-1186-4389-8fbc-ad39672d5cdb",
171 "## Train with Stateful, with Periodic Batch Reset"
176 "execution_count": null,
177 "id": "06fb5624-6d47-452d-b41b-3c7ae2f987e3",
181 "params.update({'verbose_fit': True, 'stateful': True, 'batch_schedule_type':'constant', 'bmin': 20})\n",
182 "params.update({'epochs': 30})\n",
183 "reproducibility.set_seed(123)\n",
189 "execution_count": null,
190 "id": "33938583-4823-4e84-8afd-9652d85d164a",
195 " m, errs = rnn.run_model(rnn_dat, plot_period=\"predict\")\n",
196 "except Exception as e:\n",
197 " print(\"*\"*50)\n",
198 " print(f\"Caught Error {e}\")\n",
203 "cell_type": "markdown",
204 "id": "6f071895-ae8c-4c5a-8c5c-331fbfec1e6a",
207 "## Batch Reset Schedules"
212 "execution_count": null,
213 "id": "4958e7e0-1216-4392-a239-399f47917d98",
217 "from moisture_rnn import calc_exp_intervals, calc_log_intervals"
222 "execution_count": null,
223 "id": "e37db25b-a24e-4b37-abdc-ca6830afafc2",
231 "egrid = np.arange(epochs)"
236 "execution_count": null,
237 "id": "ea826c89-5af7-438e-8db1-f91702ae032d",
241 "plt.plot(egrid, np.linspace(bmin, bmax, epochs), label='Linear Increase')\n",
242 "plt.plot(egrid, calc_exp_intervals(bmin, bmax, epochs), label='Exponential Increase')\n",
243 "plt.plot(egrid, calc_log_intervals(bmin, bmax, epochs), label='Logarithmic Increase')\n",
244 "plt.xlabel('Epoch')\n",
245 "plt.ylabel('Batch Reset Value')\n",
247 "plt.title('Batch Reset Value vs Epoch')\n",
252 "cell_type": "markdown",
253 "id": "92e06001-4b27-47e8-aa5e-03bffdb9ba03",
256 "### Linear Schedule"
261 "execution_count": null,
262 "id": "cf4819b7-7e8e-4da9-8dee-217eda8f274f",
266 "params.update({'verbose_fit': False, 'stateful': True, \n",
267 " 'batch_schedule_type':'linear', 'bmin': 20, 'bmax': rnn_dat.hours})\n",
268 "params.update({'epochs': 40})\n",
269 "reproducibility.set_seed(123)\n",
270 "rnn = RNN(params)\n",
271 "m, errs = rnn.run_model(rnn_dat, plot_period = \"predict\")"
275 "cell_type": "markdown",
276 "id": "362dad1f-7584-4a04-a146-c13c2da2dc84",
279 "### Exponential Increase"
284 "execution_count": null,
285 "id": "d8e590af-4bdc-4e3f-bced-af2f4479e015",
289 "params.update({'verbose_fit': True, 'stateful': True, \n",
290 " 'batch_schedule_type':'exp', 'bmin': 20, 'bmax': rnn_dat.hours,\n",
291 " 'early_stopping_patience': 10})\n",
292 "params.update({'epochs': 40})\n",
293 "reproducibility.set_seed(123)\n",
294 "rnn = RNN(params)\n",
295 "m, errs = rnn.run_model(rnn_dat, plot_period = \"predict\")"
299 "cell_type": "markdown",
300 "id": "2424801b-3482-4542-9236-dbebae2c1143",
308 "execution_count": null,
309 "id": "00d32580-6c20-4916-b241-1af281fedbd9",
313 "params.update({'verbose_fit': False, 'stateful': True, \n",
314 " 'batch_schedule_type':'log', 'bmin': 20, 'bmax': rnn_dat.hours})\n",
315 "params.update({'epochs': 40})\n",
316 "reproducibility.set_seed(123)\n",
317 "rnn = RNN(params)\n",
318 "m, errs = rnn.run_model(rnn_dat, plot_period = \"predict\")"
323 "execution_count": null,
324 "id": "e6588375-c1bc-4b73-ad34-f8e6c2606979",
332 "display_name": "Python 3 (ipykernel)",
333 "language": "python",
341 "file_extension": ".py",
342 "mimetype": "text/x-python",
344 "nbconvert_exporter": "python",
345 "pygments_lexer": "ipython3",