Create Batch Reset Hyperparameter tutorial notebook
[notebooks.git] / fmda / moisture_rnn_test.ipynb
blob15ba6086ecd4fa1421fac2b7a2ce678aa65018a5
2  "cells": [
3   {
4    "cell_type": "markdown",
5    "id": "a410d7d3-e783-476d-8396-e67f2ab09204",
6    "metadata": {},
7    "source": [
8     "# v1 Jan's brief test of RNN training, should still work"
9    ]
10   },
11   {
12    "cell_type": "markdown",
13    "id": "0f51f7f4",
14    "metadata": {},
15    "source": [
16     "## Testing staircase"
17    ]
18   },
19   {
20    "cell_type": "code",
21    "execution_count": null,
22    "id": "04a797f1",
23    "metadata": {},
24    "outputs": [],
25    "source": [
26     "from moisture_rnn import staircase, staircase_2\n",
27     "import numpy as np\n",
28     "from data_funcs import plot_data"
29    ]
30   },
31   {
32    "cell_type": "code",
33    "execution_count": null,
34    "id": "70b801e3",
35    "metadata": {},
36    "outputs": [],
37    "source": [
38     "# training data shape   [batches,batch_size, sequence_length, features] \n",
39     "# also called           [samples,timesteps,features]\n",
40     "# input data size       [trainsteps,features] "
41    ]
42   },
43   {
44    "cell_type": "code",
45    "execution_count": null,
46    "id": "b9218f9e",
47    "metadata": {},
48    "outputs": [],
49    "source": [
50     "datapoints=10\n",
51     "timesteps=3\n",
52     "features=1\n",
53     "outputs=1\n",
54     "x=np.tile(range(datapoints), (features, 1)).T\n",
55     "y=np.tile(range(datapoints), (outputs, 1)).T\n",
56     "# print('x =',x)\n",
57     "print('x shape =',x.shape)\n",
58     "# print('y =',y)\n",
59     "print('y shape =',y.shape)"
60    ]
61   },
62   {
63    "cell_type": "code",
64    "execution_count": null,
65    "id": "c5874621",
66    "metadata": {},
67    "outputs": [],
68    "source": [
69     "# the original staircase, offset by one, all in one batch, no hidden state passed\n",
70     "x_train, y_train = staircase(x,y,timesteps,datapoints,return_sequences=False, verbose = True)\n",
71     "print('x_train shape =',x_train.shape)\n",
72     "print('y_train shape =',y_train.shape)\n",
73     "# print('x_train =',x_train)\n",
74     "# print('y_train =',y_train)\n"
75    ]
76   },
77   {
78    "cell_type": "code",
79    "execution_count": null,
80    "id": "fc82b98c",
81    "metadata": {},
82    "outputs": [],
83    "source": [
84     "# new staircase, hidden state passed between batches\n",
85     "x_train, y_train = staircase_2(x,y,timesteps,batch_size=3,return_sequences=False, verbose = True)\n",
86     "print('x_train shape =',x_train.shape)\n",
87     "print('y_train shape =',y_train.shape)\n",
88     "print('x_train =',x_train)\n",
89     "print('y_train =',y_train)"
90    ]
91   },
92   {
93    "cell_type": "markdown",
94    "id": "230abb62",
95    "metadata": {},
96    "source": [
97     "## Testing RNN training on time lag problem"
98    ]
99   },
100   {
101    "cell_type": "code",
102    "execution_count": null,
103    "id": "1e44181b",
104    "metadata": {},
105    "outputs": [],
106    "source": [
107     "import numpy as np\n",
108     "from keras.models import Sequential\n",
109     "from keras.layers import LSTM, Dense, SimpleRNN\n",
110     "from data_funcs import plot_data\n",
111     "\n",
112     "# Generate sample time series data (replace with your actual data)\n",
113     "hours = 500  #Ensure divisible by batch size and lookback\n",
114     "x = 10*(1+np.cos(np.linspace(0, hours, hours)*2*np.pi/24))  # daily\n",
115     "x = x+5*np.exp(np.sin(np.linspace(0, hours, hours)*2*np.pi/240))# 10 day cycle\n",
116     "x = x + 1.0*np.random.randn(*x.shape) # random\n",
117     "x = x/35\n",
118     "y = np.zeros((hours))\n",
119     "z = np.zeros((hours))\n",
120     "for i in range(1,hours):\n",
121     "    y[i] = y[i-1] + (x[i-1] - y[i-1])/10.\n",
122     "    z[i] = z[i-1] + (x[i-1] - z[i-1])/3.\n",
123     "y = (y + z)/2\n",
124     "x=np.reshape(x,[-1,1])\n",
125     "y=np.reshape(y,[-1,1])\n",
126     "print('x.shape',x.shape)\n",
127     "print('y.shape',y.shape)"
128    ]
129   },
130   {
131    "cell_type": "code",
132    "execution_count": null,
133    "id": "cc470846",
134    "metadata": {},
135    "outputs": [],
136    "source": [
137     "plot_data({'E':x,'m':y},title=\"Generated equilibrium  \")"
138    ]
139   },
140   {
141    "cell_type": "code",
142    "execution_count": null,
143    "id": "c12e812e",
144    "metadata": {},
145    "outputs": [],
146    "source": [
147     "# Create training data with lookback, offset by one, all one batch\n",
148     "x_train, y_train = [], []\n",
149     "timesteps = 10\n",
150     "for i in range(len(x) - timesteps):\n",
151     "    x_train.append(x[i:i+timesteps])  # Create sequences of 5 timesteps\n",
152     "    y_train.append(y[i+timesteps])\n",
153     "x_train, y_train = np.array(x_train), np.array(y_train)\n",
154     "\n",
155     "# Reshape input data for RNN\n",
156     "x_train = x_train.reshape(-1, timesteps, 1)  # Print x_train.shape to verify\n",
157     "print(x_train)\n",
158     "print('x_train.shape',x_train.shape)\n",
159     "print('y_train.shape',y_train.shape)"
160    ]
161   },
162   {
163    "cell_type": "code",
164    "execution_count": null,
165    "id": "ea2f5b32",
166    "metadata": {},
167    "outputs": [],
168    "source": [
169     "batch_size=32\n",
170     "x_train, y_train = staircase_2(x,y,timesteps,batch_size,return_sequences=False, verbose = True)"
171    ]
172   },
173   {
174    "cell_type": "code",
175    "execution_count": null,
176    "id": "35076792",
177    "metadata": {},
178    "outputs": [],
179    "source": [
180     "from keras.callbacks import Callback\n",
181     "\n",
182     "class ResetStatesCallback(Callback):\n",
183     "    def on_epoch_end(self, epoch, logs=None):\n",
184     "        self.model.reset_states()\n",
185     "        \n",
186     "from tensorflow.keras.optimizers import Adam\n",
187     "\n",
188     "# Instantiate an optimizer with a custom learning rate\n",
189     "optimizer = Adam(learning_rate=0.001)  \n",
190     "\n",
191     "# Define the stateful RNN model\n",
192     "RNN=SimpleRNN\n",
193     "#RNN=LSTM\n",
194     "#RNN=GRU\n",
195     "epochs=50\n",
196     "activation='tanh'\n",
197     "activation='linear'\n",
198     "cells=1\n",
199     "training = 2\n",
200     "\n",
201     "model = Sequential()\n",
202     "model.add(RNN(cells, stateful=True, batch_input_shape=(batch_size, timesteps, 1),activation=activation))  \n",
203     "model.add(Dense(1))  # Output layer for single-value prediction\n",
204     "model.compile(loss='mean_squared_error', optimizer=optimizer)\n",
205     "\n",
206     "# Train the model (manual state resetting)'print('x_train.shape',x_train.shape)\n",
207     "print('x_train.shape',x_train.shape)\n",
208     "print('y_train.shape',y_train.shape)\n",
209     "\n",
210     "if training == 1:\n",
211     "    for i in range(epochs):\n",
212     "        model.fit(x_train, y_train, epochs=1, batch_size=batch_size, verbose=1, shuffle=False)\n",
213     "        model.reset_states()\n",
214     "elif training == 2:\n",
215     "    model.fit(x_train, y_train, epochs=epochs, batch_size=batch_size, verbose=1, shuffle=False, \n",
216     "              callbacks=[ResetStatesCallback()])\n",
217     "elif training == 3:\n",
218     "    sequences = x_train.shape[0]\n",
219     "    batches = sequences // batch_size\n",
220     "    print('x_train has',sequences,'sequences',batches,'batches')\n",
221     "    for i in range(epochs):\n",
222     "        for j in range(batches):\n",
223     "            print('iteration',i,'batch',j,'size',batch_size)\n",
224     "            batch_start = j*batch_size\n",
225     "            batch_next = batch_start + batch_size \n",
226     "            model.fit(x_train[batch_start:batch_next], y_train[batch_start:batch_next], \n",
227     "                      epochs=1, batch_size=batch_size, verbose=1, shuffle=False)\n",
228     "        model.reset_states()  # at the end of each iteration = epoch\n",
229     "else:\n",
230     "    raise ValueError('training must be 1 or 2 or 3')\n",
231     "# print('weights',model.get_weights())"
232    ]
233   },
234   {
235    "cell_type": "code",
236    "execution_count": null,
237    "id": "0c8ad215",
238    "metadata": {},
239    "outputs": [],
240    "source": [
241     "# Define the stateless RNN model - to be used on data in a single sequence\n",
242     "model2 = Sequential()\n",
243     "model2.add(RNN(cells, stateful=False,input_shape=(None,1),activation=activation)) \n",
244     "model2.add(Dense(1))  # Output layer for single-value prediction\n",
245     "model2.compile(loss='mean_squared_error', optimizer='adam')\n",
246     "\n",
247     "# transfer weights, predict, plot\n",
248     "w=model.get_weights()\n",
249     "model2.set_weights(w)\n",
250     "z = model2.predict(x.reshape((-1,1))) # inout and output need to be shape (-1,1), ie columns\n",
251     "plot_data({'x':x,'y':y,'z':z},xlabel='',ylabel='',title='test')"
252    ]
253   },
254   {
255    "cell_type": "code",
256    "execution_count": null,
257    "id": "c22ab512",
258    "metadata": {},
259    "outputs": [],
260    "source": [
261     "# testing scope\n",
262     "def sub():\n",
263     "    print(__name__)\n",
264     "    print(a)\n",
265     "a = 1\n",
266     "sub()\n",
267     "sub()"
268    ]
269   },
270   {
271    "cell_type": "code",
272    "execution_count": null,
273    "id": "dd53a91d",
274    "metadata": {},
275    "outputs": [],
276    "source": [
277     "import inspect\n",
278     "\n",
279     "def my_function():\n",
280     "    print(\"The current function name is:\", inspect.currentframe().f_code.co_name)\n",
281     "    caller_name = inspect.stack()[1][3]\n",
282     "    print('The caller name is:',caller_name)\n",
283     "\n",
284     "def caller():\n",
285     "    my_function()\n",
286     "    \n",
287     "caller()\n"
288    ]
289   },
290   {
291    "cell_type": "code",
292    "execution_count": null,
293    "id": "a4aba878",
294    "metadata": {},
295    "outputs": [],
296    "source": [
297     "from utils import get_item\n",
298     "dict={'a':1}\n",
299     "print(get_item(dict,'a',default=None))\n",
300     "print(get_item(dict,'b',default=None))\n"
301    ]
302   },
303   {
304    "cell_type": "code",
305    "execution_count": null,
306    "id": "15d88141",
307    "metadata": {},
308    "outputs": [],
309    "source": [
310     "import utils"
311    ]
312   },
313   {
314    "cell_type": "code",
315    "execution_count": null,
316    "id": "cc4c0e3b",
317    "metadata": {},
318    "outputs": [],
319    "source": [
320     "k=90\n",
321     "batch_size=32\n",
322     "k = (k // batch_size) * batch_size\n",
323     "print(k)"
324    ]
325   },
326   {
327    "cell_type": "code",
328    "execution_count": null,
329    "id": "3c501f68",
330    "metadata": {},
331    "outputs": [],
332    "source": []
333   }
334  ],
335  "metadata": {
336   "kernelspec": {
337    "display_name": "Python 3 (ipykernel)",
338    "language": "python",
339    "name": "python3"
340   },
341   "language_info": {
342    "codemirror_mode": {
343     "name": "ipython",
344     "version": 3
345    },
346    "file_extension": ".py",
347    "mimetype": "text/x-python",
348    "name": "python",
349    "nbconvert_exporter": "python",
350    "pygments_lexer": "ipython3",
351    "version": "3.10.9"
352   }
353  },
354  "nbformat": 4,
355  "nbformat_minor": 5