这是我的RNN:
def make_cpu_regressor():
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, 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')
regressor.fit(X_train, y_train, epochs=100, batch_size=32)
regressor.save('model-cpu.h5')
return regressor
我创建了第二个,只有一个区别,我使用CuDNNLSTM
而不是LSTM
,其他所有内容都是相同的。使用CuDNNLSTM
的NN训练得更快,但是预测之间存在显着差异:
为什么预测有这样的差异?
当我将CuDNNLSTM
修改为150和200个历元(蓝线)时,效果会更好:
编辑: 这是CuDNNLSTM版本的代码:
def make_gpu_regressor():
regressor = Sequential()
regressor.add(CuDNNLSTM(units=50, return_sequences=True, input_shape=(X_train.shape[1], 1)))
regressor.add(Dropout(0.2))
regressor.add(CuDNNLSTM(units=50, return_sequences=True))
regressor.add(Dropout(0.2))
regressor.add(CuDNNLSTM(units=50, return_sequences=True))
regressor.add(Dropout(0.2))
regressor.add(CuDNNLSTM(units=50))
regressor.add(Dropout(0.2))
regressor.add(Dense(units=1))
regressor.compile(optimizer='adam', loss='mean_squared_error')
regressor.fit(x=X_train, y=y_train, epochs=100, batch_size=32)
regressor.save('model_gpu.h5')
return regressor
regressor_gpu = make_gpu_regressor()
regressor_cpu = make_cpu_regressor()
predicted_stock_price_gpu = regressor_gpu.predict(X_test)
predicted_stock_price_gpu = sc.inverse_transform(predicted_stock_price_gpu)
predicted_stock_price_cpu = regressor_cpu.predict(X_test)
predicted_stock_price_cpu = sc.inverse_transform(predicted_stock_price_cpu)