Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python InvalidArgumentError:与Keras LSTM网络不兼容的形状_Python_Keras_Lstm_Reshape_Shapes - Fatal编程技术网

Python InvalidArgumentError:与Keras LSTM网络不兼容的形状

Python InvalidArgumentError:与Keras LSTM网络不兼容的形状,python,keras,lstm,reshape,shapes,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.mod

我想预测机器的压力。我有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_输入有3个 维度,但获得了具有形状的数组(1257,18)

在最后一行代码之后

我还试图重新塑造测试数据,但后来我得到了一个非常类似的错误

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

问题是您的模型输入
批处理输入\u形状
已修复。测试长度为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)
当然,在
逆变换
之前,您必须重塑
预测
y\u测试

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)

你忘了重塑
x\u测试
就像
x\u火车
。嗨,我已经做过了。当add:x_test=x_test.reformate(1257,1,18)y_test=y_test.reformate(1257,1,1)最终我得到了错误:invalidargumeror:uncompatible shapes:[9,10]vs.[32,10][[Node:lstm_1/while/add_5=add[T=DT_FLOAT,\u device=“/job:localhost/replica:0/task:0/device:CPU:0”](lstm_1/while/biasdd_2,m_1/while/matu 6)]]对于那些遇到类似错误的人,也可以检查此链接-更改使用的度量有助于解决问题。您好,非常感谢,现在我的代码正常工作了。你能告诉我为什么批量定在32吗?我知道我在模型中输入了32,但是我认为我需要这样做,这样我的代码就可以给我除了错误之外的任何输出。总的来说,我不理解配料、成型或重塑的原理。你有什么建议可以让我对这个特定的主题有更多的了解吗?再次非常感谢你。如果我写这篇文章作为回答而不是评论,会更好吗?(我不熟悉这一页:D)@D.Luipers欢迎光临。如果你的文字不能直接回答问题,你最好写文字作为评论。通常认为批量大小在DL中并不重要。因此,您可以随意设置该值。是否使用整形或重塑取决于您的模型输入和使用情况。简言之,你需要多练习。最后,如果解决了您的问题,请不要忘记接受已接受的答案。很抱歉再次打扰您,但我认为当您使用LSTM模型时,我在理解输入形状和数据形状方面遇到了问题。我知道我的输入数据形状应该是=(行数、每行中的值、列数)。我读到你也可以包含回望的数量,但是如果我想把它放进去,我会得到一个错误。你有什么建议吗?多谢各位much@D.Luipers您需要阅读一些keras文档()和示例(等等)。