Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

keras - Neural machine translation - seq2seq encoder-decoder

I am working on seq2seq NMT for french to english translation. In the inference model I am getting cardinality error.

ValueError: Data cardinality is ambiguous:
x sizes: 1, 5, 5
Please provide data which shares the same first dimension.


    encoder_inputs = Input(shape=(None,))
    embedding_e = Embedding(num_source_vocab,256,mask_zero = True)
    encoder_embedding = embedding_e(encoder_inputs)
    encoder = LSTM(256,return_state = True)
    encoder_outputs,state_h,state_c = encoder(encoder_embedding)
    encoder_states = [state_h,state_c]
    
    decoder_inputs = Input(shape=(None,))
    embedding_f = Embedding(num_target_vocab,256,mask_zero = True)
    decoder_embedding = embedding_f(decoder_inputs)
    decoder = LSTM(256,return_sequences = True,return_state = True)
    decoder_outputs,_,_ = decoder(decoder_embedding,initial_state=encoder_states)
    
    decoder_dense = Dense(num_target_vocab,activation= 'softmax')
    decoder_outputs = decoder_dense(decoder_outputs)
    
    model = Model([encoder_inputs,decoder_inputs],[decoder_outputs])
    
    model.compile(optimizer = 'rmsprop',loss = 'categorical_crossentropy',metrics = ['accuracy'])
    
    model.summary()
    
    filepath = 'eng2fre.h5' 
    checkpoint = ModelCheckpoint(filepath, monitor='val_accuracy', verbose=1, save_best_only=True, mode='max')
    
    history = model.fit([encoder_input_data,decoder_input_data],decoder_target_data,epochs =20,batch_size = 64,validation_split=0.2,callbacks=[checkpoint])
    
    encoder_model = Model(encoder_inputs,encoder_states)
    
    decoder_state_input_h = Input(shape=(256,))
    decoder_state_input_c = Input(shape=(256,))
    decoder_states_inputs = [decoder_state_input_h, decoder_state_input_c]
    
    decoder_inputs_single = Input(shape=(1,))
    decoder_inputs_single_x = embedding_f(decoder_inputs_single)
    
    decoder_outputs2, state_h2, state_c2 = decoder(
        decoder_inputs_single_x, initial_state=decoder_states_inputs)
    decoder_states2 = [state_h2, state_c2]
    
    decoder_outputs2 = decoder_dense(decoder_outputs2)
    decoder_model = Model(
        [decoder_inputs_single] + decoder_states_inputs,
        [decoder_outputs2] + decoder_states2)
    
    x=encoder_input_data[100]
    states = encoder_model.predict(x)
    input_single = np.zeros((1,1))
    input_single[0,0] = target_vocab['sos']
    eos_id = target_vocab['eos']

    # getting error after the following chunk of code

    for i in range(max_target_length):
        dec_op,h,c = decoder_model.predict([input_single]+ states)

question from:https://stackoverflow.com/questions/65879201/neural-machine-translation-seq2seq-encoder-decoder

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...