Please understand that I used a translator as I am not good at English.
I’m working on predicting room temperature using LSTM. The input is (5, 5) data such as indoor temperature, outdoor temperature, etc., and the output is the 5 indoor temperatures to be followed.
Looking at the results, I would like to improve this as it tends to start from the last room temperature and only predict similar temperatures.
def create_sequences(data, n_steps):
X, y = [], []
for i in range(0, len(data) - (n_steps * 2), n_steps):
X.append(data[i: i + n_steps, :5])
y.append(data[i + n_steps: i + (n_steps * 2), 0])
return np.array(X), np.array(y)
# scaler_input = MinMaxScaler()
# scaler_output = MinMaxScaler()
scaler_input = joblib.load('./input_scaler.pkl')
scaler_output = joblib.load('./output_scaler.pkl')
# scaler_input.fit(sample.iloc[:, :5])
# scaler_output.fit(sample.iloc[:, 0:1])
# joblib.dump(scaler_input, 'input_scaler.pkl')
# joblib.dump(scaler_output, 'output_scaler.pkl')
n_steps = 5
first_station_data = sample_dic[list(sample_dic.keys())[0]]
df_scaled_input = scaler_input.transform(first_station_data.iloc[:, :5])
df_scaled_output = scaler_output.transform(first_station_data.iloc[:, 0:1])
X, y = create_sequences(np.hstack((df_scaled_input, df_scaled_output)), n_steps)
for idx in range(1, len(sample_dic.keys())):
station_data = pd.DataFrame(sample_dic[list(sample_dic.keys())[idx]])
df_scaled_input = scaler_input.transform(station_data.iloc[:, :5])
df_scaled_output = scaler_output.transform(station_data.iloc[:, 0:1])
X_new, y_new = create_sequences(np.hstack((df_scaled_input, df_scaled_output)), n_steps)
X = np.concatenate((X, X_new))
y = np.concatenate((y, y_new))
# 데이터 길이
data_len = len(X)
train_size = int(data_len * 0.7)
val_size = int(data_len * 0.2)
test_size = data_len - train_size - val_size
X_train, y_train = X[:train_size], y[:train_size]
X_val, y_val = X[train_size:train_size + val_size], y[train_size:train_size + val_size]
X_test, y_test = X[train_size + val_size:], y[train_size + val_size:]
with tf.device("/gpu:0"):
def build_model(hp):
model = Sequential()
model.add(Bidirectional(LSTM(
units=hp.Int('input_unit', min_value=32, max_value=512, step=32),
input_shape=(n_steps, X_train.shape[2]),
return_sequences=True
)))
for i in range(hp.Int('n_layers', 0, 5)):
model.add(Bidirectional(LSTM(hp.Int(f'lstm_{i}_units', min_value=32, max_value=512, step=32), return_sequences=True)))
model.add(Bidirectional(LSTM(hp.Int('layer_2_neurons', min_value=32, max_value=512, step=32))))
model.add(Dropout(hp.Float('Dropout_rate', min_value=0, max_value=0.5, step=0.1)))
model.add(Dense(y_train.shape[1], activation=hp.Choice('dense_activation', values=['relu', 'sigmoid'], default='relu')))
model.compile(
optimizer=hp.Choice('optimizer', ['adam']),
loss='mean_squared_error',
metrics=['mse']
)
return model
hp = HyperParameters()
tuner = RandomSearch(
build_model,
objective='val_mse',
max_trials=30,
executions_per_trial=2,
directory='tuning',
project_name='lstm_temp_prediction_test16',
hyperparameters=hp
)
early_stopping = EarlyStopping(monitor='val_loss', patience=2, restore_best_weights=True)
lr_scheduler = tf.keras.callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.5, patience=1)
tuner.search(
X_train, y_train,
epochs=100,
batch_size=128,
validation_data=(X_val, y_val),
callbacks=[early_stopping, lr_scheduler]
)
best_model = tuner.get_best_models(num_models=1)[0]
best_hyperparameters = tuner.get_best_hyperparameters()[0]
print("Best hyperparameters:", best_hyperparameters.values)
best_model.save('./lstm_temp_model2.h5')
best_model.save_weights('./temp_model_w2.pkl')
Model: “sequential”
Layer (type) Output Shape Param #
lstm (LSTM) (None, 5, 480) 933120
lstm_1 (LSTM) (None, 5, 384) 1328640
lstm_2 (LSTM) (None, 5, 128) 262656
lstm_3 (LSTM) (None, 5, 448) 1033984
lstm_4 (LSTM) (None, 256) 721920
dropout (Dropout) (None, 256) 0
dense (Dense) (None, 5) 1285
=================================================================
Total params: 4,281,605
Trainable params: 4,281,605
Non-trainable params: 0
black : actual
red : predict
How can i improve?
I’m trying to learn again by adding a layer or using Bidirectional.