gpt4 book ai didi

python-3.x - keras LSTM模型输入和输出维度不匹配

转载 作者:行者123 更新时间:2023-11-30 09:09:05 28 4
gpt4 key购买 nike

model = Sequential()

model.add(Embedding(630, 210))
model.add(LSTM(1024, dropout = 0.2, return_sequences = True))
model.add(LSTM(1024, dropout = 0.2, return_sequences = True))
model.add(Dense(210, activation = 'softmax'))

model.compile(loss = 'categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy'])

filepath = 'ner_2-{epoch:02d}-{loss:.5f}.hdf5'
checkpoint = ModelCheckpoint(filepath, monitor = 'loss', verbose = 1, save_best_only = True, mode = 'min')
callback_list = [checkpoint]

model.fit(X, y , epochs = 20, batch_size = 1024, callbacks = callback_list)

X:输入向量的形状为 (204564, 630, 1)

y:目标向量的形状为 (204564, 210, 1)

即对于每 630 个输入,必须预测 210 个输出,但代码在编译时会抛出以下错误

ValueError                                Traceback (most recent call last)
<ipython-input-57-05a6affb6217> in <module>()
50 callback_list = [checkpoint]
51
---> 52 model.fit(X, y , epochs = 20, batch_size = 1024, callbacks = callback_list)
53 print('successful')



ValueError: Error when checking model input: expected embedding_8_input to have 2 dimensions, but got array with shape (204564, 630, 1)

请有人解释一下为什么会出现此错误以及如何解决此问题

最佳答案

消息内容如下:

Your first layer expects an input with 2 dimensions: (BatchSize, SomeOtherDimension). But your input has 3 dimensions (BatchSize=204564,SomeOtherDimension=630, 1).

好吧...从您的输入中删除 1,或在模型内重新调整它的形状:

解决方案 1 - 将其从输入中删除:

X = X.reshape((204564,630))

解决方案 2 - 添加 reshape 图层:

model = Sequential()
model.add(Reshape((630,),input_shape=(630,1)))
model.add(Embedding.....)

关于python-3.x - keras LSTM模型输入和输出维度不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44946588/

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