gpt4 book ai didi

python - 喀拉斯 LSTM : Error when checking model input dimension

转载 作者:太空宇宙 更新时间:2023-11-03 15:56:49 24 4
gpt4 key购买 nike

我是 keras 的新用户,正在尝试实现 LSTM 模型。为了进行测试,我声明了如下模型,但由于输入维度不同而失败。虽然我在这个站点发现了类似的问题,但我自己找不到我的错误。

ValueError: 
Error when checking model input:
expected lstm_input_4 to have 3 dimensions, but got array with shape (300, 100)

我的环境

  • python 3.5.2
  • keras 1.2.0 (Theano)

代码

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)

编辑 1(不工作,在 Marcin Możejko 的评论之后)

谢谢 Marcin Możejko。但是我有类似下面的错误。我更新了虚拟数据以供检查。这段代码有什么问题?

ValueError: Error when checking model target: expected timedistributed_36 to have 3 dimensions, but got array with shape (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)

最佳答案

这是 KerasLSTM 的一个非常经典的问题。 LSTM 输入形状应为 2d - 形状为 (sequence_length, nb_of_features)。额外的第三个维度来自示例维度 - 因此提供给模型的表具有形状 (nb_of_examples, sequence_length, nb_of_features)。这就是您的问题所在。请记住,1-d 序列应表示为形状为 (sequence_length, 1)2-d 数组。这应该是您的 LSTM 的输入形状:

model.add(LSTM(nb_hidden, 
name='lstm',
activation='tanh',
return_sequences=True,
input_shape=(in_size, 1)))

并记住将您的输入 reshape 为适当的格式。

关于python - 喀拉斯 LSTM : Error when checking model input dimension,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42744903/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com