【问题标题】:Cannot feed value on Stateful LSTM keras无法在有状态 LSTM keras 上提供价值
【发布时间】:2018-06-14 20:53:20
【问题描述】:

代码有什么问题?

它返回ValueError: Cannot feed value of shape (2, 5, 7) for Tensor u'lstm_1_input:0', which has shape '(5, 5, 7)' 错误。

我的代码:

model = Sequential()
model.add(LSTM(128,batch_input_shape=(5,5,7),return_sequences=True,stateful=True))
model.add(Activation('relu'))
model.add(Dropout(0.2))
model.add(LSTM(32))
model.add(Activation('relu'))
model.add(Dense(2))
model.add(Activation('softmax'))
checkpoint = ModelCheckpoint('./model28-{epoch:02d}.h5')
model.compile(optimizer='adam',loss='binary_crossentropy',metrics=['accuracy'])
print(model.summary())
model.fit(trainX,trainY,batch_size=5,epochs=20,validation_split=0.2,callbacks=[checkpoint],shuffle=False)

【问题讨论】:

  • 你的 trainX 是什么样的?错误是说你说输入形状是 (5,5,7) 但你试图给它一个它不能接受的 (2,5,7) 张量。
  • 我的 trainX 形状是(34365, 5, 7)
  • 你能不能不把输入形状改成(2,5,7)?
  • @MennoVanDijk 34365 不能被 2 分割

标签: python tensorflow keras lstm


【解决方案1】:

我的trainX 形状为 34365。我删除了最后一行 5 以具有 34360 的形状(形状应除以 batch_size)。 每个epochreset_states() 都应该被调用,所以我将代码更改为:

epo_num= 20
model = Sequential()
model.add(LSTM(128,batch_input_shape=(2,5,7),return_sequences=False,stateful=True))
model.add(Activation('relu'))
model.add(Dropout(0.2))
model.add(LSTM(32))
model.add(Activation('relu'))
model.add(Dense(2))
model.add(Activation('softmax'))
#checkpoint = ModelCheckpoint('./model30-{epoch:02d}.h5')
model.compile(optimizer='adam',loss='binary_crossentropy',metrics=['accuracy'])
print(model.summary())
for i in range(epo_num):
    print('Epochs {:d}/{:d}'.format(i+1,epo_num))
    model.fit(trainX,trainY,batch_size=2,epochs=1,validation_split=0.2,shuffle=False)
    model.reset_states()

【讨论】:

    猜你喜欢
    • 2018-03-30
    • 2017-08-31
    • 2018-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-08
    • 2017-08-06
    相关资源
    最近更新 更多