检查输入时出错:预期 lstm_1_input 有 3 个维度,但得到形状为 (5, 3) 的数组
Error when checking input: expected lstm_1_input to have 3 dimensions, but got array with shape (5, 3)
我的任务是使用 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 (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))
谁能帮我找出问题所在?所有数据都是类别转换成整数,这样创建模型合理吗?
您需要像这样重塑您的输入:
x = x.reshape(x.shape[0], 1, x.shape[1]) # the 1 is the steps
并像这样指定输入形状:
input_shape=(x.shape[1:])
所以使用这些行,它将起作用:
x = x.reshape(7, 1, 3)
model.add(LSTM((1), activation='softmax', input_shape=x_train.shape[1:],
return_sequences=False))
最后,如果你只有两个类,你的激活函数应该是sigmoid
。
我的任务是使用 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 (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
orconstants
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))
谁能帮我找出问题所在?所有数据都是类别转换成整数,这样创建模型合理吗?
您需要像这样重塑您的输入:
x = x.reshape(x.shape[0], 1, x.shape[1]) # the 1 is the steps
并像这样指定输入形状:
input_shape=(x.shape[1:])
所以使用这些行,它将起作用:
x = x.reshape(7, 1, 3)
model.add(LSTM((1), activation='softmax', input_shape=x_train.shape[1:],
return_sequences=False))
最后,如果你只有两个类,你的激活函数应该是sigmoid
。