Keras LSTM网络,形状错误:InvalidArgumentError:形状不兼容:[6853]与[89]

时间:2018-07-29 09:05:22

标签: python tensorflow keras lstm

我正在尝试使用Keras和TensorFlow安装一个简单的LSTM网络。尝试拟合时,我不断收到以下错误或其他错误。我试过了89和1的批处理大小,并且都返回相同的错误,只是显示的形状有冲突而不同。

我的Xtrain的形状是:

In [9]: print(Xtrain.shape)
(6853, 89, 250)

In [10]: print(ytrain.shape)
(6853, 89, 1)

    In [11]: model = Sequential()^M
   ...: model.add(LSTM(units=89,^M
   ...:     batch_input_shape=(Xtrain.shape[0],timesteps,len(features)),^M
   ...:     activation='relu',^M
   ...:     return_sequences=True,^M
   ...:     stateful=True))^M
   ...:     ^M
   ...: model.add(Dropout(0.2))^M
   ...: ^M
   ...: model.add(Dense(21))^M
   ...:     ^M
   ...: model.add(Dropout(0.2))^M
   ...: ^M
   ...: model.add(TimeDistributed(Dense(1)))^M
   ...: #model.add(Dense(1))^M
   ...: model.compile(loss='mean_squared_error', optimizer=opt, metrics=['mae'])^M
   ...: model.summary()^M
   ...: model.fit(Xtrain,ytrain,batch_size=89,verbose=1,shuffle=False,epochs=1)
   ...:

_________________________________________________________________
Layer (type)                 Output Shape              Param #
=================================================================
lstm_5 (LSTM)                (6853, 89, 89)            121040
_________________________________________________________________
dropout_9 (Dropout)          (6853, 89, 89)            0
_________________________________________________________________
dense_9 (Dense)              (6853, 89, 21)            1890
_________________________________________________________________
dropout_10 (Dropout)         (6853, 89, 21)            0
_________________________________________________________________
time_distributed_5 (TimeDist (6853, 89, 1)             22
=================================================================
Total params: 122,952
Trainable params: 122,952
Non-trainable params: 0
_________________________________________________________________
Epoch 1/1
---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-9-9ecc3d11a1f7> in <module>()
     16 model.compile(loss='mean_squared_error', optimizer=opt, metrics=['mae'])
     17 model.summary()
---> 18 model.fit(Xtrain,ytrain,batch_size=89,verbose=1,shuffle=False,epochs=1)

C:\Users\asus\Anaconda3\lib\site-packages\keras\engine\training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_dat
   1035                                         initial_epoch=initial_epoch,
   1036                                         steps_per_epoch=steps_per_epoch,
-> 1037                                         validation_steps=validation_steps)
   1038
   1039     def evaluate(self, x=None, y=None,

C:\Users\asus\Anaconda3\lib\site-packages\keras\engine\training_arrays.py in fit_loop(model, f, ins, out_labels, batch_size, epochs, verbose, callbacks, val_f
    197                     ins_batch[i] = ins_batch[i].toarray()
    198
--> 199                 outs = f(ins_batch)
    200                 outs = to_list(outs)
    201                 for l, o in zip(out_labels, outs):

C:\Users\asus\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py in __call__(self, inputs)
   2664                 return self._legacy_call(inputs)
   2665
-> 2666             return self._call(inputs)
   2667         else:
   2668             if py_any(is_tensor(x) for x in inputs):

C:\Users\asus\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py in _call(self, inputs)
   2634                                 symbol_vals,
   2635                                 session)
-> 2636         fetched = self._callable_fn(*array_vals)
   2637         return fetched[:len(self.outputs)]
   2638

C:\Users\asus\Anaconda3\lib\site-packages\tensorflow\python\client\session.py in __call__(self, *args)
   1452         else:
   1453           return tf_session.TF_DeprecatedSessionRunCallable(
-> 1454               self._session._session, self._handle, args, status, None)
   1455
   1456     def __del__(self):

C:\Users\asus\Anaconda3\lib\site-packages\tensorflow\python\framework\errors_impl.py in __exit__(self, type_arg, value_arg, traceback_arg)
    517             None, None,
    518             compat.as_text(c_api.TF_Message(self.status.status)),
--> 519             c_api.TF_GetCode(self.status.status))
    520     # Delete the underlying status object from memory otherwise it stays alive
    521     # as there is a reference to status from this from the traceback due to

InvalidArgumentError: Incompatible shapes: [6853] vs. [89]
         [[Node: training_4/Adam/gradients/loss_4/time_distributed_5_loss/mul_grad/BroadcastGradientArgs = BroadcastGradientArgs[T=DT_INT32, _class=["loc:@tra

我已经检查了github和SO上的其他一些类似错误的问题,但所有问题都与自定义层或多个GPU上的并行处理有关,在我的情况下都不适用。

我收到的唯一有用的错误消息是batch_size必须为

我在这里做什么错了?

0 个答案:

没有答案