-
Notifications
You must be signed in to change notification settings - Fork 2
/
train.py
84 lines (67 loc) · 2.95 KB
/
train.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import os
import torch
from torch.autograd import Variable
import torch.nn as nn
from torch import optim
import torch.nn.functional as F
import math
from random import randint
#from decoder import *
SOS_token = 2
EOS_token = 3
UNK_token = 1
MAX_LENGTH = 40
CLIP = 1.0
use_cuda = torch.cuda.is_available()
def train_nmt(input_variable,target_variable,input_lengths,model,criterion,optimizer,teacher_force_ratio=0.5):
#specify this is the training stage
model.train()
#Zero gradients of the optimizer
optimizer.zero_grad()
#FeedForward
loss= model(input_variable,input_lengths,target_variable,teacher_force_ratio,criterion=criterion)
#Backpropagate the Loss
loss.backward()
torch.nn.utils.clip_grad_norm_(model.parameters(),CLIP)
optimizer.step()
return loss.item()
################################Designed for New Imagine Model######################
def train_imagine_beam(input_variable, target_variable, im_variable, input_lengths,model,optimizer,criterion_mt,criterion_vse,loss_weight,teacher_force_ratio,max_length=MAX_LENGTH,clip=1):
#Make model back to trianing
model.train()
#Zero gradients of both optimizerse
optimizer.zero_grad()
#Get the output from the model
loss,loss_mt,loss_vse = model(input_variable,input_lengths,target_variable,im_variable,teacher_force_ratio,criterion_mt=criterion_mt, criterion_vse=criterion_vse)
loss.backward()
torch.nn.utils.clip_grad_norm_(model.parameters(), clip)
#Optimize the Encoder and Decoder
optimizer.step()
return loss.data.item(),loss_mt.data.item(),loss_vse.data.item()
def train_imagine_beam_v2(input_variable, target_variable, im_variable, input_lengths,model,optimizer,criterion_mt,criterion_vse,teacher_force_ratio,max_length=MAX_LENGTH,clip=1,optimized_task="mt"):
#Make model back to trianing
model.train()
#Zero gradients of both optimizerse
optimizer.zero_grad()
#Get the output from the model
loss,loss_mt,loss_vse = model(input_variable,input_lengths,target_variable,im_variable,teacher_force_ratio,criterion_mt=criterion_mt, criterion_vse=criterion_vse)
if optimized_task == "mt":
loss_mt.backward()
torch.nn.utils.clip_grad_norm(model.parameters(),clip)
#Optimize the Encoder and Decoder
optimizer.step()
else:
loss_vse.backward()
torch.nn.utils.clip_grad_norm(model.parameters(),clip)
#Optimize the vse module, as well as Encoder and Decoder
optimizer.step()
return loss.data[0],loss_mt.data[0],loss_vse.data[0]
################################################
#Randomly Display Some Results
def random_sample_display(test_data,output_list):
sample_index = randint(0,len(test_data)-1)
sample_source = test_data[sample_index][0]
sample_ref = test_data[sample_index][1]
sample_output_tokens = output_list[sample_index]
sample_output = ' '.join(sample_output_tokens)
return sample_source, sample_ref, sample_output