1 # v2 training and prediction class infrastructure
6 import tensorflow
as tf
7 import matplotlib
.pyplot
as plt
9 from tensorflow
.keras
.callbacks
import Callback
, EarlyStopping
, TerminateOnNaN
10 # from sklearn.metrics import mean_squared_error
12 from tensorflow
.keras
.layers
import LSTM
, SimpleRNN
, Input
, Dropout
, Dense
14 import reproducibility
15 # from utils import print_dict_summary
16 from abc
import ABC
, abstractmethod
17 from utils
import hash2
, all_items_exist
, hash_ndarray
, hash_weights
18 from data_funcs
import rmse
, plot_data
, compare_dicts
21 from sklearn
.preprocessing
import MinMaxScaler
, StandardScaler
24 #*************************************************************************************
25 # Data Formatting Functions
27 def staircase(x
,y
,timesteps
,datapoints
,return_sequences
=False, verbose
= False):
28 # x [datapoints,features] all inputs
29 # y [datapoints,outputs]
30 # timesteps: split x and y into samples length timesteps, shifted by 1
31 # datapoints: number of timesteps to use for training, no more than y.shape[0]
33 print('staircase: shape x = ',x
.shape
)
34 print('staircase: shape y = ',y
.shape
)
35 print('staircase: timesteps=',timesteps
)
36 print('staircase: datapoints=',datapoints
)
37 print('staircase: return_sequences=',return_sequences
)
40 samples
= datapoints
-timesteps
+1
42 print('staircase: samples=',samples
,'timesteps=',timesteps
,'features=',features
)
43 x_train
= np
.empty([samples
, timesteps
, features
])
46 print('returning all timesteps in a sample')
47 y_train
= np
.empty([samples
, timesteps
, outputs
]) # all
48 for i
in range(samples
):
49 for k
in range(timesteps
):
50 x_train
[i
,k
,:] = x
[i
+k
,:]
51 y_train
[i
,k
,:] = y
[i
+k
,:]
54 print('returning only the last timestep in a sample')
55 y_train
= np
.empty([samples
, outputs
])
56 for i
in range(samples
):
57 for k
in range(timesteps
):
58 x_train
[i
,k
,:] = x
[i
+k
,:]
59 y_train
[i
,:] = y
[i
+timesteps
-1,:]
61 return x_train
, y_train
63 def staircase_2(x
,y
,timesteps
,batch_size
=None,trainsteps
=np
.inf
,return_sequences
=False, verbose
= False):
64 # create RNN training data in multiple batches
68 # timesteps: split x and y into sequences length timesteps
69 # a.k.a. lookback or sequence_length
71 # print params if verbose
73 if batch_size
is None:
74 raise ValueError('staircase_2 requires batch_size')
76 print('staircase_2: shape x = ',x
.shape
)
77 print('staircase_2: shape y = ',y
.shape
)
78 print('staircase_2: timesteps=',timesteps
)
79 print('staircase_2: batch_size=',batch_size
)
80 print('staircase_2: return_sequences=',return_sequences
)
84 datapoints
= min(nx
,ny
,trainsteps
)
86 print('staircase_2: datapoints=',datapoints
)
88 # sequence j in a given batch is assumed to be the continuation of sequence j in the previous batch
89 # https://www.tensorflow.org/guide/keras/working_with_rnns Cross-batch statefulness
91 # example with timesteps=3 batch_size=3 datapoints=15
92 # batch 0: [0 1 2] [1 2 3] [2 3 4]
93 # batch 1: [3 4 5] [4 5 6] [5 6 7]
94 # batch 2: [6 7 8] [7 8 9] [8 9 10]
95 # batch 3: [9 10 11] [10 11 12] [11 12 13]
96 # batch 4: [12 13 14] [13 14 15] when runs out this is the last batch, can be shorter
98 # TODO: implement for multiple locations, same starting time for each batch
100 # batch 0: [0 1 2] [0 1 2] [0 1 2]
101 # batch 1: [3 4 5] [3 4 5] [3 4 5]
102 # batch 2: [6 7 8] [6 7 8] [6 7 8]
103 # TODO: second epoch shift starting time at batch 0 in time
105 # TODO: implement for multiple locations, different starting times for each batch
107 # batch 0: [0 1 2] [1 2 3] [2 3 4]
108 # batch 1: [3 4 5] [4 5 6] [5 6 57
109 # batch 2: [6 7 8] [7 8 9] [8 9 10]
112 # the first sample in batch j starts from timesteps*j and ends with timesteps*(j+1)-1
113 # e.g. the final hidden state of the rnn after the sequence of steps [0 1 2] in batch 0
114 # becomes the starting hidden state of the rnn in the sequence of steps [3 4 5] in batch 1, etc.
116 # sample [0 1 2] means the rnn is used twice to map state 0 -> 1 -> 2
117 # the state at time 0 is fixed but the state is considered a variable at times 1 and 2
118 # the loss is computed from the output at time 2 and the gradient of the loss function by chain rule which ends at time 0 because the state there is a constant -> derivative is zero
119 # sample [3 4 5] means the rnn is used twice to map state 3 -> 4 -> 5 # the state at time 3 is fixed to the output of the first sequence [0 1 2]
120 # the loss is computed from the output at time 5 and the gradient of the loss function by chain rule which ends at time 3 because the state there is considered constant -> derivative is zero
121 # how is the gradient computed? I suppose keras adds gradient wrt the weights at 2 5 8 ... 3 6 9... 4 7 ... and uses that to update the weights
122 # there is only one set of weights h(2) = f(h(1),w) h(1) = f(h(0),w) but w is always the same
123 # each column is a one successive evaluation of h(n+1) = f(h(n),w) for n = n_startn n_start+1,...
124 # the cannot be evaluated efficiently on gpu because gpu is a parallel processor
125 # this of it as each column served by one thread, and the threads are independent because they execute in parallel, there needs to be large number of threads (32 is a good number)\
126 # each batch consists of independent calculations
127 # but it can depend on the result of the previous batch (that's the recurrent parr)
131 max_batches
= datapoints
// timesteps
132 max_sequences
= max_batches
* batch_size
135 print('staircase_2: max_batches=',max_batches
)
136 print('staircase_2: max_sequences=',max_sequences
)
138 x_train
= np
.zeros((max_sequences
, timesteps
, features
))
140 y_train
= np
.empty((max_sequences
, timesteps
, outputs
))
142 y_train
= np
.empty((max_sequences
, outputs
))
144 # build the sequences
146 for i
in range(max_batches
):
147 for j
in range(batch_size
):
148 begin
= i
*timesteps
+ j
149 next
= begin
+ timesteps
150 if next
> datapoints
:
153 print('sequence',k
,'batch',i
,'sample',j
,'data',begin
,'to',next
-1)
154 x_train
[k
,:,:] = x
[begin
:next
,:]
156 y_train
[k
,:,:] = y
[begin
:next
,:]
158 y_train
[k
,:] = y
[next
-1,:]
161 print('staircase_2: shape x_train = ',x_train
.shape
)
162 print('staircase_2: shape y_train = ',y_train
.shape
)
163 print('staircase_2: sequences generated',k
)
164 print('staircase_2: batch_size=',batch_size
)
165 k
= (k
// batch_size
) * batch_size
167 print('staircase_2: removing partial and empty batches at the end, keeping',k
)
168 x_train
= x_train
[:k
,:,:]
170 y_train
= y_train
[:k
,:,:]
172 y_train
= y_train
[:k
,:]
175 print('staircase_2: shape x_train = ',x_train
.shape
)
176 print('staircase_2: shape y_train = ',y_train
.shape
)
178 return x_train
, y_train
181 # Dictionary of scalers, used to avoid multiple object creation and to avoid multiple if statements
183 'minmax': MinMaxScaler(),
184 'standard': StandardScaler()
188 def batch_setup(ids
, batch_size
):
190 Sets up stateful batched training data scheme for RNN training.
192 This function takes a list or array of identifiers (`ids`) and divides them into batches of a specified size (`batch_size`). If the last batch does not have enough elements to meet the `batch_size`, the function will loop back to the start of the identifiers and continue filling the batch until it reaches the required size.
196 ids : list or numpy array
197 A list or numpy array containing the ids to be batched.
200 The desired size of each batch.
204 batches : list of lists
205 A list where each element is a batch (itself a list) of identifiers. Each batch will contain exactly `batch_size` elements.
209 >>> ids = [1, 2, 3, 4, 5]
211 >>> batch_setup(ids, batch_size)
212 [[1, 2, 3], [4, 5, 1]]
216 - If `ids` is shorter than `batch_size`, the returned list will contain a single batch where identifiers are repeated from the start of `ids` until the batch is filled.
218 # Ensure ids is a numpy array
221 # Initialize the list to hold the batches
224 # Use a loop to slice the list/array into batches
225 for i
in range(0, len(x
), batch_size
):
226 batch
= list(x
[i
:i
+ batch_size
])
228 # If the batch is not full, continue from the start
229 while len(batch
) < batch_size
:
230 # Calculate the remaining number of items needed
231 remaining
= batch_size
- len(batch
)
232 # Append the needed number of items from the start of the array
233 batch
.extend(x
[:remaining
])
235 batches
.append(batch
)
239 def staircase_spatial(X
, y
, batch_size
, timesteps
, hours
=None, start_times
= None, verbose
= True):
241 Prepares spatially formatted time series data for RNN training by creating batches of sequences across different locations, stacked to be compatible with stateful models.
243 This function processes multi-location time series data by slicing it into batches and formatting it to fit into a recurrent neural network (RNN) model. It utilizes a staircase-like approach to prepare sequences for each location and then interlaces them to align with stateful RNN structures.
247 X : list of numpy arrays
248 A list where each element is a numpy array containing features for a specific location. The shape of each array is `(total_time_steps, features)`.
250 y : list of numpy arrays
251 A list where each element is a numpy array containing the target values for a specific location. The shape of each array is `(total_time_steps,)`.
254 The number of sequences to include in each batch.
257 The number of time steps to include in each sequence for the RNN.
259 hours : int, optional
260 The length of each time series to consider for each location. If `None`, it defaults to the minimum length of `y` across all locations.
262 start_times : numpy array, optional
263 The initial time step for each location. If `None`, it defaults to an array starting from 0 and incrementing by 1 for each location.
265 verbose : bool, optional
266 If `True`, prints additional information during processing. Default is `True`.
271 A 3D numpy array with shape `(total_sequences, timesteps, features)` containing the prepared feature sequences for all locations.
274 A 2D numpy array with shape `(total_sequences, 1)` containing the corresponding target values for all locations.
277 Number of sequences per location. Used to reset states when location changes. Hidden state of RNN will be reset after n_seqs number of batches
281 - The function handles spatially distributed time series data by batching and formatting it for stateful RNNs.
282 - `hours` determines how much of the time series is used for each location. If not provided, it defaults to the shortest series in `y`.
283 - If `start_times` is not provided, it assumes each location starts its series at progressively later time steps.
284 - The `batch_setup` function is used internally to manage the creation of location and time step batches.
285 - The returned feature sequences `XX` and target sequences `yy` are interlaced to align with the expected input format of stateful RNNs.
288 # Generate ids based on number of distinct timeseries provided
289 n_loc
= len(y
) # assuming each list entry for y is a separate location
290 loc_ids
= np
.arange(n_loc
)
292 # Generate hours and start_times if None
294 print("Setting total hours to minimum length of y in provided dictionary")
295 hours
= min(len(yi
) for yi
in y
)
296 if start_times
is None:
297 print("Setting Start times to offset by 1 hour by location")
298 start_times
= np
.arange(n_loc
)
300 loc_batch
, t_batch
= batch_setup(loc_ids
, batch_size
), batch_setup(start_times
, batch_size
)
302 print(f
"Location ID Batches: {loc_batch}")
303 print(f
"Start Times for Batches: {t_batch}")
305 # Loop over batches and construct with staircase_2
308 for i
in range(0, len(loc_batch
)):
309 locs_i
= loc_batch
[i
]
311 for j
in range(0, len(locs_i
)):
314 # Create RNNData Dict
315 # Subset data to given location and time from t0 to t0+hours
316 X_temp
= X
[j
][t0
:tend
,:]
317 y_temp
= y
[j
][t0
:tend
].reshape(-1,1)
320 Xi
, yi
= staircase_2(
323 timesteps
= timesteps
,
324 batch_size
= 1, # note: using 1 here to format sequences for a single location, not same as target batch size for training data
330 # Drop incomplete batches
331 lens
= [yi
.shape
[0] for yi
in ys
]
334 print(f
"Minimum number of sequences by location: {n_seqs}")
335 print(f
"Applying minimum length to other arrays.")
336 Xs
= [Xi
[:n_seqs
] for Xi
in Xs
]
337 ys
= [yi
[:n_seqs
] for yi
in ys
]
339 # Interlace arrays to match stateful structure
340 n_features
= Xi
.shape
[2]
343 for i
in range(0, len(loc_batch
)):
344 locs_i
= loc_batch
[i
]
345 XXi
= np
.empty((Xs
[0].shape
[0]*batch_size
, 5, n_features
))
346 yyi
= np
.empty((Xs
[0].shape
[0]*batch_size
, 1))
347 for j
in range(0, len(locs_i
)):
348 XXi
[j
::(batch_size
)] = Xs
[locs_i
[j
]]
349 yyi
[j
::(batch_size
)] = ys
[locs_i
[j
]]
352 yy
= np
.concatenate(yys
, axis
=0)
353 XX
= np
.concatenate(XXs
, axis
=0)
356 print(f
"Spatially Formatted X Shape: {XX.shape}")
357 print(f
"Spatially Formatted X Shape: {yy.shape}")
360 return XX
, yy
, n_seqs
362 #***********************************************************************************************
363 ### RNN Class Functionality
365 class RNNParams(dict):
367 A custom dictionary class for handling RNN parameters. Automatically calculates certain params based on others. Overwrites the update method to protect from incompatible parameter choices. Inherits from dict
369 def __init__(self
, input_dict
):
371 Initializes the RNNParams instance and runs checks and shape calculations.
376 A dictionary containing RNN parameters.
378 super().__init
__(input_dict
)
379 # Automatically run checks on initialization
381 # Automatically calculate shapes on initialization
382 self
.calc_param_shapes()
383 def run_checks(self
, verbose
=True):
385 Validates that required keys exist and are of the correct type.
389 verbose : bool, optional
390 If True, prints status messages. Default is True.
392 print("Checking params...")
393 # Keys must exist and be integers
395 'batch_size', 'timesteps', 'rnn_layers',
396 'rnn_units', 'dense_layers', 'dense_units', 'epochs'
400 assert key
in self
, f
"Missing required key: {key}"
401 assert isinstance(self
[key
], int), f
"Key '{key}' must be an integer"
403 # Keys must exist and be lists
404 list_keys
= ['activation', 'features_list', 'dropout']
405 for key
in list_keys
:
406 assert key
in self
, f
"Missing required key: {key}"
407 assert isinstance(self
[key
], list), f
"Key '{key}' must be a list"
409 # Keys must exist and be floats
410 float_keys
= ['learning_rate', 'train_frac', 'val_frac']
411 for key
in float_keys
:
412 assert key
in self
, f
"Missing required key: {key}"
413 assert isinstance(self
[key
], float), f
"Key '{key}' must be a float"
415 print("Input dictionary passed all checks.")
416 def calc_param_shapes(self
, verbose
=True):
418 Calculates and updates the shapes of certain parameters based on input data.
422 verbose : bool, optional
423 If True, prints status messages. Default is True.
426 print("Calculating shape params based on features list, timesteps, and batch size")
427 print(f
"Input Feature List: {self['features_list']}")
428 print(f
"Input Timesteps: {self['timesteps']}")
429 print(f
"Input Batch Size: {self['batch_size']}")
431 n_features
= len(self
['features_list'])
432 batch_shape
= (self
["batch_size"], self
["timesteps"], n_features
)
434 print("Calculated params:")
435 print(f
"Number of features: {n_features}")
436 print(f
"Batch Shape: {batch_shape}")
438 # Update the dictionary
440 'n_features': n_features
,
441 'batch_shape': batch_shape
446 def update(self
, *args
, verbose
=True, **kwargs
):
448 Overwrites the standard update functon from dict. This is to prevent certain keys from being modified directly and to automatically update keys to be compatible with each other. The keys handled relate to the shape of the input data to the RNN.
452 verbose : bool, optional
453 If True, prints status messages. Default is True.
455 # Prevent updating n_features and batch_shape
456 restricted_keys
= {'n_features', 'batch_shape'}
457 keys_to_check
= {'features_list', 'timesteps', 'batch_size'}
459 # Check for restricted keys in args
461 if isinstance(args
[0], dict):
462 if restricted_keys
& args
[0].keys():
463 raise KeyError(f
"Cannot directly update keys: {restricted_keys & args[0].keys()}, \n Instead update one of: {keys_to_check}")
464 elif isinstance(args
[0], (tuple, list)) and all(isinstance(i
, tuple) and len(i
) == 2 for i
in args
[0]):
465 if restricted_keys
& {k
for k
, v
in args
[0]}:
466 raise KeyError(f
"Cannot directly update keys: {restricted_keys & {k for k, v in args[0]}}, \n Instead update one of: {keys_to_check}")
468 # Check for restricted keys in kwargs
469 if restricted_keys
& kwargs
.keys():
470 raise KeyError(f
"Cannot update restricted keys: {restricted_keys & kwargs.keys()}")
473 # Track if specific keys are updated
476 # Update using the standard dict update method
478 if isinstance(args
[0], dict):
479 keys_updated
.update(args
[0].keys() & keys_to_check
)
480 elif isinstance(args
[0], (tuple, list)) and all(isinstance(i
, tuple) and len(i
) == 2 for i
in args
[0]):
481 keys_updated
.update(k
for k
, v
in args
[0] if k
in keys_to_check
)
484 keys_updated
.update(kwargs
.keys() & keys_to_check
)
486 # Call the parent update method
487 super().update(*args
, **kwargs
)
489 # Recalculate shapes if necessary
491 self
.calc_param_shapes(verbose
=verbose
)
494 ## Class for handling input data
497 A custom dictionary class for managing RNN data, with validation, scaling, and train-test splitting functionality.
499 required_keys
= {"loc", "time", "X", "y", "features_list"}
500 def __init__(self
, input_dict
, scaler
=None, features_list
=None):
502 Initializes the RNNData instance, performs checks, and prepares data.
507 A dictionary containing the initial data.
508 scaler : str, optional
509 The name of the scaler to be used (e.g., 'minmax', 'standard'). Default is None.
510 features_list : list, optional
511 A subset of features to be used. Default is None which means all features.
514 # Copy to avoid changing external input
515 input_data
= input_dict
.copy()
516 # Initialize inherited dict class
517 super().__init
__(input_data
)
519 # Check if input data is one timeseries dataset or multiple
520 if type(self
.loc
['STID']) == str:
522 print("Input data is single timeseries.")
523 elif type(self
.loc
['STID']) == list:
525 print("Input data from multiple timeseries.")
527 raise KeyError(f
"Input locations not list or single string")
529 # Set up Data Scaling
531 if scaler
is not None:
532 self
.set_scaler(scaler
)
534 # Rename and define other stuff.
536 self
['hours'] = min(arr
.shape
[0] for arr
in self
.y
)
538 self
['hours'] = len(self
['y'])
540 self
['all_features_list'] = self
.pop('features_list')
541 if features_list
is None:
542 print("Using all input features.")
543 self
.features_list
= self
.all_features_list
545 self
.features_list
= features_list
547 self
.__dict
__.update(self
)
549 # TODO: Fix checks for multilocation
550 def run_checks(self
, verbose
=True):
552 Validates that required keys are present and checks the integrity of data shapes.
556 verbose : bool, optional
557 If True, prints status messages. Default is True.
559 missing_keys
= self
.required_keys
- self
.keys()
561 raise KeyError(f
"Missing required keys: {missing_keys}")
563 # y_shape = np.shape(self.y)
564 # if not (len(y_shape) == 1 or (len(y_shape) == 2 and y_shape[1] == 1)):
565 # raise ValueError(f"'y' must be one-dimensional, with shape (N,) or (N, 1). Current shape is {y_shape}.")
567 # # Check if 'hours' is provided and matches len(y)
568 # if 'hours' in self:
569 # if self.hours != len(self.y):
570 # raise ValueError(f"Provided 'hours' value {self.hours} does not match the length of 'y', which is {len(self.y)}.")
571 # Check desired subset of features is in all input features
572 if not all_items_exist(self
.features_list
, self
.all_features_list
):
573 raise ValueError(f
"Provided 'features_list' {self.features_list} has elements not in input features.")
574 def set_scaler(self
, scaler
):
576 Sets the scaler to be used for data normalization.
581 The name of the scaler (e.g., 'minmax', 'standard').
583 recognized_scalers
= ['minmax', 'standard']
584 if scaler
in recognized_scalers
:
585 print(f
"Setting data scaler: {scaler}")
586 self
.scaler
= scalers
[scaler
]
588 raise ValueError(f
"Unrecognized scaler '{scaler}'. Recognized scalers are: {recognized_scalers}.")
589 def train_test_split(self
, train_frac
, val_frac
=0.0, subset_features
=True, features_list
=None, split_time
=True, split_space
=False, verbose
=True):
591 Splits the data into training, validation, and test sets.
596 The fraction of data to be used for training.
597 val_frac : float, optional
598 The fraction of data to be used for validation. Default is 0.0.
599 subset_features : bool, optional
600 If True, subsets the data to the specified features list. Default is True.
601 features_list : list, optional
602 A list of features to use for subsetting. Default is None.
603 split_time : bool, optional
604 Whether to split the data based on time. Default is True.
605 split_space : bool, optional
606 Whether to split the data based on space. Default is False.
607 verbose : bool, optional
608 If True, prints status messages. Default is True.
610 # Indicate whether multi timeseries or not
611 spatial
= self
.spatial
613 # Extract data to desired features
617 if verbose
and self
.features_list
!= self
.all_features_list
:
618 print(f
"Subsetting input data to features_list: {self.features_list}")
619 # Indices to subset all features with based on params features
621 for item
in self
.features_list
:
622 if item
in self
.all_features_list
:
623 indices
.append(self
.all_features_list
.index(item
))
625 print(f
"Warning: feature name '{item}' not found in list of all features from input data")
627 X
= [Xi
[:, indices
] for Xi
in X
]
631 # Setup train/test in time
632 train_ind
= int(np
.floor(self
.hours
* train_frac
)); self
.train_ind
= train_ind
633 test_ind
= int(train_ind
+ round(self
.hours
* val_frac
)); self
.test_ind
= test_ind
635 # Check for any potential issues with indices
636 if test_ind
> self
.hours
:
637 print(f
"Setting test index to {self.hours}")
638 test_ind
= self
.hours
639 if train_ind
>= test_ind
:
640 raise ValueError("Train index must be less than test index.")
642 # Training data from 0 to train_ind
643 # Validation data from train_ind to test_ind
644 # Test data from test_ind to end
646 self
.X_train
= [Xi
[:train_ind
] for Xi
in X
]
647 self
.y_train
= [yi
[:train_ind
].reshape(-1,1) for yi
in y
]
649 self
.X_val
= [Xi
[train_ind
:test_ind
] for Xi
in X
]
650 self
.y_val
= [yi
[train_ind
:test_ind
].reshape(-1,1) for yi
in y
]
651 self
.X_test
= [Xi
[test_ind
:] for Xi
in X
]
652 self
.y_test
= [yi
[test_ind
:].reshape(-1,1) for yi
in y
]
654 self
.X_train
= X
[:train_ind
]
655 self
.y_train
= y
[:train_ind
].reshape(-1,1) # assumes y 1-d, change this if vector output
657 self
.X_val
= X
[train_ind
:test_ind
]
658 self
.y_val
= y
[train_ind
:test_ind
].reshape(-1,1) # assumes y 1-d, change this if vector output
659 self
.X_test
= X
[test_ind
:]
660 self
.y_test
= y
[test_ind
:].reshape(-1,1) # assumes y 1-d, change this if vector output
664 # Print statements if verbose
666 print(f
"Train index: 0 to {train_ind}")
667 print(f
"Validation index: {train_ind} to {test_ind}")
668 print(f
"Test index: {test_ind} to {self.hours}")
671 print(f
"X_train[0] shape: {self.X_train[0].shape}, y_train[0] shape: {self.y_train[0].shape}")
672 print(f
"X_val[0] shape: {self.X_val[0].shape}, y_val[0] shape: {self.y_val[0].shape}")
673 print(f
"X_test[0] shape: {self.X_test[0].shape}, y_test[0] shape: {self.y_test[0].shape}")
675 print(f
"X_train shape: {self.X_train.shape}, y_train shape: {self.y_train.shape}")
676 print(f
"X_val shape: {self.X_val.shape}, y_val shape: {self.y_val.shape}")
677 print(f
"X_test shape: {self.X_test.shape}, y_test shape: {self.y_test.shape}")
679 def scale_data(self
, verbose
=True):
681 Scales the training data using the set scaler.
685 verbose : bool, optional
686 If True, prints status messages. Default is True.
688 # Indicate whether multi timeseries or not
689 spatial
= self
.spatial
690 if self
.scaler
is None:
691 raise ValueError("Scaler is not set. Use 'set_scaler' method to set a scaler before scaling data.")
692 if not hasattr(self
, "X_train"):
693 raise AttributeError("No X_train within object. Run train_test_split first. This is to avoid fitting the scaler with prediction data.")
695 print(f
"Scaling training data with scaler {self.scaler}, fitting on X_train")
698 # Fit scaler on row-joined training data
699 self
.scaler
.fit(np
.vstack(self
.X_train
))
700 # Transform data using fitted scaler
701 self
.X_train
= [self
.scaler
.transform(Xi
) for Xi
in self
.X_train
]
702 if hasattr(self
, 'X_val'):
703 self
.X_val
= [self
.scaler
.transform(Xi
) for Xi
in self
.X_val
]
704 self
.X_test
= [self
.scaler
.transform(Xi
) for Xi
in self
.X_test
]
706 # Fit the scaler on the training data
707 self
.scaler
.fit(self
.X_train
)
708 # Transform the data using the fitted scaler
709 self
.X_train
= self
.scaler
.transform(self
.X_train
)
710 if hasattr(self
, 'X_val'):
711 self
.X_val
= self
.scaler
.transform(self
.X_val
)
712 self
.X_test
= self
.scaler
.transform(self
.X_test
)
714 # NOTE: only works for non spatial
715 def scale_all_X(self
, verbose
=True):
717 Scales the all data using the set scaler.
721 verbose : bool, optional
722 If True, prints status messages. Default is True.
726 Scaled X matrix, subsetted to features_list.
729 raise ValueError("Not implemented for spatial data")
731 if self
.scaler
is None:
732 raise ValueError("Scaler is not set. Use 'set_scaler' method to set a scaler before scaling data.")
734 print(f
"Scaling all X data with scaler {self.scaler}, fitted on X_train")
737 for item
in self
.features_list
:
738 if item
in self
.all_features_list
:
739 indices
.append(self
.all_features_list
.index(item
))
741 print(f
"Warning: feature name '{item}' not found in list of all features from input data")
742 X
= self
.X
[:, indices
]
743 X
= self
.scaler
.transform(X
)
747 def inverse_scale(self
, return_X
= 'all_hours', save_changes
=False, verbose
=True):
749 Inversely scales the data to its original form.
753 return_X : str, optional
754 Specifies what data to return after inverse scaling. Default is 'all_hours'.
755 save_changes : bool, optional
756 If True, updates the internal data with the inversely scaled values. Default is False.
757 verbose : bool, optional
758 If True, prints status messages. Default is True.
761 print("Inverse scaling data...")
762 X_train
= self
.scaler
.inverse_transform(self
.X_train
)
763 X_val
= self
.scaler
.inverse_transform(self
.X_val
)
764 X_test
= self
.scaler
.inverse_transform(self
.X_test
)
767 print("Inverse transformed data saved")
768 self
.X_train
= X_train
773 print("Inverse scaled, but internal data not changed.")
775 print(f
"Attempting to return {return_X}")
776 if return_X
== "all_hours":
777 return np
.concatenate((X_train
, X_val
, X_test
), axis
=0)
779 print(f
"Unrecognized or unimplemented return value {return_X}")
780 def batch_reshape(self
, timesteps
, batch_size
, hours
=None, verbose
=False):
782 Restructures input data to RNN using batches and sequences.
787 The size of each training batch to reshape the data.
789 The number of timesteps or sequence length. Consistitutes a single sample
791 Number of timesteps or sequence length used for a single sequence in RNN training. Constitutes a single sample to the model
794 Number of sequences used within a batch of training
799 This method reshapes the data in place.
803 If either 'X_train' or 'y_train' attributes do not exist within the instance.
807 The reshaping method depends on self param "spatial".
808 - spatial == False: Reshapes data assuming no spatial dimensions.
809 - spatial == True: Reshapes data considering spatial dimensions.
813 if not hasattr(self
, 'X_train') or not hasattr(self
, 'y_train'):
814 raise AttributeError("Both 'X_train' and 'y_train' must be set before reshaping batches.")
816 # Indicator of spatial training scheme or not
817 spatial
= self
.spatial
820 print(f
"Reshaping spatial training data using batch size: {batch_size} and timesteps: {timesteps}")
821 self
.X_train
, self
.y_train
, self
.n_seqs
= staircase_spatial(self
.X_train
, self
.y_train
, timesteps
= timesteps
, batch_size
=batch_size
, hours
=hours
, verbose
=verbose
)
822 if hasattr(self
, "X_val"):
823 print(f
"Reshaping validation data using batch size: {batch_size} and timesteps: {timesteps}")
824 self
.X_val
, self
.y_val
, _
= staircase_spatial(self
.X_val
, self
.y_val
, timesteps
= timesteps
, batch_size
=batch_size
, hours
=None, verbose
=verbose
)
826 print(f
"Reshaping training data using batch size: {batch_size} and timesteps: {timesteps}")
827 self
.X_train
, self
.y_train
= staircase_2(self
.X_train
, self
.y_train
, timesteps
= timesteps
, batch_size
=batch_size
, verbose
=verbose
)
828 if hasattr(self
, "X_val"):
829 print(f
"Reshaping validation data using batch size: {batch_size} and timesteps: {timesteps}")
830 self
.X_val
, self
.y_val
= staircase_2(self
.X_val
, self
.y_val
, timesteps
= timesteps
, batch_size
=batch_size
, verbose
=verbose
)
832 def print_hashes(self
, attrs_to_check
= ['X', 'y', 'X_train', 'y_train', 'X_val', 'y_val', 'X_test', 'y_test']):
834 Prints the hash of specified data attributes.
838 attrs_to_check : list, optional
839 A list of attribute names to hash and print. Default includes 'X', 'y', and split data.
841 for attr
in attrs_to_check
:
842 if hasattr(self
, attr
):
843 value
= getattr(self
, attr
)
847 print(f
"Hash of {attr}: {hash_ndarray(value)}")
848 def __getattr__(self
, key
):
850 Allows attribute-style access to dictionary keys, a.k.a. enables the "." operator for get elements
855 raise AttributeError(f
"'rnn_data' object has no attribute '{key}'")
857 def __setitem__(self
, key
, value
):
859 Ensures dictionary and attribute updates stay in sync for required keys.
861 super().__setitem
__(key
, value
) # Update the dictionary
862 if key
in self
.required_keys
:
863 super().__setattr
__(key
, value
) # Ensure the attribute is updated as well
865 def __setattr__(self
, key
, value
):
867 Ensures dictionary keys are updated when setting attributes.
872 # Function to check reproduciblity hashes, environment info, and model parameters
873 def check_reproducibility(dict0
, params
, m_hash
, w_hash
):
875 Performs reproducibility checks on a model by comparing current settings and outputs with stored reproducibility information.
880 The data dictionary that should contain reproducibility information under the 'repro_info' attribute.
882 The current model parameters to be checked against the reproducibility information.
884 The hash of the current model predictions.
886 The hash of the current fitted model weights.
891 The function returns None. It issues warnings if any reproducibility checks fail.
895 - Checks are only performed if the `dict0` contains the 'repro_info' attribute.
896 - Issues warnings for mismatches in model weights, predictions, Python version, TensorFlow version, and model parameters.
897 - Skips checks if physics-based initialization is used (not implemented).
899 if not hasattr(dict0
, "repro_info"):
900 warnings
.warn("The provided data dictionary does not have the required 'repro_info' attribute. Not running reproduciblity checks.")
903 repro_info
= dict0
.repro_info
905 if params
['phys_initialize']:
906 hashes
= repro_info
['phys_initialize']
907 warnings
.warn("Physics Initialization not implemented yet. Not running reproduciblity checks.")
909 hashes
= repro_info
['rand_initialize']
910 print(f
"Fitted weights hash: {w_hash} \n Reproducibility weights hash: {hashes['fitted_weights_hash']}")
911 print(f
"Model predictions hash: {m_hash} \n Reproducibility preds hash: {hashes['preds_hash']}")
912 if (w_hash
!= hashes
['fitted_weights_hash']) or (m_hash
!= hashes
['preds_hash']):
913 if w_hash
!= hashes
['fitted_weights_hash']:
914 warnings
.warn("The fitted weights hash does not match the reproducibility weights hash.")
915 if m_hash
!= hashes
['preds_hash']:
916 warnings
.warn("The predictions hash does not match the reproducibility predictions hash.")
918 print("***Reproducibility Checks passed - model weights and model predictions match expected.***")
921 current_py_version
= sys
.version
[0:6]
922 current_tf_version
= tf
.__version
__
923 if current_py_version
!= repro_info
['env_info']['py_version']:
924 warnings
.warn(f
"Python version mismatch: Current Python version is {current_py_version}, "
925 f
"expected {repro_info['env_info']['py_version']}.")
927 if current_tf_version
!= repro_info
['env_info']['tf_version']:
928 warnings
.warn(f
"TensorFlow version mismatch: Current TensorFlow version is {current_tf_version}, "
929 f
"expected {repro_info['env_info']['tf_version']}.")
932 repro_params
= repro_info
.get('params', {})
934 for key
, repro_value
in repro_params
.items():
936 if params
[key
] != repro_value
:
937 warnings
.warn(f
"Parameter mismatch for '{key}': Current value is {params[key]}, "
938 f
"repro value is {repro_value}.")
940 warnings
.warn(f
"Parameter '{key}' is missing in the current params.")
946 Abstract base class for RNN models, providing structure for training, predicting, and running reproducibility checks.
948 def __init__(self
, params
: dict):
950 Initializes the RNNModel with the given parameters.
955 A dictionary containing model parameters.
958 if type(self
) is RNNModel
:
959 raise TypeError("MLModel is an abstract class and cannot be instantiated directly")
963 def _build_model_train(self
):
964 """Abstract method to build the training model."""
968 def _build_model_predict(self
, return_sequences
=True):
969 """Abstract method to build the prediction model. This model copies weights from the train model but with input structure that allows for easier prediction of arbitrary length timeseries. This model is not to be used for training, or don't use with .fit calls"""
972 def is_stateful(self
):
974 Checks whether any of the layers in the internal model (self.model_train) are stateful.
977 bool: True if at least one layer in the model is stateful, False otherwise.
979 This method iterates over all the layers in the model and checks if any of them
980 have the 'stateful' attribute set to True. This is useful for determining if
981 the model is designed to maintain state across batches during training.
987 for layer
in self
.model_train
.layers
:
988 if hasattr(layer
, 'stateful') and layer
.stateful
:
992 def fit(self
, X_train
, y_train
, plot_history
=True, plot_title
= '',
993 weights
=None, callbacks
=[], validation_data
=None, *args
, **kwargs
):
995 Trains the model on the provided training data. Uses the fit method of the training model and then copies the weights over to the prediction model, which has a less restrictive input shape. Formats a list of callbacks to use within the fit method based on params input
1000 The input matrix data for training.
1001 y_train : np.ndarray
1002 The target vector data for training.
1003 plot_history : bool, optional
1004 If True, plots the training history. Default is True.
1005 plot_title : str, optional
1006 The title for the training plot. Default is an empty string.
1008 Initial weights for the model. Default is None.
1009 callbacks : list, optional
1010 A list of callback functions to use during training. Default is an empty list.
1011 validation_data : tuple, optional
1012 Validation data to use during training, expected format (X_val, y_val). Default is None.
1014 # verbose_fit argument is for printing out update after each epoch, which gets very long
1015 verbose_fit
= self
.params
['verbose_fit']
1016 verbose_weights
= self
.params
['verbose_weights']
1018 print(f
"Training simple RNN with params: {self.params}")
1021 if self
.params
["reset_states"]:
1022 callbacks
=callbacks
+[ResetStatesCallback(self
.params
), TerminateOnNaN()]
1024 # Early stopping callback requires validation data
1025 if validation_data
is not None:
1026 X_val
, y_val
=validation_data
[0], validation_data
[1]
1027 print("Using early stopping callback.")
1028 callbacks
=callbacks
+[EarlyStoppingCallback(patience
= self
.params
['early_stopping_patience'])]
1030 print(f
"Formatted X_train hash: {hash_ndarray(X_train)}")
1031 print(f
"Formatted y_train hash: {hash_ndarray(y_train)}")
1032 if validation_data
is not None:
1033 print(f
"Formatted X_val hash: {hash_ndarray(X_val)}")
1034 print(f
"Formatted y_val hash: {hash_ndarray(y_val)}")
1035 print(f
"Initial weights before training hash: {hash_weights(self.model_train)}")
1037 ## TODO: Hidden State Initialization
1038 # Evaluate Model once to set nonzero initial state
1039 # self.model_train(X_train[0:self.params['batch_size'],:,:])
1041 if validation_data
is not None:
1042 history
= self
.model_train
.fit(
1043 X_train
, y_train
+self
.params
['centering'][1],
1044 epochs
=self
.params
['epochs'],
1045 batch_size
=self
.params
['batch_size'],
1046 callbacks
= callbacks
,
1047 verbose
=verbose_fit
,
1048 validation_data
= (X_val
, y_val
),
1052 history
= self
.model_train
.fit(
1053 X_train
, y_train
+self
.params
['centering'][1],
1054 epochs
=self
.params
['epochs'],
1055 batch_size
=self
.params
['batch_size'],
1056 callbacks
= callbacks
,
1057 verbose
=verbose_fit
,
1062 self
.plot_history(history
,plot_title
)
1064 if self
.params
["verbose_weights"]:
1065 print(f
"Fitted Weights Hash: {hash_weights(self.model_train)}")
1067 # Update Weights for Prediction Model
1068 w_fitted
= self
.model_train
.get_weights()
1069 self
.model_predict
.set_weights(w_fitted
)
1071 def predict(self
, X_test
):
1073 Generates predictions on the provided test data using the internal prediction model.
1078 The input data for generating predictions.
1083 The predicted values.
1085 print("Predicting test data")
1086 X_test
= self
._format
_pred
_data
(X_test
)
1087 preds
= self
.model_predict
.predict(X_test
).flatten()
1091 def _format_pred_data(self
, X
):
1093 Formats the prediction data for RNN input.
1103 The formatted input data.
1105 return np
.reshape(X
,(1, X
.shape
[0], self
.params
['n_features']))
1107 def plot_history(self
, history
, plot_title
, create_figure
=True):
1109 Plots the training history. Uses log scale on y axis for readability.
1113 history : History object
1114 The training history object from model fitting. Output of keras' .fit command
1116 The title for the plot.
1120 plt
.semilogy(history
.history
['loss'], label
='Training loss')
1121 if 'val_loss' in history
.history
:
1122 plt
.semilogy(history
.history
['val_loss'], label
='Validation loss')
1123 plt
.title(f
'{plot_title} Model loss')
1126 plt
.legend(loc
='upper left')
1129 def run_model(self
, dict0
, reproducibility_run
=False, plot_period
='all', save_outputs
=True):
1131 Runs the RNN model on input data dictionary, including training, prediction, and reproducibility checks.
1135 dict0 : RNNData (dict)
1136 The dictionary containing the input data and configuration.
1137 reproducibility_run : bool, optional
1138 If True, performs reproducibility checks after running the model. Default is False.
1140 If True, writes model outputs into input dictionary.
1145 Model predictions and a dictionary of RMSE errors broken up by time period.
1147 verbose_fit
= self
.params
['verbose_fit']
1148 verbose_weights
= self
.params
['verbose_weights']
1150 print("Input data hashes, NOT formatted for rnn sequence/batches yet")
1151 dict0
.print_hashes()
1153 X_train
, y_train
, X_test
, y_test
= dict0
.X_train
, dict0
.y_train
, dict0
.X_test
, dict0
.y_test
1154 if 'X_val' in dict0
:
1155 X_val
, y_val
= dict0
.X_val
, dict0
.y_val
1158 case_id
= dict0
.case
1162 self
.fit(X_train
, y_train
, plot_title
=case_id
)
1164 self
.fit(X_train
, y_train
, validation_data
= (X_val
, y_val
), plot_title
=case_id
)
1166 # Generate Predictions and Evaluate Test Error
1168 m
, errs
= self
._eval
_multi
(dict0
)
1172 m
, errs
= self
._eval
_single
(dict0
, verbose_weights
, reproducibility_run
)
1175 plot_data(dict0
, title
="RNN", title2
=dict0
.case
, plot_period
=plot_period
)
1179 def _eval_single(self
, dict0
, verbose_weights
, reproducibility_run
):
1180 # Generate Predictions,
1181 # run through training to get hidden state set properly for forecast period
1182 print(f
"Running prediction on all input data, Training through Test")
1183 X
= dict0
.scale_all_X()
1184 y
= dict0
.y
.flatten()
1187 print(f
"All X hash: {hash_ndarray(X)}")
1189 m
= self
.predict(X
).flatten()
1191 print(f
"Predictions Hash: {hash_ndarray(m)}")
1193 if reproducibility_run
:
1194 print("Checking Reproducibility")
1195 check_reproducibility(dict0
, self
.params
, hash_ndarray(m
), hash_weights(self
.model_predict
))
1197 # print(dict0.keys())
1198 # Plot final fit and data
1200 # plot_data(dict0, title="RNN", title2=dict0['case'], plot_period=plot_period)
1204 train_ind
= dict0
.train_ind
# index of final training set value
1205 test_ind
= dict0
.test_ind
# index of first test set value
1207 err_train
= rmse(m
[:train_ind
], y
[:train_ind
].flatten())
1208 err_pred
= rmse(m
[test_ind
:], y
[test_ind
:].flatten())
1211 'training': err_train
,
1212 'prediction': err_pred
1216 def _eval_multi(self
, dict0
):
1217 # Train Error: NOT DOING YET. DECIDE WHETHER THIS IS NEEDED
1220 new_data
= np
.stack(dict0
.X_test
, axis
=0)
1221 y_array
= np
.stack(dict0
.y_test
, axis
=0)
1222 preds
= self
.model_predict
.predict(new_data
)
1225 ## Note: not using util rmse function since this approach is for 3d arrays
1226 # Compute the squared differences
1227 squared_diff
= np
.square(preds
- y_array
)
1229 # Mean squared error along the timesteps and dimensions (axis 1 and 2)
1230 mse
= np
.mean(squared_diff
, axis
=(1, 2))
1232 # Root mean squared error (RMSE) for each timeseries
1233 rmses
= np
.sqrt(mse
)
1240 # Helper functions for batch reset schedules
1241 def calc_exp_intervals(bmin
, bmax
, n_epochs
, force_bmax
= True):
1242 # Calculate the exponential intervals for each epoch
1243 epochs
= np
.arange(n_epochs
)
1244 factors
= epochs
/ n_epochs
1245 intervals
= bmin
* (bmax
/ bmin
) ** factors
1247 intervals
[-1] = bmax
# Ensure the last value is exactly bmax
1248 return intervals
.astype(int)
1250 def calc_log_intervals(bmin
, bmax
, n_epochs
, force_bmax
= True):
1251 # Calculate the logarithmic intervals for each epoch
1252 epochs
= np
.arange(n_epochs
)
1253 factors
= np
.log(1 + epochs
) / np
.log(1 + n_epochs
)
1254 intervals
= bmin
+ (bmax
- bmin
) * factors
1256 intervals
[-1] = bmax
# Ensure the last value is exactly bmax
1257 return intervals
.astype(int)
1259 class ResetStatesCallback(Callback
):
1261 Custom callback to reset the states of RNN layers at the end of each epoch and optionally after a specified number of batches.
1265 batch_reset : int, optional
1266 If provided, resets the states of RNN layers after every `batch_reset` batches. Default is None.
1268 # def __init__(self, bmin=None, bmax=None, epochs=None, loc_batch_reset = None, batch_schedule_type='linear', verbose=True):
1269 def __init__(self
, params
=None, verbose
=True):
1271 Initializes the ResetStatesCallback with an optional batch reset interval.
1275 params: dict, optional
1276 Dictionary of parameters. If None provided, only on_epoch_end will trigger reset of hidden states.
1278 Minimum for batch reset schedule
1280 Maximum for batch reset schedule
1282 Number of training epochs.
1283 - loc_batch_reset : int
1284 Interval of batches after which to reset the states of RNN layers for location changes. Triggers reset for training AND validation phases
1285 - batch_schedule_type : str
1286 Type of batch scheduling to be used. Recognized methods are following:
1287 - 'constant' : Used fixed batch reset interval throughout training
1288 - 'linear' : Increases the batch reset interval linearly over epochs from bmin to bmax.
1289 - 'exp' : Increases the batch reset interval exponentially over epochs from bmin to bmax.
1290 - 'log' : Increases the batch reset interval logarithmically over epochs from bmin to bmax.
1295 Only in-place reset of hidden states of RNN that calls uses this callback.
1298 super(ResetStatesCallback
, self
).__init
__()
1300 # Check for optional arguments, set None if missing in input params
1301 arg_list
= ['bmin', 'bmax', 'epochs', 'loc_batch_reset', 'batch_schedule_type']
1302 for arg
in arg_list
:
1303 setattr(self
, arg
, params
.get(arg
, None))
1305 self
.verbose
= verbose
1307 print(f
"Using ResetStatesCallback with Batch Reset Schedule: {self.batch_schedule_type}")
1308 # Calculate the reset intervals for each epoch during initialization
1309 if self
.batch_schedule_type
is not None:
1310 if self
.epochs
is None:
1311 raise ValueError(f
"Arugment `epochs` cannot be none with self.batch_schedule_type: {self.batch_schedule_type}")
1312 self
.batch_reset_intervals
= self
._calc
_reset
_intervals
(self
.batch_schedule_type
)
1314 print(f
"batch_reset_intervals: {self.batch_reset_intervals}")
1316 self
.batch_reset_intervals
= None
1317 def on_epoch_end(self
, epoch
, logs
=None):
1319 Resets the states of RNN layers at the end of each epoch.
1324 The index of the current epoch.
1325 logs : dict, optional
1326 A dictionary containing metrics from the epoch. Default is None.
1328 # print(f" Resetting hidden state after epoch: {epoch+1}", flush=True)
1329 # Iterate over each layer in the model
1330 for layer
in self
.model
.layers
:
1331 # Check if the layer has a reset_states method
1332 if hasattr(layer
, 'reset_states'):
1333 layer
.reset_states()
1334 def _calc_reset_intervals(self
,batch_schedule_type
):
1335 methods
= ['constant', 'linear', 'exp', 'log']
1336 if batch_schedule_type
not in methods
:
1337 raise ValueError(f
"Batch schedule method {batch_schedule_type} not recognized. \n Available methods: {methods}")
1338 if batch_schedule_type
== "constant":
1340 return np
.repeat(self
.bmin
, self
.epochs
).astype(int)
1341 elif batch_schedule_type
== "linear":
1342 return np
.linspace(self
.bmin
, self
.bmax
, self
.epochs
).astype(int)
1343 elif batch_schedule_type
== "exp":
1344 return calc_exp_intervals(self
.bmin
, self
.bmax
, self
.epochs
)
1345 elif batch_schedule_type
== "log":
1346 return calc_log_intervals(self
.bmin
, self
.bmax
, self
.epochs
)
1347 def on_epoch_begin(self
, epoch
, logs
=None):
1348 # Set the reset interval for the current epoch
1349 if self
.batch_reset_intervals
is not None:
1350 self
.current_batch_reset
= self
.batch_reset_intervals
[epoch
]
1352 self
.current_batch_reset
= None
1353 def on_train_batch_end(self
, batch
, logs
=None):
1355 Resets the states of RNN layers during training after a specified number of batches, if `batch_reset` or `loc_batch_reset` are provided. The `batch_reset` is used for stability and to avoid exploding gradients at the beginning of training when a hidden state is being passed with weights that haven't learned yet. The `loc_batch_reset` is used to reset the states when a particular batch is from a new location and thus the hidden state should be passed.
1360 The index of the current batch.
1361 logs : dict, optional
1362 A dictionary containing metrics from the batch. Default is None.
1364 batch_reset
= self
.current_batch_reset
1365 if (batch_reset
is not None and batch
% batch_reset
== 0):
1366 # print(f" Resetting states after batch {batch + 1}")
1367 # Iterate over each layer in the model
1368 for layer
in self
.model
.layers
:
1369 # Check if the layer has a reset_states method
1370 if hasattr(layer
, 'reset_states'):
1371 layer
.reset_states()
1372 def on_test_batch_end(self
, batch
, logs
=None):
1374 Resets the states of RNN layers during validation if `loc_batch_reset` is provided to demarcate a new location and thus avoid passing a hidden state to a wrong location.
1379 The index of the current batch.
1380 logs : dict, optional
1381 A dictionary containing metrics from the batch. Default is None.
1383 loc_batch_reset
= self
.loc_batch_reset
1384 if (loc_batch_reset
is not None and batch
% loc_batch_reset
== 0):
1385 # print(f"Resetting states in Validation mode after batch {batch + 1}")
1386 # Iterate over each layer in the model
1387 for layer
in self
.model
.layers
:
1388 # Check if the layer has a reset_states method
1389 if hasattr(layer
, 'reset_states'):
1390 layer
.reset_states()
1392 ## Learning Schedules
1394 lr_schedule
= tf
.keras
.optimizers
.schedules
.CosineDecay(
1395 initial_learning_rate
=0.001,
1399 # warmup_target=None,
1404 def EarlyStoppingCallback(patience
=5):
1406 Creates an EarlyStopping callback with the specified patience.
1409 patience (int): Number of epochs with no improvement after which training will be stopped.
1412 EarlyStopping: Configured EarlyStopping callback.
1414 return EarlyStopping(
1419 restore_best_weights
=True
1423 'DeltaE': [0,-1], # bias correction
1424 'T1': 0.1, # 1/fuel class (10)
1425 'fm_raise_vs_rain': 0.2 # fm increase per mm rain
1430 def get_initial_weights(model_fit
,params
,scale_fm
=1):
1431 # Given a RNN architecture and hyperparameter dictionary, return array of physics-initiated weights
1433 # model_fit: output of create_RNN_2 with no training
1434 # params: (dict) dictionary of hyperparameters
1435 # rnn_dat: (dict) data dictionary, output of create_rnn_dat
1436 # Returns: numpy ndarray of weights that should be a rough solution to the moisture ODE
1437 DeltaE
= phys_params
['DeltaE']
1438 T1
= phys_params
['T1']
1439 fmr
= phys_params
['fm_raise_vs_rain']
1440 centering
= params
['centering'] # shift activation down
1442 w0_initial
={'Ed':(1.-np
.exp(-T1
))/2,
1443 'Ew':(1.-np
.exp(-T1
))/2,
1444 'rain':fmr
* scale_fm
} # wx - input feature
1445 # wh wb wd bd = bias -1
1447 w_initial
=np
.array([np
.nan
, np
.exp(-0.1), DeltaE
[0]/scale_fm
, # layer 0
1448 1.0, -centering
[0] + DeltaE
[1]/scale_fm
]) # layer 1
1449 if params
['verbose_weights']:
1450 print('Equilibrium moisture correction bias',DeltaE
[0],
1451 'in the hidden layer and',DeltaE
[1],' in the output layer')
1453 w_name
= ['wx','wh','bh','wd','bd']
1455 w
=model_fit
.get_weights()
1456 for j
in range(w
[0].shape
[0]):
1457 feature
= params
['features_list'][j
]
1458 for k
in range(w
[0].shape
[1]):
1459 w
[0][j
][k
]=w0_initial
[feature
]
1460 for i
in range(1,len(w
)): # number of the weight
1461 for j
in range(w
[i
].shape
[0]): # number of the inputs
1463 # initialize all entries of the weight matrix to the same number
1464 for k
in range(w
[i
].shape
[1]):
1465 w
[i
][j
][k
]=w_initial
[i
]/w
[i
].shape
[0]
1467 w
[i
][j
]=w_initial
[i
]
1469 print('weight',i
,'shape',w
[i
].shape
)
1470 raise ValueError("Only 1 or 2 dimensions supported")
1471 if params
['verbose_weights']:
1472 print('weight',i
,w_name
[i
],'shape',w
[i
].shape
,'ndim',w
[i
].ndim
,
1473 'initial: sum',np
.sum(w
[i
],axis
=0),'\nentries',w
[i
])
1477 class RNN(RNNModel
):
1479 A concrete implementation of the RNNModel abstract base class, using simple recurrent cells for hidden recurrent layers.
1484 A dictionary of model parameters.
1485 loss : str, optional
1486 The loss function to use during model training. Default is 'mean_squared_error'.
1488 def __init__(self
, params
, loss
='mean_squared_error'):
1490 Initializes the RNN model by building the training and prediction models.
1494 params : dict or RNNParams
1495 A dictionary containing the model's parameters.
1496 loss : str, optional
1497 The loss function to use during model training. Default is 'mean_squared_error'.
1499 super().__init
__(params
)
1500 self
.model_train
= self
._build
_model
_train
()
1501 self
.model_predict
= self
._build
_model
_predict
()
1503 def _build_model_train(self
):
1505 Builds and compiles the training model, with batch & sequence shape specifications for input.
1509 model : tf.keras.Model
1510 The compiled Keras model for training.
1512 inputs
= tf
.keras
.Input(batch_shape
=self
.params
['batch_shape'])
1514 for i
in range(self
.params
['rnn_layers']):
1515 # Return sequences True if recurrent layer feeds into another recurrent layer.
1516 # False if feeds into dense layer
1517 return_sequences
= True if i
< self
.params
['rnn_layers'] - 1 else False
1519 units
=self
.params
['rnn_units'],
1520 activation
=self
.params
['activation'][0],
1521 dropout
=self
.params
["dropout"][0],
1522 recurrent_dropout
= self
.params
["recurrent_dropout"],
1523 stateful
=self
.params
['stateful'],
1524 return_sequences
=return_sequences
)(x
)
1525 if self
.params
["dropout"][1] > 0:
1526 x
= Dropout(self
.params
["dropout"][1])(x
)
1527 for i
in range(self
.params
['dense_layers']):
1528 x
= Dense(self
.params
['dense_units'], activation
=self
.params
['activation'][1])(x
)
1529 # Add final output layer, must be 1 dense cell with linear activation if continuous scalar output
1530 x
= Dense(units
=1, activation
='linear')(x
)
1531 model
= tf
.keras
.Model(inputs
=inputs
, outputs
=x
)
1532 optimizer
=tf
.keras
.optimizers
.Adam(learning_rate
=self
.params
['learning_rate'])
1533 model
.compile(loss
='mean_squared_error', optimizer
=optimizer
)
1535 if self
.params
["verbose_weights"]:
1536 print(f
"Initial Weights Hash: {hash_weights(model)}")
1537 # print(model.get_weights())
1539 if self
.params
['phys_initialize']:
1540 assert self
.params
['scaler'] == 'reproducibility', f
"Not implemented yet to do physics initialize with given data scaling {self.params['scaler']}"
1541 assert self
.params
['features_list'] == ['Ed', 'Ew', 'rain'], f
"Physics initiation can only be done with features ['Ed', 'Ew', 'rain'], but given features {self.params['features_list']}"
1542 print("Initializing Model with Physics based weights")
1543 w
, w_name
=get_initial_weights(model
, self
.params
)
1544 model
.set_weights(w
)
1545 print('initial weights hash =',hash_weights(model
))
1548 def _build_model_predict(self
, return_sequences
=True):
1550 Builds and compiles the prediction model, doesn't use batch shape nor sequence length to make it easier to predict arbitrary number of timesteps. This model has weights copied over from training model is not directly used for training itself.
1554 return_sequences : bool, optional
1555 Whether to return the full sequence of outputs. Default is True.
1559 model : tf.keras.Model
1560 The compiled Keras model for prediction.
1562 inputs
= tf
.keras
.Input(shape
=(None,self
.params
['n_features']))
1564 for i
in range(self
.params
['rnn_layers']):
1565 x
= SimpleRNN(self
.params
['rnn_units'],activation
=self
.params
['activation'][0],
1566 stateful
=False,return_sequences
=return_sequences
)(x
)
1567 for i
in range(self
.params
['dense_layers']):
1568 x
= Dense(self
.params
['dense_units'], activation
=self
.params
['activation'][1])(x
)
1569 # Add final output layer, must be 1 dense cell with linear activation if continuous scalar output
1570 x
= Dense(units
=1, activation
='linear')(x
)
1571 model
= tf
.keras
.Model(inputs
=inputs
, outputs
=x
)
1572 optimizer
=tf
.keras
.optimizers
.Adam(learning_rate
=self
.params
['learning_rate'])
1573 model
.compile(loss
='mean_squared_error', optimizer
=optimizer
)
1575 # Set Weights to model_train
1576 w_fitted
= self
.model_train
.get_weights()
1577 model
.set_weights(w_fitted
)
1582 class RNN_LSTM(RNNModel
):
1584 A concrete implementation of the RNNModel abstract base class, use LSTM cells for hidden recurrent layers.
1589 A dictionary of model parameters.
1590 loss : str, optional
1591 The loss function to use during model training. Default is 'mean_squared_error'.
1593 def __init__(self
, params
, loss
='mean_squared_error'):
1595 Initializes the RNN model by building the training and prediction models.
1599 params : dict or RNNParams
1600 A dictionary containing the model's parameters.
1601 loss : str, optional
1602 The loss function to use during model training. Default is 'mean_squared_error'.
1604 super().__init
__(params
)
1605 self
.model_train
= self
._build
_model
_train
()
1606 self
.model_predict
= self
._build
_model
_predict
()
1608 def _build_model_train(self
):
1610 Builds and compiles the training model, with batch & sequence shape specifications for input.
1614 model : tf.keras.Model
1615 The compiled Keras model for training.
1617 inputs
= tf
.keras
.Input(batch_shape
=self
.params
['batch_shape'])
1619 for i
in range(self
.params
['rnn_layers']):
1620 return_sequences
= True if i
< self
.params
['rnn_layers'] - 1 else False
1622 units
=self
.params
['rnn_units'],
1623 activation
=self
.params
['activation'][0],
1624 dropout
=self
.params
["dropout"][0],
1625 recurrent_dropout
= self
.params
["recurrent_dropout"],
1626 recurrent_activation
=self
.params
["recurrent_activation"],
1627 stateful
=self
.params
['stateful'],
1628 return_sequences
=return_sequences
)(x
)
1629 if self
.params
["dropout"][1] > 0:
1630 x
= Dropout(self
.params
["dropout"][1])(x
)
1631 for i
in range(self
.params
['dense_layers']):
1632 x
= Dense(self
.params
['dense_units'], activation
=self
.params
['activation'][1])(x
)
1633 model
= tf
.keras
.Model(inputs
=inputs
, outputs
=x
)
1634 # optimizer=tf.keras.optimizers.Adam(learning_rate=self.params['learning_rate'], clipvalue=self.params['clipvalue'])
1635 optimizer
=tf
.keras
.optimizers
.Adam(learning_rate
=self
.params
['learning_rate'])
1636 model
.compile(loss
='mean_squared_error', optimizer
=optimizer
)
1638 if self
.params
["verbose_weights"]:
1639 print(f
"Initial Weights Hash: {hash_weights(model)}")
1641 def _build_model_predict(self
, return_sequences
=True):
1643 Builds and compiles the prediction model, doesn't use batch shape nor sequence length to make it easier to predict arbitrary number of timesteps. This model has weights copied over from training model is not directly used for training itself.
1647 return_sequences : bool, optional
1648 Whether to return the full sequence of outputs. Default is True.
1652 model : tf.keras.Model
1653 The compiled Keras model for prediction.
1655 inputs
= tf
.keras
.Input(shape
=(None,self
.params
['n_features']))
1657 for i
in range(self
.params
['rnn_layers']):
1659 units
=self
.params
['rnn_units'],
1660 activation
=self
.params
['activation'][0],
1661 stateful
=False,return_sequences
=return_sequences
)(x
)
1662 for i
in range(self
.params
['dense_layers']):
1663 x
= Dense(self
.params
['dense_units'], activation
=self
.params
['activation'][1])(x
)
1664 model
= tf
.keras
.Model(inputs
=inputs
, outputs
=x
)
1665 optimizer
=tf
.keras
.optimizers
.Adam(learning_rate
=self
.params
['learning_rate'])
1666 model
.compile(loss
='mean_squared_error', optimizer
=optimizer
)
1668 # Set Weights to model_train
1669 w_fitted
= self
.model_train
.get_weights()
1670 model
.set_weights(w_fitted
)