4

我不确定如何使用 GridSearchCV 来优化 LSTM 模型。我浏览了本教程“ https://machinelearningmastery.com/grid-search-hyperparameters-deep-learning-models-python-keras/ ”;但是,他们分开做事。我也不确定他们是如何获得结果的,因为他们似乎没有在教程中比较测试和训练。该模型正在运行附加图像),但我正在努力使其变得更好。在我的代码中显示“网格搜索”的地方是我迷失了如何继续的地方。欢迎任何帮助或提示。

# Importing the libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

# Importing the training set
dataset_train = pd.read_csv('IBM_Train.csv')
training_set = dataset_train.iloc[:, 1:2].values

# Feature Scaling
from sklearn.preprocessing import MinMaxScaler
sc = MinMaxScaler(feature_range = (0, 1))
training_set_scaled = sc.fit_transform(training_set)

# Creating a data structure with 60 timesteps and 1 output
X_train = []
y_train = []
for i in range(60, 1228):
    X_train.append(training_set_scaled[i-60:i, 0])
    y_train.append(training_set_scaled[i, 0])
X_train, y_train = np.array(X_train), np.array(y_train)

# Reshaping
X_train = np.reshape(X_train, (X_train.shape[0], X_train.shape[1], 1))

# Part 2 - Building the RNN

# Importing the Keras libraries and packages
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from keras.layers import Dropout
from keras.wrappers.scikit_learn import KerasRegressor

# Initialising the RNN
def lstm_model():
    regressor = Sequential()
    regressor.add(LSTM(units=50, return_sequences=True, input_shape=(X_train.shape[1], 1)))
    regressor.add(Dropout(0.2))
    regressor.add(LSTM(units=50, return_sequences=True))
    regressor.add(Dropout(0.2))
    regressor.add(LSTM(units=50))
    regressor.add(Dropout(0.2))
    regressor.add(Dense(units=1))
    regressor.compile(optimizer='adam', loss='mean_squared_error')
    return regressor

regressor = KerasRegressor(build_fn=lstm_model, epochs=100, batch_size=16, verbose=0)

regressor.fit(X_train, y_train)

# Grid Search
from sklearn.model_selection import GridSearchCV
units = [30, 50, 70, 90]
dropout_rate = [0.0, 0.1, 0.2, 0.3, 0.4, 0.5]
activation = ['relu', 'tanh', 'sigmoid', 'hard_sigmoid']
batch_size = [8, 16, 32, 48]
epochs = [50, 100, 150, 200, 250]
optimizer = ['Adagrad', 'Adadelta', 'Adam', 'Adamax', 'Nadam']
param_grid = dict(units=units, dropout_rate=dropout_rate, activation=activation,
                  batch_size=batch_size, optimizer=optimizer, epochs=epochs)


# Part 3 - Making the predictions and visualising the results

# Getting the real stock price of 2017
dataset_test = pd.read_csv('IBM_Test.csv')
real_stock_price = dataset_test.iloc[:, 1:2].values

# Getting the predicted stock price of 2019
dataset_total = pd.concat((dataset_train['open'], dataset_test['open']), axis = 0)
inputs = dataset_total[len(dataset_total) - len(dataset_test) - 60:].values
inputs = inputs.reshape(-1,1)
inputs = sc.transform(inputs)
X_test = []
for i in range(60, 90):
    X_test.append(inputs[i-60:i, 0])
X_test = np.array(X_test)
X_test = np.reshape(X_test, (X_test.shape[0], X_test.shape[1], 1))
predicted_stock_price = regressor.predict(X_test)
predicted_stock_price = sc.inverse_transform(predicted_stock_price.reshape(-1, 1))

# Visualising the results
plt.plot(real_stock_price, color = 'red', label = 'Real IBM Stock Price')
plt.plot(predicted_stock_price, color = 'blue', label = 'Predicted IBM Stock Price')
plt.title('IBM Stock Price Prediction')
plt.xlabel('Days')
plt.ylabel('IBM Stock Price')
plt.legend()
plt.show()

[股票预测][1]

4

0 回答 0