4 "cell_type": "markdown",
5 "id": "a410d7d3-e783-476d-8396-e67f2ab09204",
8 "# v1 Jan's brief test of RNN training, should still work"
12 "cell_type": "markdown",
16 "## Testing staircase"
21 "execution_count": null,
26 "from moisture_rnn import staircase, staircase_2\n",
27 "import numpy as np\n",
28 "from data_funcs import plot_data"
33 "execution_count": null,
38 "# training data shape [batches,batch_size, sequence_length, features] \n",
39 "# also called [samples,timesteps,features]\n",
40 "# input data size [trainsteps,features] "
45 "execution_count": null,
54 "x=np.tile(range(datapoints), (features, 1)).T\n",
55 "y=np.tile(range(datapoints), (outputs, 1)).T\n",
57 "print('x shape =',x.shape)\n",
59 "print('y shape =',y.shape)"
64 "execution_count": null,
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"
79 "execution_count": null,
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)"
93 "cell_type": "markdown",
97 "## Testing RNN training on time lag problem"
102 "execution_count": null,
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",
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",
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",
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)"
132 "execution_count": null,
137 "plot_data({'E':x,'m':y},title=\"Generated equilibrium \")"
142 "execution_count": null,
147 "# Create training data with lookback, offset by one, all one batch\n",
148 "x_train, y_train = [], []\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",
155 "# Reshape input data for RNN\n",
156 "x_train = x_train.reshape(-1, timesteps, 1) # Print x_train.shape to verify\n",
158 "print('x_train.shape',x_train.shape)\n",
159 "print('y_train.shape',y_train.shape)"
164 "execution_count": null,
170 "x_train, y_train = staircase_2(x,y,timesteps,batch_size,return_sequences=False, verbose = True)"
175 "execution_count": null,
180 "from keras.callbacks import Callback\n",
182 "class ResetStatesCallback(Callback):\n",
183 " def on_epoch_end(self, epoch, logs=None):\n",
184 " self.model.reset_states()\n",
186 "from tensorflow.keras.optimizers import Adam\n",
188 "# Instantiate an optimizer with a custom learning rate\n",
189 "optimizer = Adam(learning_rate=0.001) \n",
191 "# Define the stateful RNN model\n",
196 "activation='tanh'\n",
197 "activation='linear'\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",
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",
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",
230 " raise ValueError('training must be 1 or 2 or 3')\n",
231 "# print('weights',model.get_weights())"
236 "execution_count": null,
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",
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')"
256 "execution_count": null,
263 " print(__name__)\n",
272 "execution_count": null,
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",
292 "execution_count": null,
297 "from utils import get_item\n",
299 "print(get_item(dict,'a',default=None))\n",
300 "print(get_item(dict,'b',default=None))\n"
305 "execution_count": null,
315 "execution_count": null,
322 "k = (k // batch_size) * batch_size\n",
328 "execution_count": null,
337 "display_name": "Python 3 (ipykernel)",
338 "language": "python",
346 "file_extension": ".py",
347 "mimetype": "text/x-python",
349 "nbconvert_exporter": "python",
350 "pygments_lexer": "ipython3",