python - 无效参数错误 : Incompatible shapes with Keras LSTM Net

标签 python keras lstm reshape shapes

我想预测一台机器的压力。我有 18 个输入值和压力作为输出。所以我有 19 列和 7657 行,因为数据库由 7657 个时间步长组成,每个时间步长为 1 秒。

我对以下代码有疑问:

import tensorflow as tf
import pandas as pd
from matplotlib import pyplot
from sklearn.preprocessing import MinMaxScaler
from sklearn import linear_model  
from keras.models import Sequential
from keras.layers import Dense #Standard neural network layer
from keras.layers import LSTM
from keras.layers import Activation 
from keras.layers import Dropout

df = pd.read_csv('Testdaten_2_Test.csv',delimiter=';')

feature_col_names=['LSDI','LZT1I', ..... ,'LZT5I']
predicted_class_names = ['LMDI']

x = df[feature_col_names].values
y = df[predicted_class_names].values

x_train_size = 6400
x_train, x_test = x[0:x_train_size], x[x_train_size:len(x)]

y_train_size = 6400
y_train, y_test = y[0:y_train_size], y[y_train_size:len(y)]

nb_model = linear_model.LinearRegression()
nb_model.fit(X=x_train, y=y_train)

nb_predict_train = nb_model.predict(x_test)

from sklearn import metrics

def scale(x, y):
    # fit scaler
    x_scaler = MinMaxScaler(feature_range=(-1, 1))
    x_scaler = x_scaler.fit(x)
    x_scaled = x_scaler.transform(x)

    # fit scaler
    y_scaler = MinMaxScaler(feature_range=(-1, 1))
    y_scaler = y_scaler.fit(y)
    y_scaled = y_scaler.transform(y)
    return x_scaler, y_scaler, x_scaled, y_scaled

x_scaler, y_scaler, x_scaled, y_scaled = scale(x, y)
x_train, x_test = x_scaled[0:x_train_size], x_scaled[x_train_size:len(x)]
y_train, y_test = y_scaled[0:y_train_size], y_scaled[y_train_size:len(y)]

x_train=x_train.reshape(x_train_size,1,18)
y_train=y_train.reshape(y_train_size,1,1)

model = Sequential()

model.add(LSTM(10, return_sequences=True,batch_input_shape=(32,1,18)))  
model.add(LSTM(10,return_sequences=True))  
model.add(LSTM(1,return_sequences=True, activation='linear'))

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

model.fit(x_train, y_train, epochs=10,batch_size=32)

score = model.evaluate(x_test, y_test,batch_size=32)

predicted = model.predict(x_test)
predicted = y_scaler.inverse_transform(predicted)
predicted = [x if x > 0 else 0 for x in predicted]

correct_values = y_scaler.inverse_transform(y_test)
correct_values = [x if x > 0 else 0 for x in correct_values]
print(nb_predict_train)

我得到错误:

ValueError: Error when checking input: expected lstm_1_input to have 3 dimensions, but got array with shape (1257, 18)

在最后一行代码之后。

我也尝试 reshape 测试数据,但后来我得到了一个非常相似的错误。

我想,我错过了一些非常简单或基本的东西,但我现在无法弄清楚,因为我只是编码神经元网络的初学者。 我的硕士论文需要这个,所以如果有人能帮助我,我将非常感谢。

最佳答案

问题是您的模型输入 batch_input_shape 是固定的。你的测试长度是1257,不能被32整除,应该改成如下:

model.add(LSTM(10, return_sequences=True,batch_input_shape=(None,1,18)))

您应该在模型评估测试之前修改测试形状。

x_test= x_test.reshape(len(x)-x_train_size,1,18)
y_test= y_test.reshape(len(y)-x_train_size,1,1)
score = model.evaluate(x_test, y_test,batch_size=32)

当然,在inverse_transform之前,你必须先reshape predictedy_test

predicted = model.predict(x_test)
predicted= predicted.reshape(len(y)-x_train_size,1)
y_test= y_test.reshape(len(y)-x_train_size,1)

关于python - 无效参数错误 : Incompatible shapes with Keras LSTM Net,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52254466/

相关文章:

python - 如何从 numpy 矩阵中随机选择项目(以矢量化方式)?

python - 在使用 Keras LSTM 拟合和预测数据期间,如何使用不同大小的训练数据和测试数据以及不同的 batch_sizes?

android - 如何在Android中运行Conv3D模型?

tensorflow - 让 LSTM 从 3 个变量的相关性中学习

python - 如何使用 Python 从 azure blob 读取 docx 文件

python - 使用 csv header 索引格式化值

python - 导入 pyplot 模块并调用 pyplot,plot() 但没有存储对任何变量的返回。但是调用 pyplot.show() 函数?

machine-learning - “predict_generator”返回大于 1 且小于 0 的值

python - Keras - 火车结束回调没有得到任何日志

keras - .fit() 层的 shuffle = 'batch' 参数如何在后台工作?