检查输入时出错:预期lstm_1_input具有3个维,但数组的形状为(5,3)

我的任务是使用LSTM从3个传感器预测房间入住率(1,2)。请参阅以下数据作为示例:

data

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 (most recent call last) in ----> 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 in add(self, layer) 163 # and create the node connecting the current layer 164 # to the input layer we just created. --> 165 layer(x) 166 set_inputs = True 167 else:

~/opt/anaconda3/lib/python3.7/site-packages/keras/layers/recurrent.py in call(self, inputs, initial_state, constants, **kwargs) 530 531 if initial_state is None and constants is None: --> 532 return super(RNN, self).call(inputs, **kwargs) 533 534 # If any of initial_state or constants are specified and are Keras

~/opt/anaconda3/lib/python3.7/site-packages/keras/engine/base_layer.py in call(self, inputs, **kwargs) 412 # Raise exceptions in case the input is not compatible 413 # with the input_spec specified in the layer constructor. --> 414 self.assert_input_compatibility(inputs) 415 416 # Collect input shapes to build layer.

~/opt/anaconda3/lib/python3.7/site-packages/keras/engine/base_layer.py in assert_input_compatibility(self, inputs) 309 self.name + ': expected ndim=' + 310 str(spec.ndim) + ', found ndim=' + --> 311 str(K.ndim(x))) 312 if spec.max_ndim is not None: 313 ndim = K.ndim(x)

ValueError: Input 0 is incompatible with layer lstm_1: expected ndim=3, found 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))
  1. 谁能帮助和帮助我确定问题所在?
  2. 所有数据都是类别转换为整数,以这种方式创建模型是否合理?

提前谢谢了, 莎玛