Keras LSTM使用批处理输出不正确的形状

时间:2018-08-06 08:00:00

标签: keras lstm recurrent-neural-network

我目前正在尝试实施LSTM网络。 网络本身如下所示

self.time_steps = 1
self.num_actions = 6
self.lstm_units = 64
input = Input(shape=(self.time_steps, self.num_actions))
h = Input(shape=(1, 64))
c = Input(shape=(1, 64))
LSTM_layer = LSTM(self.lstm_units, return_sequences=False, return_state=True)
lstm_output, out_h, out_c = LSTM_layer(input, initial_state=[h, c])
logit = Dense(self.num_actions, name="logit")(lstm_output)
output = Activation('softmax')(logit)

self.model = Model(inputs=[input, h, c], outputs=[output, out_h, out_c])

当我尝试使用

预测网络的结果时
input = np.zeros((16, self.time_steps, self.num_actions))
h = np.zeros((16, 1, self.lstm_units))
c = np.zeros((16, 1, self.lstm_units))
policy, h, c = self.model.predict([input, h, c])

出现错误

ValueError: could not broadcast input array from shape (256,6) into shape (16,6)

但是,如果我将批次大小更改为1(因此将所有16的外观都替换为1),则网络可以给出正确的输出。有人知道问题出在哪吗?
似乎错误的形状始终是bat​​ch ^ 2,所以如果我将其设置为12而不是16,它会抱怨(144,6)(这就是为什么我认为1起作用的原因,因为1 ^ 2 = 1 ... )

编辑:添加了有关错误的更多信息

 File "D:\Projects\file\file.py", line 123, in generate_network
    policy, h, c = self.model.predict([input, h, c])
  File "C:\Users\user\AppData\Local\Continuum\anaconda3\envs\tensorflow\lib\site-packages\keras\engine\training.py", line 1167, in predict
    steps=steps)
  File "C:\Users\user\AppData\Local\Continuum\anaconda3\envs\tensorflow\lib\site-packages\keras\engine\training_arrays.py", line 302, in predict_loop
    outs[i][batch_start:batch_end] = batch_out
ValueError: could not broadcast input array from shape (256,6) into shape (16,6)

1 个答案:

答案 0 :(得分:0)

好的,所以我尝试了您的代码,并且当我使用此处给出的代码时也出现了同样的错误,这意味着您试图在不进行模型训练的情况下直接进行预测。如果我错了,请纠正我! 首先,您需要编译模型并适合数据以进行训练。只有这样,您才能预测一些数据。像这样:

    model.compile(loss='categorical_crossentropy', optimizer=RMSprop(lr=0.001),metrics=['accuracy'])
    model.summary()
    model.fit(features, np.array(label), validation_split = 0.5, epochs=20, batch_size=64, class_weight=class_weights_dict)