【问题标题】:InvalidArgumentError: Incompatible shapes with Keras LSTM NetInvalidArgumentError:与 Keras LSTM 网络不兼容的形状
【发布时间】:2019-02-14 16:36:24
【问题描述】:

我想预测机器的压力。我有 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:检查输入时出错:预期 lstm_1_input 有 3 尺寸,但得到形状为 (1257, 18) 的数组

在最后一行代码之后。

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

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

【问题讨论】:

  • 你忘了重塑 x_testx_train
  • 嗨,我已经这样做了。添加时:x_test=x_test.reshape(1257,1,18) y_test=y_test.reshape(1257,1,1) 最后我得到错误:InvalidArgumentError:不兼容的形状:[9,10] vs. [32, 10] [[节点:lstm_1/while/add_5 = 添加[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"](lstm_1/while/BiasAdd_2, lstm_1/ while/MatMul_6)]]
  • 遇到类似错误的人也可以查看此链接github.com/keras-team/keras/issues/11749 - 更改使用的指标可能有助于解决问题。

标签: python keras lstm reshape shapes


【解决方案1】:

问题是您的模型输入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之前重塑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)

【讨论】:

  • 嗨,非常感谢,现在我的代码可以工作了。你能告诉我为什么批量大小固定为 32 吗?我知道我将模型中的输入设置为 32,但我认为我需要这样做,以便我的代码为我提供除错误之外的任何输出。总的来说,我不明白批处理和整形或重塑的原理。你有什么建议可以让我获得更多关于这个特定主题的知识吗?再次非常感谢您。如果我将这段文字写成答案而不是评论会更好吗? (我是这个页面的新手:D)
  • @D.Luipers 欢迎来到这里。如果您的文字无法直接回答问题,您最好将文字写成评论。通常认为批量大小在 DL 中并不重要。所以你可以随意设置值。是使用整形还是重塑取决于您的模型输入和使用情况。简而言之,你需要多练习。最后,如果它解决了您的问题,请不要忘记接受答案。
  • 很抱歉再次打扰您,但我认为当您使用 LSTM 模型时,我在理解输入形状和数据形状时遇到了问题。我知道我的输入数据形状应该是=(行数,每行中的值,列数)。我读到您也可以包含look_backs 的数量,但如果我想加入它会出现错误。您有什么建议吗?非常感谢
  • @D.Luipers 您需要阅读一些 keras 文档(keras.io/layers/recurrent/#lstm)和示例(deeplearning.net/tutorial/lstm.htmlmachinelearningmastery.com/… 等)。
猜你喜欢
  • 1970-01-01
  • 2021-03-07
  • 2020-10-24
  • 2021-12-31
  • 2021-09-18
  • 2021-06-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多