【发布时间】: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_test像x_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