|
|
|
|
|
**Outline of model training**
|
|
|
|
|
|
**default**
|
|
|
|
|
|
4 different locations
|
|
|
|
|
|
|
|
|
- (48.3330, 16.6319), # Vienna, Austria
|
|
|
- (64.1355, -21.8954), # Reykjavik, Iceland
|
|
|
- (35.1667, 33.3667), # Nicosia, Cyprus
|
|
|
- (59.3293, 18.0686), # Stockholm, Sweden
|
|
|
|
|
|
|
|
|
epochs=200
|
|
|
|
|
|
batch_size=64
|
|
|
|
|
|
callbacks=[checkpoint, early_stopping (patience=5), reduce_lr(factor=0.1, patience=5, min_lr=1e-6)]
|
|
|
|
|
|
learning_rate=0.001
|
|
|
|
|
|
LSTM layer with 128 units
|
|
|
|
|
|
LSTM output layer
|
|
|
|
|
|
**testing**
|
|
|
|
|
|
LSTM units 128, 64, 32
|
|
|
|
|
|
Dense units 128, 64, 32
|
|
|
|
|
|
bidir units 128, 64
|
|
|
|
|
|
batch_size 128, 64, 32
|
|
|
|
|
|
2 lstm layers
|
|
|
|
|
|
learning rate
|
|
|
|
|
|
dropout layer
|
|
|
|
|
|
patience
|
|
|
|
|
|
combinations of the parameters with better results
|
|
|
**function for training LSTM Model**
|
|
|
|
|
|
```
|
|
|
def train_model(model, X_train, y_train, X_test, y_test):
|
|
|
epochs=200
|
|
|
batch_size=64
|
|
|
|
|
|
early_stopping = EarlyStopping(monitor='val_loss', patience=5, restore_best_weights=True)
|
|
|
def train_model(base_path, model, X_train, y_train, X_test, y_test, epochs, es_patience, batch_size, rlr_patience, rlr_factor):
|
|
|
early_stopping = EarlyStopping(monitor='val_loss', patience=es_patience, restore_best_weights=True)
|
|
|
|
|
|
# Learning rate scheduler callback
|
|
|
reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=5, min_lr=1e-6)
|
|
|
|
|
|
reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=rlr_factor, patience=rlr_patience, min_lr=1e-6)
|
|
|
|
|
|
checkpoint_path = "checkpoints/model_epoch_{epoch:02d}_val_loss_{val_loss:.2f}.weights.h5"
|
|
|
checkpoint_path = base_path + "checkpoints/model_epoch_{epoch:02d}_val_loss_{val_loss:.2f}.weights.h5"
|
|
|
checkpoint = ModelCheckpoint(
|
|
|
filepath=checkpoint_path, save_weights_only=True,
|
|
|
monitor='val_loss', mode='min', save_best_only=True, verbose=1
|
... | ... | @@ -64,59 +16,42 @@ def train_model(model, X_train, y_train, X_test, y_test): |
|
|
history = model.fit(X_train, y_train, epochs=epochs, batch_size=batch_size,
|
|
|
validation_data=(X_test, y_test), callbacks=[checkpoint, early_stopping, reduce_lr])
|
|
|
return model, history
|
|
|
```
|
|
|
|
|
|
|
|
|
model1 = Sequential([
|
|
|
```
|
|
|
lstm_model = Sequential([
|
|
|
Input(shape=(sequence_length, feature_count)),
|
|
|
LSTM(128, return_sequences=True),
|
|
|
LSTM(sequence_length) # Ensure the output shape matches y
|
|
|
LSTM(sequence_length)
|
|
|
])
|
|
|
|
|
|
optimizer = Adam(learning_rate=0.001, clipvalue=1.0)
|
|
|
```
|
|
|
|
|
|
|
|
|
**After testing and more debugging I have found this to have good Correlation and RMSE values. I will keep testing and train a good model for all locations**
|
|
|
|
|
|
```
|
|
|
def train_model(model, X_train, y_train, X_test, y_test):
|
|
|
epochs=200
|
|
|
batch_size=32
|
|
|
|
|
|
early_stopping = EarlyStopping(monitor='val_loss', patience=10, restore_best_weights=True)
|
|
|
|
|
|
# Learning rate scheduler callback
|
|
|
reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=2, min_lr=1e-6)
|
|
|
|
|
|
|
|
|
checkpoint_path = "checkpoints/model_epoch_{epoch:02d}_val_loss_{val_loss:.2f}.weights.h5"
|
|
|
checkpoint = ModelCheckpoint(
|
|
|
filepath=checkpoint_path, save_weights_only=True,
|
|
|
monitor='val_loss', mode='min', save_best_only=True, verbose=1
|
|
|
)
|
|
|
|
|
|
history = model.fit(X_train, y_train, epochs=epochs, batch_size=batch_size,
|
|
|
validation_data=(X_test, y_test), callbacks=[checkpoint, early_stopping, reduce_lr])
|
|
|
return model, history
|
|
|
|
|
|
|
|
|
modelyyx = Sequential([
|
|
|
Input(shape=(sequence_length, feature_count)),
|
|
|
Bidirectional(LSTM(128, return_sequences=True)),
|
|
|
LSTM(sequence_length) # Ensure the output shape matches y
|
|
|
])
|
|
|
**LSTM Model fitted with all locations**
|
|
|
|
|
|
histories = []
|
|
|
for X_train, y_train, X_test, y_test in all_sequences:
|
|
|
optimizer = Adam(learning_rate=0.001, clipvalue=1.0)
|
|
|
modelyyx.compile(optimizer=optimizer, loss='mse')
|
|
|
print("NEW LOCATION")
|
|
|
modelyyx, history = train_model(modelyyx, X_train, y_train, X_test, y_test)
|
|
|
```
|
|
|
- lr = 0.00005 # tried 0.001, 0.0001, 0.00001
|
|
|
- epochs=200 # tried 20
|
|
|
- es_patience=15 # tried 5, 10
|
|
|
- batch_size=32 # tried 64, 128
|
|
|
- rlr_patience=3 # tried 5, 10
|
|
|
- rlr_factor=0.1 # tried 0.15, 0.2
|
|
|
|
|
|
**LSTM Model fitted with 4 locations**
|
|
|
|
|
|

|
|
|
- lr = 0.00005
|
|
|
- epochs=200
|
|
|
- es_patience=5
|
|
|
- batch_size=32
|
|
|
- rlr_patience=5
|
|
|
- rlr_factor=0.1
|
|
|
|
|
|
|
|
|
**testing different model structures**
|
|
|
|
|
|
After training this model with 20+ locations the numbers got worse, so I tried with other parameters, mainly the learning rates |
|
|
\ No newline at end of file |
|
|
- LSTM units 128, 64, 32
|
|
|
- Dense units 128, 64, 32
|
|
|
- bi directional units 128, 64
|
|
|
- batch_size 128, 64, 32
|
|
|
- 2 lstm layers
|
|
|
- learning rate
|
|
|
- patience
|
|
|
- combinations |