-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathData_prep.py
More file actions
124 lines (101 loc) · 4.02 KB
/
Copy pathData_prep.py
File metadata and controls
124 lines (101 loc) · 4.02 KB
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
import torch
import torch.nn as nn
from torch.autograd import Variable
import torch.optim as optim
import torch.nn.functional as F
import nltk
import random
import numpy as np
from collections import Counter, OrderedDict
import nltk
from copy import deepcopy
import os
import re
import unicodedata
flatten = lambda l: [item for sublist in l for item in sublist]
from torch.nn.utils.rnn import PackedSequence, pack_padded_sequence
random.seed(1024)
USE_CUDA = torch.cuda.is_available()
gpus = [0]
torch.cuda.set_device(gpus[0])
FloatTensor = torch.cuda.FloatTensor if USE_CUDA else torch.FloatTensor
LongTensor = torch.cuda.LongTensor if USE_CUDA else torch.LongTensor
ByteTensor = torch.cuda.ByteTensor if USE_CUDA else torch.ByteTensor
def getBatch(batch_size, train_data):
random.shuffle(train_data)
sindex=0
eindex=batch_size
while eindex < len(train_data):
batch = train_data[sindex: eindex]
temp = eindex
eindex = eindex + batch_size
sindex = temp
yield batch
if eindex >= len(train_data):
batch = train_data[sindex:]
yield batch
def pad_to_batch(batch, w_to_ix):
fact,q,a = list(zip(*batch))
max_fact = max([len(f) for f in fact])
max_len = max([f.size(1) for f in flatten(fact)])
max_q = max([qq.size(1) for qq in q])
max_a = max([aa.size(1) for aa in a])
facts, fact_masks, q_p, a_p = [], [], [], []
for i in range(len(batch)):
fact_p_t = []
for j in range(len(fact[i])):
if fact[i][j].size(1) < max_len:
fact_p_t.append(torch.cat([fact[i][j], Variable(LongTensor([w_to_ix['<PAD>']] * (max_len - fact[i][j].size(1)))).view(1, -1)], 1))
else:
fact_p_t.append(fact[i][j])
while len(fact_p_t) < max_fact:
fact_p_t.append(Variable(LongTensor([w_to_ix['<PAD>']] * max_len)).view(1, -1))
fact_p_t = torch.cat(fact_p_t)
facts.append(fact_p_t)
fact_masks.append(torch.cat([Variable(ByteTensor(tuple(map(lambda s: s ==0, t.data))), volatile=False) for t in fact_p_t]).view(fact_p_t.size(0), -1))
if q[i].size(1) < max_q:
q_p.append(torch.cat([q[i], Variable(LongTensor([w_to_ix['<PAD>']] * (max_q - q[i].size(1)))).view(1, -1)], 1))
else:
q_p.append(q[i])
if a[i].size(1) < max_a:
a_p.append(torch.cat([a[i], Variable(LongTensor([w_to_ix['<PAD>']] * (max_a - a[i].size(1)))).view(1, -1)], 1))
else:
a_p.append(a[i])
questions = torch.cat(q_p)
answers = torch.cat(a_p)
question_masks = torch.cat([Variable(ByteTensor(tuple(map(lambda s: s ==0, t.data))), volatile=False) for t in questions]).view(questions.size(0), -1)
return facts, fact_masks, questions, question_masks, answers
def prepare_sequence(seq, to_index):
idxs = list(map(lambda w: to_index[w] if to_index.get(w) is not None else to_index["<UNK>"], seq))
return Variable(LongTensor(idxs))
## Load the Dataset
data = open('qa5_three-arg-relations_train.txt').readlines()
data = [d[:-1] for d in data]
train_data = []
fact=[]
qa=[]
for d in data:
index=d.split(' ')[0]
if(index=='1'):
fact=[]
qa=[]
if('?' in d):
temp = d.split('\t')
ques = temp[0].strip().replace('?', '').split(' ')[1:] + ['?']
ans=temp[1].split() + ['</s>']
temp_s = deepcopy(fact)
train_data.append([temp_s, ques, ans])
else:
fact.append(d.replace('.', '').split(' ')[1:] + ['</s>'])
fact,q,a = list(zip(*train_data))
vocab = list(set(flatten(flatten(fact)) + flatten(q) + flatten(a)))
word_to_index={'<PAD>': 0, '<UNK>': 1, '<s>': 2, '</s>': 3}
for vo in vocab:
if word_to_index.get(vo) is None:
word_to_index[vo] = len(word_to_index)
index_to_word = {v:k for k, v in word_to_index.items()}
for s in train_data:
for i, fact in enumerate(s[0]):
s[0][i] = prepare_sequence(fact, word_to_index).view(1, -1)
s[1] = prepare_sequence(s[1], word_to_index).view(1, -1)
s[2] = prepare_sequence(s[2], word_to_index).view(1, -1)