From 886bc6066501872f4b7a637cf52ff9465d537aab Mon Sep 17 00:00:00 2001 From: Jan Date: Fri, 17 Mar 2023 12:21:46 -0600 Subject: [PATCH] Revert "cleanup - rm create_RNN seq2batches" This reverts commit 1d29a2c2e80d3f5e98be390bdccd504d2d3f64ea. --- fmda/fmda_rnn_rain.ipynb | 2 +- fmda/moisture_rnn.py | 38 +++++++++++++++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/fmda/fmda_rnn_rain.ipynb b/fmda/fmda_rnn_rain.ipynb index 6262561..5f8b1c2 100644 --- a/fmda/fmda_rnn_rain.ipynb +++ b/fmda/fmda_rnn_rain.ipynb @@ -41,8 +41,8 @@ "import data_funcs as datf\n", "from data_funcs import format_raws, retrieve_raws, format_precip, fixnan\n", "from data_funcs import raws_data, synthetic_data, plot_data, check_data, mse_data, to_json, from_json\n", - "from moisture_rnn import create_RNN_2, staircase, create_rnn_data, train_rnn, rnn_predict\n", "import moisture_models as mod\n", + "from moisture_rnn import create_RNN, create_RNN_2, staircase, seq2batches, create_rnn_data, train_rnn, rnn_predict\n", "\n", "meso_token=\"b40cb52cbdef43ef81329b84e8fd874f\"\n" ] diff --git a/fmda/moisture_rnn.py b/fmda/moisture_rnn.py index efefe88..f32bb7a 100644 --- a/fmda/moisture_rnn.py +++ b/fmda/moisture_rnn.py @@ -20,6 +20,17 @@ def vprint(*args): ## RNN Model Funcs +def create_RNN(hidden_units, dense_units, input_shape, activation): + inputs = tf.keras.Input(shape=input_shape) + # https://stackoverflow.com/questions/43448029/how-can-i-print-the-values-of-keras-tensors + # inputs2 = K.print_tensor(inputs, message='inputs = ') # change allso inputs to inputs2 below, must be used + x = tf.keras.layers.SimpleRNN(hidden_units, input_shape=input_shape, + activation=activation[0])(inputs) + outputs = tf.keras.layers.Dense(dense_units, activation=activation[1])(x) + model = tf.keras.Model(inputs=inputs, outputs=outputs) + model.compile(loss='mean_squared_error', optimizer='adam') + return model + def staircase(x,y,timesteps,trainsteps,return_sequences=False, verbose = False): # x [trainsteps+forecaststeps,features] all inputs # y [trainsteps,outputs] @@ -57,7 +68,32 @@ def staircase(x,y,timesteps,trainsteps,return_sequences=False, verbose = False): return x_train, y_train -def create_RNN_2(hidden_units, dense_units, activation, stateful=False, +def seq2batches(x,y,timesteps,trainsteps): + # x [trainsteps+forecaststeps,features] all inputs + # y [trainsteps,outputs] + # timesteps: split x and y into samples length timesteps, shifted by 1 + # trainsteps: number of timesteps to use for training, no more than y.shape[0] + print('shape x = ',x.shape) + print('shape y = ',y.shape) + print('timesteps=',timesteps) + print('trainsteps=',trainsteps) + outputs = y.shape[1] + features = x.shape[1] + samples= trainsteps - timesteps + 1 + print('samples=',samples) + x_train = np.empty([samples, timesteps, features]) + y_train = np.empty([samples, timesteps, outputs]) # only the last + print('samples=',samples,' timesteps=',timesteps, + ' features=',features,' outputs=',outputs) + for i in range(samples): + for k in range(timesteps): + for j in range(features): + x_train[i,k,j] = x[i+k,j] + for j in range(outputs): + y_train[i,k,j] = y[i+k,j] # return sequences + return x_train, y_train + +def create_RNN_2(hidden_units, dense_units, activation, stateful=False, batch_shape=None, input_shape=None, dense_layers=1, rnn_layers=1,return_sequences=False, initial_state=None, verbose = True): -- 2.11.4.GIT