我是深度学习新手,我开始使用 GridSearchCV 实现 LSTM 的超参数调整。我的数据集包含 15551 行和 21 列,所有值都是 float 类型。这是我的代码:
# import libraries
import numpy as np
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
from sklearn.model_selection import TimeSeriesSplit, GridSearchCV, train_test_split
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, Dense
from tensorflow.keras.wrappers.scikit_learn import KerasRegressor
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings('ignore')
# Load and preprocess the dataset
data = pd.read_csv("dataset.csv") # Replace with the actual path
target_col = 'rn_val'
# Extract the target variable
target = data[target_col].values
#Variables for training
cols = list(data)[1:22]
# Normalize the data
scaler = MinMaxScaler()
data_normalized = scaler.fit_transform(data[cols].astype(float))
# Define the number of time steps and features
n_steps = 7 # Number of time steps to consider
n_features = data_normalized.shape[1]
# Splitting data into train (67%) and temp (33%)
X_train, data_temp, y_train, target_temp = train_test_split(data_normalized, target, test_size=0.33, random_state=42)
# Splitting temp into validation (17%) and test (16%)
X_val, X_test, y_val, y_test = train_test_split(data_temp, target_temp, test_size=0.48, random_state=42)
# Define the LSTM network architecture
def create_lstm_model(neurons, optimizer, activation):
model = Sequential()
model.add(LSTM(units=neurons, input_shape=(n_steps, n_features)))
model.add(Dense(1, activation=activation))
model.compile(loss='mse', optimizer=optimizer)
return model
# define the grid search parameters
param_grid = {'neurons': [8, 16, 32, 64, 128],
'optimizer': ['SGD', 'RMSprop', 'Adam'],
'activation':['relu', 'tanh', 'sigmoid', 'linear','swish']}
model = KerasRegressor(build_fn=create_lstm_model, epochs=10, batch_size=16, verbose=0)
grid = GridSearchCV(estimator=model, param_grid=param_grid,cv=3,refit=False, scoring="accuracy", n_jobs = -1)
grid_result = grid.fit(X_train, y_train)
# summarize results
print("Best accuracy of: %f using %s" % (grid_result.best_score_,
grid_result.best_params_))
means = grid_result.cv_results_['mean_test_score']
stds = grid_result.cv_results_['std_test_score']
params = grid_result.cv_results_['params']
for mean, stdev, param in zip(means, stds, params):
print("%f (%f) with: %r" % (mean, stdev, param))
但我得到的准确度值与 Nan 相同。这是我得到的输出,如屏幕截图所示。有人可以检查我的代码并纠正我可能犯的任何错误吗?