-
Notifications
You must be signed in to change notification settings - Fork 11
/
data.py
150 lines (116 loc) · 5.28 KB
/
data.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
from torchtext import data
from torchtext.vocab import Vocab, GloVe
import torch
from torch.autograd import Variable
import re
from collections import OrderedDict, Counter
import numpy as np
import pickle
URL_TOK = '__url__'
PATH_TOK = '__path__'
class UDCv1:
"""
Wrapper for UDCv2 taken from: http://dataset.cs.mcgill.ca/ubuntu-corpus-1.0/.
Everything has been preprocessed and converted to numerical indexes.
"""
def __init__(self, path, batch_size=256, max_seq_len=160, use_mask=False, gpu=True, use_fasttext=False):
self.batch_size = batch_size
self.max_seq_len_c = max_seq_len
self.max_seq_len_r = int(max_seq_len/2)
self.use_mask = use_mask
self.gpu = gpu
self.desc_len = 44
#load the dataset pickle file
with open(f'{path}/dataset_1M.pkl', 'rb') as f:
dataset = pickle.load(f, encoding='ISO-8859-1')
self.train, self.valid, self.test = dataset
#load the fasttext vector
if use_fasttext:
vectors = np.load(f'{path}/fast_text_200_v.npy')
#vectors = np.load(f'{path}/w2vec_200.npy')
#man_vec = np.load(f'{path}/key_vec.npy')
else:
with open(f'{path}/W.pkl', 'rb') as f:
vectors, _ = pickle.load(f, encoding='ISO-8859-1')
#load the command description file
self.ubuntu_cmd_vec = np.load(f'{path}/command_description.npy').item()
#self.ubuntu_cmd_vec = np.load(f'{path}/man_dict_key.npy').item()
print('Finished loading dataset!')
self.n_train = len(self.train['y'])
self.n_valid = len(self.valid['y'])
self.n_test = len(self.test['y'])
self.vectors = torch.from_numpy(vectors.astype(np.float32))
#self.man_vec = torch.from_numpy(man_vec.astype(np.float32))
self.vocab_size = self.vectors.size(0)
self.emb_dim = self.vectors.size(1)
def get_iter(self, dataset='train'):
if dataset == 'train':
dataset = self.train
elif dataset == 'valid':
dataset = self.valid
else:
dataset = self.test
for i in range(0, len(dataset['y']), self.batch_size):
c = dataset['c'][i:i+self.batch_size]
r = dataset['r'][i:i+self.batch_size]
y = dataset['y'][i:i+self.batch_size]
c, r, y, c_mask, r_mask, key_r, key_mask_r = self._load_batch(c, r, y, self.batch_size)
if self.use_mask:
yield c, r, y, c_mask, r_mask, key_r, key_mask_r
else:
yield c, r, y
def get_key(self, sentence, max_seq_len, max_len):
"""
get key mask
:param sentence:
:param max_len:
:return:
"""
key_mask = np.zeros((max_seq_len))
keys = np.zeros((max_seq_len, max_len))
for j, word in enumerate(sentence):
if int(word) in self.ubuntu_cmd_vec.keys():
keys[j] = self.ubuntu_cmd_vec[int(word)][:max_len]
key_mask[j] = 1
else:
keys[j] = np.zeros((max_len))
return key_mask, keys
def _load_batch(self, c, r, y, size):
c_arr = np.zeros([size, self.max_seq_len_c], np.int)
r_arr = np.zeros([size, self.max_seq_len_r], np.int)
y_arr = np.zeros(size, np.float32)
c_mask = np.zeros([size, self.max_seq_len_c], np.float32)
r_mask = np.zeros([size, self.max_seq_len_r], np.float32)
#key_c = np.zeros([size, self.max_seq_len_c, self.desc_len], np.float32)
key_r = np.zeros([size, self.max_seq_len_r, self.desc_len], np.float32)
#key_mask_c = np.zeros([size, self.max_seq_len_c], np.float32)
key_mask_r = np.zeros([size, self.max_seq_len_r], np.float32)
for j, (row_c, row_r, row_y) in enumerate(zip(c, r, y)):
# Truncate
row_c = row_c[:self.max_seq_len_c]
row_r = row_r[:self.max_seq_len_r]
c_arr[j, :len(row_c)] = row_c
r_arr[j, :len(row_r)] = row_r
y_arr[j] = float(row_y)
c_mask[j, :len(row_c)] = 1
r_mask[j, :len(row_r)] = 1
#key_mask_c[j], key_c[j] = self.get_key(row_c, self.max_seq_len_c, self.desc_len)
key_mask_r[j], key_r[j] = self.get_key(row_r, self.max_seq_len_r, self.desc_len)
# Convert to PyTorch tensor
c = Variable(torch.from_numpy(c_arr))
r = Variable(torch.from_numpy(r_arr))
y = Variable(torch.from_numpy(y_arr))
c_mask = Variable(torch.from_numpy(c_mask))
r_mask = Variable(torch.from_numpy(r_mask))
#key_mask_c = Variable(torch.from_numpy(key_mask_c), requires_grad = False)
key_mask_r = Variable(torch.from_numpy(key_mask_r), requires_grad = False)
#key_c = Variable(torch.from_numpy(key_c)).type(torch.LongTensor)
key_r = Variable(torch.from_numpy(key_r)).type(torch.LongTensor)
# Load to GPU
if self.gpu:
c, r, y = c.cuda(), r.cuda(), y.cuda()
c_mask, r_mask = c_mask.cuda(), r_mask.cuda()
#r_mask = r_mask.cuda()
#key_c, key_mask_c, key_r, key_mask_r = key_c.cuda(), key_mask_c.cuda(), key_r.cuda(), key_mask_r.cuda()
key_r, key_mask_r = key_r.cuda(), key_mask_r.cuda()
return c, r, y, c_mask, r_mask, key_r, key_mask_r