DSatishchandra commited on
Commit
65c2ae6
Β·
verified Β·
1 Parent(s): 01ce2c5

Update train_lstm_model.py

Browse files
Files changed (1) hide show
  1. train_lstm_model.py +15 -23
train_lstm_model.py CHANGED
@@ -47,7 +47,7 @@ scaler = MinMaxScaler(feature_range=(0, 1))
47
  energy_data = scaler.fit_transform(energy_data)
48
  print("Data normalized successfully.")
49
 
50
- # Prepare the dataset for LSTM (using 60 previous time steps to predict the next time step)
51
  X, y = [], []
52
  for i in range(60, len(energy_data)):
53
  X.append(energy_data[i-60:i, 0])
@@ -55,7 +55,7 @@ for i in range(60, len(energy_data)):
55
  X, y = np.array(X), np.array(y)
56
  print(f"Prepared data for LSTM: X shape = {X.shape}, y shape = {y.shape}")
57
 
58
- # Reshape X for LSTM input (samples, time steps, features)
59
  X = X.reshape((X.shape[0], X.shape[1], 1))
60
  print(f"Reshaped X for LSTM input: X shape = {X.shape}")
61
 
@@ -71,27 +71,19 @@ model.compile(optimizer=Adam(), loss='mean_squared_error')
71
  model.fit(X, y, epochs=10, batch_size=32, verbose=1)
72
  print("Model training completed.")
73
 
74
- # Ensure the models directory exists and has write permissions
75
- os.makedirs('models', exist_ok=True)
76
- if not os.access('models', os.W_OK):
77
- raise PermissionError("No write permissions for the 'models' directory.")
 
78
  print("Models directory created and writable.")
79
 
80
- # Remove any existing (invalid) model file
81
- model_path = 'models/lstm_energy_model.h5'
82
- if os.path.exists(model_path):
83
- os.remove(model_path)
84
- print(f"Removed existing (invalid) file at {model_path}")
85
 
86
- # Save the trained model
87
- try:
88
- model.save(model_path)
89
- except Exception as e:
90
- raise RuntimeError(f"Failed to save model to {model_path}: {e}")
91
- print(f"Model saved to {model_path}")
92
-
93
- # Verify the saved model file
94
- if os.path.exists(model_path) and os.path.getsize(model_path) > 1024: # Expect at least 1 KB
95
- print(f"βœ… LSTM model trained and saved as {model_path} (Size: {os.path.getsize(model_path) / 1024:.2f} KB)")
96
- else:
97
- raise RuntimeError(f"Model file {model_path} is invalid or too small (Size: {os.path.getsize(model_path)} bytes).")
 
47
  energy_data = scaler.fit_transform(energy_data)
48
  print("Data normalized successfully.")
49
 
50
+ # Prepare the dataset for LSTM
51
  X, y = [], []
52
  for i in range(60, len(energy_data)):
53
  X.append(energy_data[i-60:i, 0])
 
55
  X, y = np.array(X), np.array(y)
56
  print(f"Prepared data for LSTM: X shape = {X.shape}, y shape = {y.shape}")
57
 
58
+ # Reshape X for LSTM input
59
  X = X.reshape((X.shape[0], X.shape[1], 1))
60
  print(f"Reshaped X for LSTM input: X shape = {X.shape}")
61
 
 
71
  model.fit(X, y, epochs=10, batch_size=32, verbose=1)
72
  print("Model training completed.")
73
 
74
+ # Ensure the models directory exists
75
+ model_path = 'models/lstm_energy_model'
76
+ os.makedirs(model_path, exist_ok=True)
77
+ if not os.access(model_path, os.W_OK):
78
+ raise PermissionError("No write permissions for the 'models/lstm_energy_model' directory.")
79
  print("Models directory created and writable.")
80
 
81
+ # Remove any existing model directory
82
+ if os.path.exists(model_path) and os.path.isdir(model_path):
83
+ shutil.rmtree(model_path)
84
+ print(f"Removed existing directory at {model_path}")
85
+ os.makedirs(model_path, exist_ok=True)
86
 
87
+ # Save the trained model in SavedModel format
88
+ model.save(model_path, save_format='tf')
89
+ print(f"βœ… LSTM model trained and saved as SavedModel at {model_path}")