【发布时间】:2020-06-09 09:21:59
【问题描述】:
我的任务是使用 LSTM 从 3 个传感器预测房间占用率 (1,2)。有关此数据的示例,请参见下图:
import pandas as pd
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from sklearn.model_selection import train_test_split
import numpy as np
import matplotlib.pyplot as plt
x = np.array([
[31, 3, 5],
[32, 3, 5],
[29, 0, 3],
[31, 3, 4],
[23, 2, 4],
[22, 2, 4],
[23, 1, 4], ])
y = np.array([
[2],
[2],
[1],
[2],
[1],
[1],
[1], ])
x = x.reshape(7, 3, 1)
x_train,x_test,y_train,y_test = train_test_split(x, y,test_size =0.2, random_state = 4)
model=Sequential()
model.add(LSTM((1), activation='softmax', input_shape=x_train.shape,return_sequences=False))
我在这里遇到了一个错误:
----------------------------------- ---------------------------- ValueError Traceback(最近一次调用 最后)在 ----> 1 model.add(LSTM((1), activation='softmax', input_shape=x_train.shape, return_sequences=False))
~/opt/anaconda3/lib/python3.7/site-packages/keras/engine/sequential.py 在添加(自我,层) 163 # 并创建连接当前层的节点 164 # 到我们刚刚创建的输入层。 --> 165 层(x) 第166章 167 其他:
~/opt/anaconda3/lib/python3.7/site-packages/keras/layers/recurrent.py 在调用中(self、inputs、initial_state、constants、**kwargs) 530 531 如果 initial_state 为 None 并且 constants 为 None: --> 532 return super(RNN, self).call(inputs, **kwargs) 533 534 # 如果
initial_state或constants中的任何一个被指定并且是Keras~/opt/anaconda3/lib/python3.7/site-packages/keras/engine/base_layer.py 在调用(自我、输入、**kwargs) 412 # 在输入不兼容的情况下引发异常 413 # 使用层构造函数中指定的 input_spec。 --> 414 self.assert_input_compatibility(输入) 415 416 # 收集输入形状来构建图层。
~/opt/anaconda3/lib/python3.7/site-packages/keras/engine/base_layer.py 在 assert_input_compatibility(自我,输入) 309 self.name + ': 预期的 ndim=' + 310 str(spec.ndim)+',找到ndim='+ --> 311 str(K.ndim(x))) 312 如果 spec.max_ndim 不是无: 第313章
ValueError:输入 0 与层 lstm_1 不兼容:预期 ndim=3,发现 ndim=4
然后由于错误,我无法运行以下几行:
model.compile(loss='binary_crossentropy', optimizer='adam',metrics=['accuracy'])
model.summary()
history = model.fit(x_train, y_train, epochs=20, validation_data=(x_test, y_test))
谁能帮我找出问题所在?所有数据都是转换成整数的类别,这样创建模型合理吗?
【问题讨论】:
标签: python keras lstm categorical-data