File size: 3,306 Bytes
b87d56f
 
 
 
 
 
10210e7
4116c4b
ea7776b
 
ed32826
fb3320e
ed32826
 
 
 
4116c4b
 
 
 
 
ed32826
b87d56f
10210e7
 
 
 
ed32826
10210e7
 
4116c4b
 
 
 
ed32826
4116c4b
 
10210e7
 
4116c4b
 
b87d56f
ed32826
b87d56f
644bc61
b87d56f
 
ed32826
b87d56f
65c2ae6
45ebd5f
b87d56f
 
 
45ebd5f
ed32826
644bc61
65c2ae6
b87d56f
ed32826
b87d56f
644bc61
b87d56f
 
 
 
ed32826
b87d56f
644bc61
b87d56f
4116c4b
ed32826
b87d56f
65c2ae6
 
 
 
 
ed32826
10210e7
65c2ae6
 
 
 
 
ea7776b
65c2ae6
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import numpy as np
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, Dense
from tensorflow.keras.optimizers import Adam
import os
import shutil
import tensorflow as tf

# Disable GPU to avoid cuDNN/cuFFT/cuBLAS conflicts
os.environ["CUDA_VISIBLE_DEVICES"] = ""
print("CUDA_VISIBLE_DEVICES set to empty to disable GPU usage.")

# Check TensorFlow version
print(f"TensorFlow version: {tf.__version__}")

# Check disk space
available_space = shutil.disk_usage('.').free / (1024 * 1024)  # Free space in MB
if available_space < 10:
    raise RuntimeError(f"Insufficient disk space: {available_space:.2f} MB available. Need at least 10 MB.")
print(f"Available disk space: {available_space:.2f} MB")

# Ensure the data file exists
data_path = 'data/energy_data.csv'
if not os.path.exists(data_path):
    raise FileNotFoundError(f"Energy data file not found at {data_path}. Please ensure the file exists.")
print(f"Data file found at {data_path}")

# Load energy generation dataset
try:
    data = pd.read_csv(data_path)
except Exception as e:
    raise RuntimeError(f"Failed to load {data_path}: {e}")
print(f"Data loaded successfully. Number of rows: {len(data)}")

# Validate the dataset
if 'energy_generation' not in data.columns:
    raise ValueError("CSV file must contain an 'energy_generation' column.")
if len(data) < 60:
    raise ValueError(f"Dataset must have at least 60 rows for LSTM training. Found {len(data)} rows.")
energy_data = data['energy_generation'].values.reshape(-1, 1)
print("Dataset validated: 'energy_generation' column present.")

# Normalize the data
scaler = MinMaxScaler(feature_range=(0, 1))
energy_data = scaler.fit_transform(energy_data)
print("Data normalized successfully.")

# Prepare the dataset for LSTM
X, y = [], []
for i in range(60, len(energy_data)):
    X.append(energy_data[i-60:i, 0])
    y.append(energy_data[i, 0])
X, y = np.array(X), np.array(y)
print(f"Prepared data for LSTM: X shape = {X.shape}, y shape = {y.shape}")

# Reshape X for LSTM input
X = X.reshape((X.shape[0], X.shape[1], 1))
print(f"Reshaped X for LSTM input: X shape = {X.shape}")

# Define the LSTM model
model = Sequential()
model.add(LSTM(units=50, return_sequences=True, input_shape=(X.shape[1], 1)))
model.add(LSTM(units=50, return_sequences=False))
model.add(Dense(units=1))
print("LSTM model defined successfully.")

# Compile and train the model
model.compile(optimizer=Adam(), loss='mean_squared_error')
model.fit(X, y, epochs=10, batch_size=32, verbose=1)
print("Model training completed.")

# Ensure the models directory exists
model_path = 'models/lstm_energy_model'
os.makedirs(model_path, exist_ok=True)
if not os.access(model_path, os.W_OK):
    raise PermissionError("No write permissions for the 'models/lstm_energy_model' directory.")
print("Models directory created and writable.")

# Remove any existing model directory
if os.path.exists(model_path) and os.path.isdir(model_path):
    shutil.rmtree(model_path)
    print(f"Removed existing directory at {model_path}")
os.makedirs(model_path, exist_ok=True)

# Save the trained model in SavedModel format
model.save(model_path, save_format='tf')
print(f"βœ… LSTM model trained and saved as SavedModel at {model_path}")