-
Notifications
You must be signed in to change notification settings - Fork 14
/
copy_task.py
201 lines (154 loc) · 5.71 KB
/
copy_task.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
##########################################################
# pytorch-qnn v1.0
# Titouan Parcollet
# LIA, Université d'Avignon et des Pays du Vaucluse
# ORKIS, Aix-en-provence
# October 2018
##########################################################
import sys
import torch
import torch.nn as nn
from torch.nn import Parameter
from torch.nn import functional as F
import torch.optim
from torch.autograd import Variable
import numpy as np
from recurrent_models import QRNN, RNN, LSTM, QLSTM
#
# Convert to torch.Variable
#
def tovar(x):
return Variable(torch.FloatTensor(x).cuda())
def getTask(N_BATCH, SEQ_LENGTH, FEAT_SIZE, BLANK_SIZE, embedding):
data = []
lab = []
seq = []
target = []
for i in range(N_BATCH):
# Target values of blank and delim
blank = FEAT_SIZE
delim = FEAT_SIZE + 1
# Embedding
blank_emb = FEAT_SIZE
blank_emb = torch.tensor(blank_emb, dtype=torch.long)
blank_emb = embedding(blank_emb).data.numpy()
delim_emb = FEAT_SIZE + 1
delim_emb = torch.tensor(delim_emb, dtype=torch.long)
delim_emb = embedding(delim_emb).data.numpy()
random_index_list = []
for j in range(SEQ_LENGTH):
random = np.random.randint(FEAT_SIZE, size=(1))
feat = torch.tensor(random, dtype=torch.long)
feat = embedding(feat).data.numpy()[0]
random_index_list.append(random)
seq.append(feat)
target.append(blank)
# BLANK
for j in range(BLANK_SIZE - 1):
seq.append(blank_emb)
target.append(blank)
# Append a last blank to target for delimiter in input
target.append(blank)
# DELIMITER
seq.append(delim_emb)
# Append input to target
for j in random_index_list:
target.append(j)
seq.append(delim_emb)
data.append(seq)
lab.append(target)
seq = []
target = []
return np.array(data), np.array(lab)
#
# DEFINING THE TASK
#
if len(sys.argv) > 1:
BLANK_SIZE = int(sys.argv[1])
else:
BLANK_SIZE = 25
CUDA = True
N_BATCH_TRAIN = 10
SEQ_LENGTH = 10
FEAT_SIZE = 8
EPOCHS = 2000
RNN_HIDDEN_SIZE = 40
QRNN_HIDDEN_SIZE = 80
losses_r = []
losses_q = []
accs_r = []
accs_q = []
accs_test = []
net_r = LSTM(FEAT_SIZE, RNN_HIDDEN_SIZE, CUDA).cuda()
net_q = QLSTM(FEAT_SIZE, QRNN_HIDDEN_SIZE, CUDA).cuda()
emb = nn.Embedding(FEAT_SIZE+2, FEAT_SIZE, max_norm=1.0)
nb_param_q = sum(p.numel() for p in net_q.parameters() if p.requires_grad)
nb_param_r = sum(p.numel() for p in net_r.parameters() if p.requires_grad)
print("QRNN & RNN Copy Task - Titouan Parcollet - LIA, ORKIS")
print("Models Infos --------------------")
print("(QRNN) Number of trainable parameters : "+str(nb_param_q))
print("(RNN) Number of trainable parameters : "+str(nb_param_r))
#
# TRAINING LOOP
#
break_r = False
break_q = False
for epoch in range(EPOCHS):
#
# The input sequence size is 2 times the sequence length + number_of_blank - 1 + 1
# (+ 1 for the delimiter). We generate N_BATCH_TRAIN new sequences each epoch
#
train, train_target = getTask(N_BATCH_TRAIN,SEQ_LENGTH, FEAT_SIZE, BLANK_SIZE, emb)
# Train shape must be (SEQ_LENGTH, BATCH_SIZE, FEATURE_SIZE) for QLSTM and LSTM
train = train.reshape((BLANK_SIZE+(2*SEQ_LENGTH),N_BATCH_TRAIN,FEAT_SIZE))
train_var = tovar(train)
train_target_var = tovar(train_target)
# NN Training
net_r.zero_grad()
p = net_r.forward(train_var)
# Pred. shape : (SEQ_LENGTH, BATCH_SIZE, FEATURE_SIZE) to (SEQ_LENGTH * BATCH_SIZE, FEATURE_SIZE)
predictions = p.view(-1, FEAT_SIZE+1)
# Target shape to (BATCH_SIZE)
targets = train_target_var.view(-1)
loss = nn.CrossEntropyLoss()
val_loss = loss(predictions, targets.long())
val_loss.backward()
net_r.adam.step()
# Train ACC and LOSS
p = p.cpu().data.numpy()
shape = np.argmax(p, axis=2).shape
p = np.reshape(np.argmax(p, axis=2), shape[0]*shape[1])
targets = targets.cpu().data.numpy()
acc = np.sum( p == targets) / (train_target.size)
if (epoch % 5) == 0:
accs_r.append(acc)
losses_r.append(float(val_loss.data))
if (epoch % 10) == 0:
string = " (NN) It : "+str(epoch)+" | Train Loss = "+str(float(val_loss.data))+" | Train Acc = "+str(acc)
print(string)
# QNN Training
net_q.zero_grad()
p = net_q.forward(train_var)
predictions = p.view(-1, FEAT_SIZE+1)
targets = train_target_var.view(-1)
loss = nn.CrossEntropyLoss()
val_loss = loss(predictions, targets.long())
val_loss.backward()
net_q.adam.step()
p = p.cpu().data.numpy()
shape = np.argmax(p, axis=2).shape
p = np.reshape(np.argmax(p, axis=2), shape[0]*shape[1])
targets = targets.cpu().data.numpy()
acc = np.sum( p == targets) / (train_target.size)
if (epoch % 5) == 0:
losses_q.append(float(val_loss.data))
accs_q.append(acc)
if (epoch % 10) == 0:
string = "(QNN) It : "+str(epoch)+" | Train Loss = "+str(float(val_loss.data))+" | Train Acc = "+str(acc)
print(string)
print("Training Ended - Saving Acc and losses in RES")
np.savetxt("RES/memory_task_acc_q_"+str(BLANK_SIZE)+".txt", accs_q)
np.savetxt("RES/memory_task_acc_r_"+str(BLANK_SIZE)+".txt", accs_r)
np.savetxt("RES/memory_task_loss_q_"+str(BLANK_SIZE)+".txt", losses_q)
np.savetxt("RES/memory_task_loss_r_"+str(BLANK_SIZE)+".txt", losses_r)
print("Done ! That's All Folks ;) !")