Keras编码器解码器应具有2个尺寸

时间:2018-12-10 03:45:32

标签: python numpy keras neural-network lstm

当输入的形状似乎不兼容时,Keras编码器解码器将返回InvalidArgumentError。

我有:

  • X_numerical.shape将(304、2500、4)作为输入数据
  • y_numerical.shape提供(304、40、22)作为输出数据

Keras编解码器如下:

# Define an input sequence and process it. 
encoder_inputs = Input(shape=(None, 4)) 
encoder = LSTM(32, return_state=True) 
encoder_outputs, state_h, state_c = encoder(encoder_inputs)

# We discard `encoder_outputs` and only keep the states. 
encoder_states = [state_h, state_c]

# Set up the decoder, using `encoder_states` as initial state. 
decoder_inputs = Input(shape=(None, 22))
# We set up our decoder to return full output sequences,
# and to return internal states as well. We don't use the
# return states in the training model, but we will use them in inference. 
decoder_lstm = LSTM(32, return_sequences=True, return_state=True) 
decoder_outputs, _, _ = decoder_lstm(decoder_inputs,
                                     initial_state=encoder_states) 
decoder_dense = Dense(22, activation='softmax')
decoder_outputs = decoder_dense(decoder_outputs)

# Define the model that will turn
# `encoder_input_data` & `decoder_input_data` into `decoder_target_data` 
model = Model([encoder_inputs, decoder_inputs], decoder_outputs)

# Run training 
model.compile(optimizer='rmsprop', loss='categorical_crossentropy') 

### THE ERROR OCCURS IN THE `.fit()` CALL
model.fit([X_numerical, y_numerical], y_numerical,
          batch_size=4,
          epochs=1)

然后出现以下错误:

  

InvalidArgumentError跟踪(最近的调用)   最后)在()        25 model.fit([X_numerical,y_numerical],y_numerical,        26 batch_size = 4,   ---> 27个时代= 1)

     

InvalidArgumentError:不兼容的形状:[4,40,22]与[1,22,1]
  [[节点:   training_6 / RMSprop / gradients / dense_15 / add_grad / BroadcastGradientArgs =   BroadcastGradientArgs [T = DT_INT32,   _class = [“ loc:@ training_6 / RMSprop / gradients / dense_15 / add_grad / Sum”],_ device =“ / job:localhost /副本:0 / task:0 / device:CPU:0”]](training_6 / RMSprop / gradients / dense_15 / add_grad / Shape,   training_6 / RMSprop / gradients / dense_15 / add_grad / Shape_1)]]

我试图做的是将y_numerical重塑为(304,22,40),但是没有用。我还尝试了y_numerical .squeeze()并在model.fit()调用中更改了batch_size,它们都返回了各种错误。

此尺寸错误可能是什么原因?

我的模型摘要是:

__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_37 (InputLayer)           (None, None, 4)      0                                            
__________________________________________________________________________________________________
input_38 (InputLayer)           (None, None, 22)     0                                            
__________________________________________________________________________________________________
lstm_39 (LSTM)                  [(None, 32), (None,  4736        input_37[0][0]                   
__________________________________________________________________________________________________
lstm_40 (LSTM)                  [(None, None, 32), ( 7040        input_38[0][0]                   
                                                                 lstm_39[0][1]                    
                                                                 lstm_39[0][2]                    
__________________________________________________________________________________________________
dense_19 (Dense)                (None, None, 22)     726         lstm_40[0][0]                    
==================================================================================================
Total params: 12,502
Trainable params: 12,502
Non-trainable params: 0
__________________________________________________________________________________________________

0 个答案:

没有答案