seq2seq输出长度可变

时间:2018-06-24 09:03:13

标签: python keras sequence-to-sequence

我为时间序列数据实现了Keras seq2seq模型,当我以相同的序列长度对其进行测试时,它运行良好。当我想使用target_seqs> input_seqs(length = 10000)进行推断时,会收到以下消息: 索引10000超出轴10000的大小10000

代码如下:

num_encoder_tokens = 1
num_decoder_tokens = 1
encoder_seq_length = None
decoder_seq_length = None
batch_size = #100
epochs = #1
hidden_units= #30
timesteps= #20

#Input Data 
 input_seqs=train #data shape(10000,20,1)
 target_seqs=reversed_train # shape(10000,20,1) 
 with same shape as above

 #define training encoder
 encoder_inputs = Input(shape=(None, num_encoder_tokens))
 encoder = LSTM(hidden_units, return_state=True)
 encoder_outputs, state_h, state_c = encoder(encoder_inputs)
 encoder_states = [state_h, state_c]
 #define training decoder
 decoder_inputs = Input(shape=(None,num_decoder_tokens))
 decoder_lstm = LSTM(hidden_units, return_sequences=True, 
 return_state=True)

 decoder_outputs, _, _ = decoder_lstm(decoder_inputs, 
 initial_state=encoder_states)
 decoder_dense = Dense(num_encoder_tokens, activation='tanh')
 decoder_outputs = decoder_dense(decoder_outputs)
 model = Model([encoder_inputs, decoder_inputs], decoder_outputs)

 #training
 model.compile(optimizer='adam', loss='mse')
 model.fit([input_seqs, target_seqs], target_seqs,batch_size=batch_size, 
  epochs=epochs,validation_split=0.2)

 #Testdata
 target_seqs=test#shape(20000,20,1)

 #define inference encoder
 encoder_model = Model(encoder_inputs, encoder_states)
 #define inference decoder
 decoder_state_input_h = Input(shape=(hidden_units,))
 decoder_state_input_c = Input(shape=(hidden_units,))
 decoder_states_inputs = [decoder_state_input_h, decoder_state_input_c]

 decoder_outputs, state_h, state_c = decoder_lstm(decoder_inputs, 
 initial_state=decoder_states_inputs)
 decoder_states = [state_h, state_c]
 decoder_outputs = decoder_dense(decoder_outputs)
 decoder_model = Model([decoder_inputs] + decoder_states_inputs, 
 [decoder_outputs] + decoder_states)


 #Initalize states for decoder 
 states_values = encoder_model.predict(input_seqs)

 #empty target
 target_seq = np.zeros((1, 1, num_decoder_tokens))
 #predict
 output=list()
 for t in range(timesteps):
    output_tokens, h, c  = decoder_model.predict([target_seqs] + states_values)
    output.append(output_tokens[0,0,:])
    states_values = [h,c]
    target_seq = output_tokens

该问题如何解决?

0 个答案:

没有答案