简体   繁体   English

InvalidArgumentError:与Keras LSTM Net不兼容的形状

[英]InvalidArgumentError: Incompatible shapes with Keras LSTM Net

I want to predict the pressure of a machine. 我想预测一台机器的压力。 I have 18 input values and the pressure as output. 我有18个输入值,压力作为输出。 So I have 19 columns and 7657 rows as the database consists of 7657 time steps and each counts for 1 sec. 因此,我有19列和7657行,因为该数据库包含7657个时间步长,每个时间步长为1秒。

I have a problem with the following code: 我的以下代码有问题:

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)

I Get the Error: 我得到错误:

ValueError: Error when checking input: expected lstm_1_input to have 3 dimensions, but got array with shape (1257, 18) ValueError:检查输入时出错:预期lstm_1_input具有3个维,但数组的形状为(1257,18)

After the last line of code. 最后一行代码之后。

I also tried to reshape the test data but then I get a very similar error. 我也尝试过重塑测试数据,但是后来我得到了一个非常相似的错误。

I think, I'm missing something very easy or basic but I can't figure it out at the moment, as I'm just a beginner in coding neuronal networks. 我认为,我缺少一些非常简单或基本的东西,但是由于我只是编码神经元网络的初学者,因此目前无法解决。 I need this for my master thesis so I would be very thank full if anyone could help me out. 我的硕士论文需要这个,所以如果有人可以帮助我,我将非常感谢。

The problem is that your model input batch_input_shape is fixed. 问题是您的模型输入batch_input_shape是固定的。 The length of your test length is 1257 and cannot be divisible by 32. It should be changed as follows: 测试长度的长度为1257,不能被32整除。应按以下步骤进行更改:

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

You should modify test shape before the model evaluate test. 您应该在模型评估测试之前修改测试形状。

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)

Of course, you have to reshape predicted and y_test before inverse_transform . 当然,您必须在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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 不兼容的形状:Tensorflow/Keras Sequential LSTM with Autoencoder - Incompatible Shapes: Tensorflow/Keras Sequential LSTM with Autoencoder InvalidArgumentError:不兼容的形状:[32] 与 [8] - Keras 迁移学习 - InvalidArgumentError: Incompatible shapes: [32] vs. [8] - Keras Transfer Learning 在 Keras 中训练变分自动编码器引发“InvalidArgumentError:不兼容的形状”错误 - Training Variational Auto Encoder in Keras raises “InvalidArgumentError: Incompatible shapes” error Keras中带有RNN / LSTM的InvalidArgumentError - InvalidArgumentError with RNN/LSTM in Keras InvalidArgumentError:不兼容的形状:[3] 与 [4] - InvalidArgumentError: Incompatible shapes: [3] vs. [4] keras 分割 InvalidArgumentError:不兼容的形状:[32,256,256,3] 与 [32,256,256,4] - keras segmentation InvalidArgumentError: Incompatible shapes: [32,256,256,3] vs. [32,256,256,4] Keras:InvalidArgumentError:不兼容的形状:尝试使用 SVM 分类器添加 ResNet 时 [64,7,7,1] 与 [64,1] - Keras: InvalidArgumentError: Incompatible shapes: [64,7,7,1] vs. [64,1] when trying to add ResNet with SVM classifier Keras InvalidArgumentError:不兼容的形状:[1,8,32]与[1,10,32] - Keras InvalidArgumentError: Incompatible shapes: [1,8,32] vs. [1,10,32] Tensorflow + Keras训练:InvalidArgumentError:不兼容的形状:[7,128,2,2] vs [7,128,3,3] - Tensorflow + Keras training: InvalidArgumentError: Incompatible shapes: [7,128,2,2] vs [7,128,3,3] Keras 不兼容的形状 - Keras incompatible shapes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM