我是keras的新用户,并尝试实施LSTM模型。对于测试,我声明了如下所示的模型,但由于输入维度的不同而失败。虽然我在这个网站上发现了类似的问题,但我自己找不到自己的错误。
ValueError:
Error when checking model input:
expected lstm_input_4 to have 3 dimensions, but got array with shape (300, 100)
from keras.layers import Input, Dense from keras.models import Sequential from keras.layers import LSTM from keras.optimizers import RMSprop, Adadelta from keras.layers.wrappers import TimeDistributed import numpy as np in_size = 100 out_size = 10 nb_hidden = 8 model = Sequential() model.add(LSTM(nb_hidden, name='lstm', activation='tanh', return_sequences=True, input_shape=(None, in_size))) model.add(TimeDistributed(Dense(out_size, activation='softmax'))) adadelta = Adadelta(clipnorm=1.) model.compile(optimizer=adadelta, loss='categorical_crossentropy', metrics=['accuracy']) # create dummy data data_size = 300 train = np.zeros((data_size, in_size,), dtype=np.float32) labels = np.zeros((data_size, out_size,), dtype=np.float32) model.fit(train, labels)
谢谢MarcinMożejko。但我有类似的错误,如下所示。我更新了虚拟数据以供检查。这段代码出了什么问题?
ValueError:检查模型目标时出错:预期 timedistributed_36有3个维度,但得到了有形状的数组 (208,1)
def create_dataset(X, Y, loop_back=1):
dataX, dataY = [], []
for i in range(len(X) - loop_back-1):
a = X[i:(i+loop_back), :]
dataX.append(a)
dataY.append(Y[i+loop_back, :])
return np.array(dataX), np.array(dataY)
data_size = 300
dataset = np.zeros((data_size, feature_size), dtype=np.float32)
dataset_labels = np.zeros((data_size, 1), dtype=np.float32)
train_size = int(data_size * 0.7)
trainX = dataset[0:train_size, :]
trainY = dataset_labels[0:train_size, :]
testX = dataset[train_size:, :]
testY = dataset_labels[train_size:, 0]
trainX, trainY = create_dataset(trainX, trainY)
print(trainX.shape, trainY.shape) # (208, 1, 1) (208, 1)
# in_size = 100
feature_size = 1
out_size = 1
nb_hidden = 8
model = Sequential()
model.add(LSTM(nb_hidden,
name='lstm',
activation='tanh',
return_sequences=True,
input_shape=(1, feature_size)))
model.add(TimeDistributed(Dense(out_size, activation='softmax')))
adadelta = Adadelta(clipnorm=1.)
model.compile(optimizer=adadelta,
loss='categorical_crossentropy',
metrics=['accuracy'])
model.fit(trainX, trainY, nb_epoch=10, batch_size=1)
答案 0 :(得分:3)
这是LSTM
中Keras
的一个非常经典的问题。 LSTM
输入形状应为2d
- 形状为(sequence_length, nb_of_features)
。额外的第三个维度来自示例维度 - 因此提供给模型的表格具有形状(nb_of_examples, sequence_length, nb_of_features)
。这是您的问题所在。请注意,1-d
序列应显示为2-d
数组,其形状为(sequence_length, 1)
。这应该是LSTM
:
model.add(LSTM(nb_hidden,
name='lstm',
activation='tanh',
return_sequences=True,
input_shape=(in_size, 1)))
请记住reshape
输入适当的格式。