python RNN LSTM错误

Posted

技术标签:

【中文标题】python RNN LSTM错误【英文标题】:python RNN LSTM error 【发布时间】:2018-06-15 17:56:39 【问题描述】:

这是一个循环神经网络 LSTM 模型,旨在预测外汇市场走势的未来价值。

数据集形状为(1713, 50),第一列为日期时间索引,其他为数值。 但是在打印训练数据和验证数据之后,错误就开始了。

当我尝试实现这段代码时:

from sklearn.preprocessing import MinMaxScaler
from keras.layers.core import Dense, Activation, Dropout
from keras.layers.recurrent import LSTM
from keras.models import Sequential
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

df = pd.read_csv(r"E:\Business\Stocks\StocksDF.csv", parse_dates=[0], index_col=[0], low_memory=False, dtype='object')
features = len(df.columns)
val_ratio = 0.2
epochs = 500
batch_size = df.__len__()
sequence_length = 822

data = df.as_matrix()
data_processed = []
for index in range(len(data) - sequence_length):
    data_processed.append(data[index: index + sequence_length])
data_processed = np.array(data_processed)

val_split = round((1 - val_ratio) * data_processed.shape[0])
train = data_processed[:, int(val_split), :]
val = data_processed[int(val_split):, :]

print('Training data: '.format(train.shape))
print('Validation data: '.format(val.shape))



train_samples, train_nx, train_ny = train.shape
val_samples, val_nx, val_ny = val.shape

train = train.reshape((train_samples, train_nx * train_ny))
val = val.reshape((val_samples, val_nx * val_ny))

preprocessor = MinMaxScaler().fit(train)
train = preprocessor.transform(train)
val = preprocessor.transform(val)

train = train.reshape((train_samples, train_nx, train_ny))
val = val.reshape((val_samples, val_nx, val_ny))

X_train = train[:, : -1]
y_train = train[:, -1][:, -1]
X_val = val[:, : -1]
y_val = val[:, -1][:, -1]

X_train = np.reshape(X_train, (X_train.shape[0], X_train.shape[1], features))
X_val = np.reshape(X_val, (X_val.shape[0], X_val.shape[1], features))

model = Sequential()
model.add(LSTM(input_shape=(X_train.shape[1:]), units=100, return_sequences=True))
model.add(Dropout(0.5))
model.add(LSTM(2, return_sequences=False))
model.add(Dropout(0.25))
model.add(Dense(units=1))
model.add(Activation("relu"))

model.compile(loss='mean_squared_error', optimizer='adam', metrics=['mae', 'mse', 'accuracy'])

history = model.fit(
    X_train,
    y_train,
    batch_size=batch_size,
    epochs=epochs,
    verbose=2)

preds_val = model.predict(X_val)
diff = []
for i in range(len(y_val)):
    pred = preds_val[i][0]
    diff.append(y_val[i] - pred)

real_min = preprocessor.data_min_[104]
real_max = preprocessor.data_max_[104]
print(preprocessor.data_min_[:1])
print(preprocessor.data_max_[:1])

preds_real = preds_val * (real_max - real_min) + real_min
y_val_real = y_val * (real_max - real_min) + real_min

plt.plot(preds_real, label='Predictions')
plt.plot(y_val_real, label='Actual values')
plt.xlabel('test')
plt.legend(loc=0)
plt.show()
print(model.summary())

我收到了这个错误:

使用 TensorFlow 后端。

Traceback(最近一次调用最后一次):

训练数据:(891, 50)

文件“E:/Tutorial/new.py”,第 31 行,在 验证数据:(178, 822, 50)

train_samples, train_nx, train_ny = train.shape

ValueError: 没有足够的值来解包(预期 3,得到 2)

【问题讨论】:

如果我可以正确地看到问题开始之前,如果您可以看到训练数据有 2 个值,而验证数据有 3 个值。和错误一样 @roganjosh 任何建议 在一个猜测train_nx, train_ny = train.shape。但是我可以很好地处理返回的内容。关键是,错误消息在所有 python 中都是标准的,因此所有代码和背景故事都无关紧要,您唯一需要了解的是 train.shape 实际代表什么。 试试print(train.shape),看看它是什么样子的。 print(train.shape) 给了你同样的错误?我看不出这怎么可能。我并不是说print 可以解决问题,只是为了调试 【参考方案1】:

这一行有错误:

train = data_processed[:, int(val_split), :]

应该是:

train = data_processed[:int(val_split), :, :]
val = data_processed[int(val_split):, :, :]

【讨论】:

以上是关于python RNN LSTM错误的主要内容,如果未能解决你的问题,请参考以下文章

Python - 基于 LSTM 的 RNN 需要 3D 输入?

图像标注 python实现-LSTM篇

IMPLEMENTING A GRU/LSTM RNN WITH PYTHON AND THEANO - 学习笔记

文本分类 RNN - LSTM - 错误检查目标

循环神经网络LSTM RNN回归:sin曲线预测

循环神经网络LSTM RNN回归:sin曲线预测