(encoder, decoder, criterion, encoder_optimizer, decoder_optimizer, teach_forcing_prob=1)
return loss
target_label:采用后处理的方式，进行编码和对齐，以便进行batch训练
target_label:采用后处理的方式，进行编码和对齐，以便进行batch训练
target_label : 采用后处理的方式，进行编码和对齐，以便进行batch训练
def trainBatch(encoder, decoder, criterion, encoder_optimizer, decoder_optimizer, teach_forcing_prob=1):
    '''
        target_label:采用后处理的方式，进行编码和对齐，以便进行batch训练
    '''
    data = train_iter.next()
    cpu_images, cpu_texts = data
    b = cpu_images.size(0)
    target_variable = converter.encode(cpu_texts)
    utils.loadData(image, cpu_images)

    encoder_outputs = encoder(image)               # cnn+biLstm做特征提取
    target_variable = target_variable.cuda()
    decoder_input = target_variable[0].cuda()      # 初始化decoder的开始,从0开始输出
    decoder_hidden = decoder.initHidden(b).cuda()
    loss = 0.0
    teach_forcing = True if random.random() > teach_forcing_prob else False
    if teach_forcing:
        # 教师强制：将目标label作为下一个输入
        for di in range(1, target_variable.shape[0]):           # 最大字符串的长度
            decoder_output, decoder_hidden, decoder_attention = decoder(
                decoder_input, decoder_hidden, encoder_outputs)
            loss += criterion(decoder_output, target_variable[di])          # 每次预测一个字符
            decoder_input = target_variable[di]  # Teacher forcing/前一次的输出
    else:
        for di in range(1, target_variable.shape[0]):
            decoder_output, decoder_hidden, decoder_attention = decoder(
                decoder_input, decoder_hidden, encoder_outputs)
            loss += criterion(decoder_output, target_variable[di])  # 每次预测一个字符
            topv, topi = decoder_output.data.topk(1)
            ni = topi.squeeze()
            decoder_input = ni
    encoder.zero_grad()
    decoder.zero_grad()
    loss.backward()
    encoder_optimizer.step()
    decoder_optimizer.step()
    return loss
def trainBatch ( encoder , decoder , criterion , encoder_optimizer , decoder_optimizer , teach_forcing_prob = 1 ) : data = train_iter . next ( ) cpu_images , cpu_texts = data b = cpu_images . size ( 0 ) target_variable = converter . encode ( cpu_texts ) utils . loadData ( image , cpu_images ) encoder_outputs = encoder ( image ) # cnn+biLstm做特征提取 target_variable = target_variable . cuda ( ) decoder_input = target_variable [ 0 ] . cuda ( ) # 初始化decoder的开始,从0开始输出 decoder_hidden = decoder . initHidden ( b ) . cuda ( ) loss = 0.0 teach_forcing = True if random . random ( ) > teach_forcing_prob else False if teach_forcing : # 教师强制：将目标label作为下一个输入 for di in range ( 1 , target_variable . shape [ 0 ] ) : # 最大字符串的长度 decoder_output , decoder_hidden , decoder_attention = decoder ( decoder_input , decoder_hidden , encoder_outputs ) loss += criterion ( decoder_output , target_variable [ di ] ) # 每次预测一个字符 decoder_input = target_variable [ di ] # Teacher forcing/前一次的输出 else : for di in range ( 1 , target_variable . shape [ 0 ] ) : decoder_output , decoder_hidden , decoder_attention = decoder ( decoder_input , decoder_hidden , encoder_outputs ) loss += criterion ( decoder_output , target_variable [ di ] ) # 每次预测一个字符 topv , topi = decoder_output . data . topk ( 1 ) ni = topi . squeeze ( ) decoder_input = ni encoder . zero_grad ( ) decoder . zero_grad ( ) loss . backward ( ) encoder_optimizer . step ( ) decoder_optimizer . step ( ) return loss