if episode_length % 1000 == 0: if tag_txt: path = checkpoint_path + '/' + str(episode_length) + '_txt.model' torch.save(model_txt.state_dict(), path) if tag_img: path = checkpoint_path + '/' + str(episode_length) + '_img.model' torch.save(model_img.state_dict(), path) if tag_txt: model_txt.train() if tag_img: model_img.train() if dataset == 'mir': ori_img_fc7, pos_img_fc7, neg_img_fc7, ori_img_fc6, pos_img_fc6, neg_img_fc6, ori_img_p5, pos_img_p5, neg_img_p5, ori_txt, pos_txt, neg_txt, lab = traintest.get_batch( batch_size) if tag_img: ori_img_fc7 = Variable(ori_img_fc7).cuda() pos_img_fc7 = Variable(pos_img_fc7).cuda() neg_img_fc7 = Variable(neg_img_fc7).cuda() ori_img_fc6 = Variable(ori_img_fc6).cuda() pos_img_fc6 = Variable(pos_img_fc6).cuda() neg_img_fc6 = Variable(neg_img_fc6).cuda() ori_img_p5 = Variable(ori_img_p5).cuda() pos_img_p5 = Variable(pos_img_p5).cuda() neg_img_p5 = Variable(neg_img_p5).cuda() hash_o_img = Variable(torch.zeros(batch_size, 1).cuda())
if update_lr_flag: if opt.learning_rate_decay_start >= 0 and epoch > opt.learning_rate_decay_start: frac = (epoch - opt.learning_rate_decay_start ) // opt.learning_rate_decay_every decay_factor = opt.learning_rate_decay_rate**frac current_lr = opt.learning_rate * decay_factor utils.set_lr(optimizer, current_lr) if opt.scheduled_sampling_start >= 0 and epoch > opt.scheduled_sampling_start: frac = (epoch - opt.scheduled_sampling_start ) // opt.scheduled_sampling_increase_every model.ss_prob = min(opt.scheduled_sampling_increase_prob**frac, opt.scheduled_sampling_max_prob) update_lr_flag = False # 加载训练数据 data = loader.get_batch('train') torch.cuda.synchronize() buf = [ data['fc_feats'], data['att_feats'], data['captions'], data['masks'] ] buf = [ Variable(torch.from_numpy(d), requires_grad=False).cuda() for d in buf ] fc_feats, att_feats, captions, masks = buf optimizer.zero_grad() output = model(fc_feats, att_feats, captions) loss = criterion(output, captions[:, 1:], masks[:, 1:]) loss.backward()