<jupyter_start><jupyter_text>Change Here for input files<jupyter_code># input_img_h5 = 'vdl_img_vgg_demo.h5'
# input_ques_h5 = 'visdial_data_demo.h5'
input_img_h5 = 'vdl_img_vgg.h5'
input_ques_h5 = 'visdial_data.h5'
input_json = 'visdial_params.json'
negative_sample = 20
num_val = 1000
dataset = train(input_img_h5=input_img_h5, input_ques_h5=input_ques_h5,
                input_json=input_json, negative_sample = negative_sample,
                num_val = num_val, data_split = 'train')

dataset_val = validate(input_img_h5=input_img_h5, input_ques_h5=input_ques_h5,
                input_json=input_json, negative_sample = negative_sample,
                num_val = num_val, data_split = 'test')
batchSize = 10
num_workers = 0
dloader = torch.utils.data.DataLoader(dataset_val, batch_size=batchSize,
                                         shuffle=True, num_workers=int(num_workers))
dataloader_val = torch.utils.data.DataLoader(dataset_val, batch_size=1,
                                         shuffle=False, num_workers=int(num_workers))
data_iter1 = iter(dloader)
data = data_iter1.next()
# in case dataloader used is dloader
# image, history, question, answer, answerT, answerLen, answerIdx, answerIds, questionL, \
#         opt_answerT, opt_answerLen, opt_answerIdx = data
wantVal = 1
if wantVal == 0:
    image, history, question, answer, answerT, questionL, opt_answer, \
                opt_answerT, answerIds, answerLen, opt_answerLen, img_id  = data
if wantVal == 1:
    data_iter_val = iter(dataloader_val)
    data_val = data_iter_val.next()
    image, history, question, answer, answerT, questionL, opt_answer, \
                opt_answerT, answer_ids, answerLen, opt_answerLen, img_id  = data_val
rnd = 3
ques_length = dataset.ques_length
negative_sample = 20
n_neg = negative_sample
vocab_size = dataset.vocab_size
ques_length = dataset.ques_length
ans_length = dataset.ans_length + 1
his_length = dataset.ans_length + dataset.ques_length
itow = dataset.itow
img_feat_size = 512
netW = _netW(vocab_size, ninp, dropout)
netE = _netE(model, ninp, nhid, nlayers, dropout, img_feat_size)
if enCuda:
    netE.cuda()
    netW.cuda()
ques_input = torch.LongTensor(ques_length, batchSize)
if enCuda:
    ques_input = ques_input.cuda()
ques_input = Variable(ques_input)
ques = question[:,rnd,:].t()
ques_input.data.resize_(ques.size()).copy_(ques)

ques_emb = netW(ques_input, format = 'index')
image = image.view(-1, img_feat_size)
img_input = torch.FloatTensor(batchSize)
img_input = Variable(img_input)
img_input.data.resize_(image.size()).copy_(image)

his_input = torch.LongTensor(his_length, batchSize)
if(enCuda):
    his_input = his_input.cuda()
his_input = Variable(his_input)
his = history[:,:rnd+1,:].clone().view(-1, his_length).t()
his_input.data.resize_(his.size()).copy_(his)

his_emb = netW(his_input, format = 'index')

batch_size = question.size(0)

ques_hidden = netE.init_hidden(batchSize)
hist_hidden = netE.init_hidden(batchSize)

ques_hidden = repackage_hidden(ques_hidden, batch_size)
hist_hidden = repackage_hidden(hist_hidden, his_input.size(1))
batch_size
if enCuda:
    his_input = his_input.cuda()
    ques_input = ques_input.cuda()
    img_input = img_input.cuda()
    ques_emb = ques_emb.cuda()
    his_emb = his_emb.cuda()
featD, ques_hidden = netE(ques_emb, his_emb, img_input, ques_hidden, hist_hidden, rnd+1)
featD.mean()<jupyter_output><empty_output><jupyter_text>Decoder<jupyter_code>netD = _netD(model, ninp, nhid, nlayers, vocab_size, dropout)
if enCuda:
    netD.cuda()
opt_ans_input = torch.LongTensor(ans_length, batchSize)
opt_ans_input = Variable(opt_ans_input)
if enCuda:
    opt_ans_input = opt_ans_input.cuda()

opt_ans = opt_answerT[:,rnd,:].clone().view(-1, ans_length).t()
opt_ans_input.data.resize_(opt_ans.size()).copy_(opt_ans)


opt_ans_emb = netW(opt_ans_input, format = 'index')
# opt_ans_emb = opt_ans_emb.cuda()

opt_hidden = netD.init_hidden(batchSize)
# opt_hidden.cuda()
opt_hidden = repackage_hidden(opt_hidden, opt_ans_input.size(1))
# opt_hidden[0].data.cuda()
# opt_hidden[1].data.cuda()
# opt_hidden = torch.LongTensor(opt_hidden)
# print(opt_hidden.shape())

prob = netD(featD,opt_hidden,opt_ans_emb,vocab_size)
prob<jupyter_output><empty_output><jupyter_text>Training & Val Trial<jupyter_code># del img_input, ques_input, his_input, ans_input, ans_target, wrong_ans_input, opt_ans_input, batch_sample_idx
# del fake_diff_mask, fake_len, noise_input, gt_index
# %reset 
img_input = torch.FloatTensor(batchSize)
ques_input = torch.LongTensor(ques_length, batchSize)
his_input = torch.LongTensor(his_length, batchSize)

# answer input
ans_input = torch.LongTensor(ans_length, batchSize)
ans_target = torch.LongTensor(ans_length, batchSize)
wrong_ans_input = torch.LongTensor(ans_length, batchSize)
sample_ans_input = torch.LongTensor(1, batchSize)
opt_ans_input = torch.LongTensor(ans_length, batchSize)

batch_sample_idx = torch.LongTensor(batchSize)
fake_diff_mask = torch.ByteTensor(batchSize)
fake_len = torch.LongTensor(batchSize)
noise_input = torch.FloatTensor(batchSize)
gt_index = torch.LongTensor(batchSize)
if enCuda:
    ques_input, his_input, img_input = ques_input.cuda(), his_input.cuda(), img_input.cuda()
    ans_input, ans_target = ans_input.cuda(), ans_target.cuda()
    wrong_ans_input = wrong_ans_input.cuda()
    sample_ans_input = sample_ans_input.cuda()

    fake_len = fake_len.cuda()
    noise_input = noise_input.cuda()
    batch_sample_idx = batch_sample_idx.cuda()
    fake_diff_mask = fake_diff_mask.cuda()
    opt_ans_input = opt_ans_input.cuda()
    gt_index = gt_index.cuda()
ques_input = Variable(ques_input)
img_input = Variable(img_input)
his_input = Variable(his_input)

ans_input = Variable(ans_input)
ans_target = Variable(ans_target)
wrong_ans_input = Variable(wrong_ans_input)
sample_ans_input = Variable(sample_ans_input)

noise_input = Variable(noise_input)
batch_sample_idx = Variable(batch_sample_idx)
fake_diff_mask = Variable(fake_diff_mask)
opt_ans_input = Variable(opt_ans_input)
gt_index = Variable(gt_index)
lr = 0.0004
beta1 = 0.8
log_interval = 50
optimizer = optim.Adam([{'params': netW.parameters()},
                        {'params': netE.parameters()},
                        {'params': netD.parameters()}], lr=lr, betas=(beta1, 0.999))
critD = nn.NLLLoss()

if enCuda:
    critD.cuda()

# netW.cuda()
epoch = 0

trainModel(epoch)
rank_all = valModel()
epoch = 1
R1 = np.sum(np.array(rank_all)==1) / float(len(rank_all))
R5 =  np.sum(np.array(rank_all)<=5) / float(len(rank_all))
R10 = np.sum(np.array(rank_all)<=10) / float(len(rank_all))
ave = np.sum(np.array(rank_all)) / float(len(rank_all))
mrr = np.sum(1/(np.array(rank_all, dtype='float'))) / float(len(rank_all))
print ('%d/%d: mrr: %f R1: %f R5 %f R10 %f Mean %f' %(epoch, len(dataloader_val), mrr, R1, R5, R10, ave))
rank_all<jupyter_output><empty_output><jupyter_text>Main<jupyter_code>save_path = '~/notebooks/save/D.15-4-17'
torch.save({'epoch': epoch,
                    'netW': netW.state_dict(),
                    'netD': netD.state_dict(),
                    'netE': netE.state_dict()},
                    '%s/epoch_%d.pth' % (save_path, epoch))
# model_path = ''
# model_path = save_path
model_path = './save/D.15-4-16/epoch_1.pth'
outf = './save'
decoder = 'D'

if model_path != '':
    print("=> loading checkpoint '{}'".format(model_path))
    checkpoint = torch.load(model_path)
    start_epoch = checkpoint['epoch']
    batchSize = 1
else:
    # create new folder.
    t = datetime.datetime.now()
    cur_time = '%s-%s-%s' %(t.day, t.month, t.hour)
    save_path = os.path.join(outf, decoder + '.' + cur_time)
    try:
        os.makedirs(save_path)
    except OSError:
        pass
if model_path != '': # load the pre-trained model.
    netW.load_state_dict(checkpoint['netW'])
    netE.load_state_dict(checkpoint['netE'])
    netD.load_state_dict(checkpoint['netD'])
lr = 0.04
beta1 = 0.8
niter = 5
neg_batch_sample = 30 
log_interval = 20
save_iter = 1
save_path = '~/notebooks/save'
optimizer = optim.Adam([{'params': netW.parameters()},
                        {'params': netE.parameters()},
                        {'params': netD.parameters()}], lr=lr, betas=(beta1, 0.999))
enCuda = 1

if enCuda:
    torch.cuda.manual_seed(manualSeed)
    
critD = nn.NLLLoss()
if enCuda:
    critD.cuda()

history = []

for epoch in range(niter):
    t = time.time()
    train_loss = trainModel(epoch)
    print ('Epoch: %d learningRate %4f train loss %4f Time: %3f' % (epoch, lr, train_loss, time.time()-t))
    train_his = {'loss': train_loss}

    print('Evaluating ... ')
    rank_all = valModel()
    R1 = np.sum(np.array(rank_all)==1) / float(len(rank_all))
    R5 =  np.sum(np.array(rank_all)<=5) / float(len(rank_all))
    R10 = np.sum(np.array(rank_all)<=10) / float(len(rank_all))
    ave = np.sum(np.array(rank_all)) / float(len(rank_all))
    mrr = np.sum(1/(np.array(rank_all, dtype='float'))) / float(len(rank_all))
    print ('%d/%d: mrr: %f R1: %f R5 %f R10 %f Mean %f' %(epoch, len(dataloader_val), mrr, R1, R5, R10, ave))
    val_his = {'R1': R1, 'R5':R5, 'R10': R10, 'Mean':ave, 'mrr':mrr}
    history.append({'epoch':epoch, 'train': train_his, 'val': val_his})

#     saving the model.
#     if epoch % save_iter == 0:
#         torch.save({'epoch': epoch,
#                     'netW': netW.state_dict(),
#                     'netD': netD.state_dict(),
#                     'netE': netE.state_dict()},
#                     '%s/epoch_%d.pth' % (save_path, epoch))

#         json.dump(history, open('%s/log.json' %(save_path), 'w'))<jupyter_output>/home/ubuntu/anaconda2/lib/python2.7/site-packages/ipykernel/__main__.py:41: UserWarning: Implicit dimension choice for log_softmax has been deprecated. Change the call to include dim=X as an argument.
{'fraction_non_alphanumeric': 0.13759967286853403, 'fraction_numerical': 0.015027601717440196, 'mean_word_length': 4.313959804454101, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '75480', 'license': 'MIT', 'n_tokens_mistral': 3799, 'n_tokens_neox': 3690, 'n_words': 649, 'path': 'simple-train.ipynb', 'repo_name': 'anirudhvenu/notebooks', 'size': '9782'}