-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmydataset.py
448 lines (366 loc) · 16.6 KB
/
mydataset.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
import pickle
import numpy as np
import scipy.io as sio
import torch
from torch.utils.data import Dataset, DataLoader
# minist
class CustomDataset(Dataset):
def __init__(self, data_file):
# with open(data_file, 'rb') as f:
# data = pickle.load(f)
# self.train_data = data['doc_bow'].toarray()
# self.N, self.vocab_size = self.train_data.shape
# self.voc = data['word2id']
data = sio.loadmat('mnist_data/mnist')
self.train_data = np.array(np.ceil(data['train_mnist'] * 5), order='C') # 0-1
self.test_data = np.array(np.ceil(data['test_mnist'] * 5), order='C') # 0-1
self.N, self.vocab_size = self.train_data.shape
def __getitem__(self, index):
topic_data = self.train_data[index, :]
return np.squeeze(topic_data), 1
def __len__(self):
return self.N
def get_loader(topic_data_file, batch_size=200, shuffle=True, num_workers=0):
dataset = CustomDataset(topic_data_file)
return DataLoader(dataset, batch_size=batch_size, shuffle=shuffle, num_workers=num_workers,
drop_last=True), dataset.vocab_size
# txt data
class CustomDataset_txt(Dataset):
def __init__(self, data_file, voc_size):
with open(data_file, 'rb') as f:
data = pickle.load(f)
if voc_size == 2000:
data_all = data['data_2000'].toarray()
self.voc = data['voc2000']
else:
data_all = data['data_36804'].toarray()
self.voc = data['voc36804']
self.train_data = data_all[data['train_id']].astype("int32")
self.train_label = [data['label'][i] for i in data['train_id']]
self.N, self.vocab_size = self.train_data.shape
def __getitem__(self, index):
topic_data = self.train_data[index, :]
label_data = self.train_label[index]
return torch.from_numpy(np.squeeze(topic_data)).float(), torch.tensor(label_data).float()
def __len__(self):
return self.N
class CustomTestDataset_txt(Dataset):
def __init__(self, data_file, voc_size):
with open(data_file, 'rb') as f:
data = pickle.load(f)
if voc_size == 2000:
data_all = data['data_2000'].toarray()
self.voc = data['voc2000']
elif voc_size == 36804:
data_all = data['data_36804'].toarray()
self.voc = data['voc36804']
else:
data_all = data['data_20000'].toarray()
self.voc = data['voc20000']
self.test_data = data_all[data['test_id']].astype("int32")
self.test_label = [data['label'][i] for i in data['test_id']]
self.N, self.vocab_size = self.test_data.shape
def __getitem__(self, index):
topic_data = self.test_data[index, :]
label_data = self.test_label[index]
return torch.from_numpy(np.squeeze(topic_data)).float(), torch.tensor(label_data).float()
def __len__(self):
return self.N
class CustomTest_ALL_Dataset_txt(Dataset):
def __init__(self, data_file, voc_size):
with open(data_file, 'rb') as f:
data = pickle.load(f)
if voc_size == 2000:
data_all = data['data_2000'].toarray()
self.voc = data['voc2000']
elif voc_size == 36804:
data_all = data['data_36804'].toarray()
self.voc = data['voc36804']
else:
data_all = data['data_20000'].toarray()
self.voc = data['voc20000']
self.train_data = data_all.astype("int32")
self.train_label = data['label']
self.N, self.vocab_size = self.train_data.shape
def __getitem__(self, index):
topic_data = self.train_data[index, :]
label_data = self.train_label[index]
return torch.from_numpy(np.squeeze(topic_data)).float(), torch.tensor(label_data).float()
def __len__(self):
return self.N
class CustomTestDataset_r8_txt(Dataset):
def __init__(self, data_file, voc_size):
with open(data_file, 'rb') as f:
data = pickle.load(f)
data_all = data['test_bow'].toarray()
self.voc = data['voc']
self.test_data = data_all.astype("int32")
self.test_label = data['test_label']
self.N, self.vocab_size = self.test_data.shape
def __getitem__(self, index):
topic_data = self.test_data[index, :]
label_data = self.test_label[index]
return torch.from_numpy(np.squeeze(topic_data)).float(), torch.tensor(label_data).float()
def __len__(self):
return self.N
class CustomTrainDataset_r8_txt(Dataset):
def __init__(self, data_file, voc_size):
with open(data_file, 'rb') as f:
data = pickle.load(f)
data_all = data['train_bow'].toarray()
self.voc = data['voc']
self.test_data = data_all.astype("int32")
self.test_label = data['train_label']
self.N, self.vocab_size = self.test_data.shape
def __getitem__(self, index):
topic_data = self.test_data[index, :]
label_data = self.test_label[index]
return torch.from_numpy(np.squeeze(topic_data)).float(), torch.tensor(label_data).float()
def __len__(self):
return self.N
class CustomDataset_txt_ppl(Dataset):
def __init__(self, data_file):
with open(data_file, 'rb') as f:
data = pickle.load(f)
data_all = data['data_36804'].toarray()
self.train_data, self.test_data = gen_ppl_doc(data_all.astype("int32"))
self.voc = data['voc36804']
self.N, self.vocab_size = self.train_data.shape
def __getitem__(self, index):
return torch.from_numpy(np.squeeze(self.train_data[index])).float(), torch.from_numpy(np.squeeze(self.test_data[index])).float()
def __len__(self):
return self.N
class CustomDataset_txt_ppl_2000(Dataset):
def __init__(self, data_file):
with open(data_file, 'rb') as f:
data = pickle.load(f)
data_all = data['data_2000'].toarray()
self.train_data, self.test_data = gen_ppl_doc(data_all.astype("int32"))
self.voc = data['voc2000']
self.N, self.vocab_size = self.train_data.shape
self.label = data['label']
def __getitem__(self, index):
return torch.from_numpy(np.squeeze(self.train_data[index])).float(), torch.from_numpy(np.squeeze(self.test_data[index])).float()
def __len__(self):
return self.N
class CustomDataset_txt_cluster_r8(Dataset):
def __init__(self, data_file):
with open(data_file, 'rb') as f:
data = pickle.load(f)
data_all = data['fea']
self.train_data = data_all.astype("int32")
self.voc = data['voc']
self.N, self.vocab_size = self.train_data.shape
self.train_label = data['gnd']
def __getitem__(self, index):
return torch.from_numpy(np.squeeze(self.train_data[index])).float(), torch.from_numpy(np.squeeze(self.train_label[index])).float()
def __len__(self):
return self.N
class CustomDataset_txt_ppl_r8(Dataset):
def __init__(self, data_file):
with open(data_file, 'rb') as f:
data = pickle.load(f)
data_all = data['fea']
self.train_data, self.test_data = gen_ppl_doc(data_all.astype("int32"))
self.voc = data['voc']
self.N, self.vocab_size = self.train_data.shape
self.train_label = data['gnd']
def __getitem__(self, index):
return torch.from_numpy(np.squeeze(self.train_data[index])).float(), torch.from_numpy(np.squeeze(self.test_data[index])).float()
def __len__(self):
return self.N
class CustomDataset_cluster_trec_6(Dataset):
def __init__(self, data_file):
with open(data_file, 'rb') as f:
data = pickle.load(f)
data_all = data['fea']
# self.train_data, self.test_data = gen_ppl_doc(data_all.astype("int32"))
self.train_data = data_all.astype("int32")
self.voc = data['voc']
self.N, self.vocab_size = self.train_data.shape
self.train_label = data['gnd']
def __getitem__(self, index):
return torch.from_numpy(np.squeeze(self.train_data[index])).float(), torch.from_numpy(
np.squeeze(self.train_label[index])).float()
def __len__(self):
return self.N
class CustomDataset_txt_ppl_rcv1_2000(Dataset):
def __init__(self, data_file):
with open(data_file, 'rb') as f:
data = pickle.load(f)
self.data_all = data['rcv2_bow_2000']
self.voc = data['rcv2_voc_2000']
del data
self.N, self.vocab_size = self.data_all.shape
def __getitem__(self, index):
ret = self.data_all[index].toarray()
train_data, test_data = gen_ppl_doc(ret.astype("int32"))
return torch.from_numpy(np.squeeze(train_data)).float(), torch.from_numpy(np.squeeze(test_data)).float()
def __len__(self):
return self.N
class CustomDataset_txt_ppl_rcv1_10000(Dataset):
def __init__(self, data_file):
with open(data_file, 'rb') as f:
data = pickle.load(f)
self.data_all = data['rcv2_bow_10000']
self.voc = data['rcv2_voc_10000']
del data
self.N, self.vocab_size = self.data_all.shape
def __getitem__(self, index):
ret = self.data_all[index].toarray()
train_data, test_data = gen_ppl_doc(ret.astype("int32"))
return torch.from_numpy(np.squeeze(train_data)).float(), torch.from_numpy(np.squeeze(test_data)).float()
def __len__(self):
return self.N
def gen_ppl_doc(x, ratio=0.8):
"""
inputs:
x: N x V, np array,
ratio: float or double,
returns:
x_1: N x V, np array, the first half docs whose length equals to ratio * doc length,
x_2: N x V, np array, the second half docs whose length equals to (1 - ratio) * doc length,
"""
import random
x_1, x_2 = np.zeros_like(x), np.zeros_like(x)
# indices_x, indices_y = np.nonzero(x)
for doc_idx, doc in enumerate(x):
indices_y = np.nonzero(doc)[0]
l = []
for i in range(len(indices_y)):
value = doc[indices_y[i]]
for _ in range(int(value)):
l.append(indices_y[i])
random.seed(2020)
random.shuffle(l)
l_1 = l[:int(len(l) * ratio)]
l_2 = l[int(len(l) * ratio):]
for l1_value in l_1:
x_1[doc_idx][l1_value] += 1
for l2_value in l_2:
x_2[doc_idx][l2_value] += 1
return x_1, x_2
def get_loader_txt(topic_data_file, batch_size=200, voc_size=36804, shuffle=True, num_workers=0):
if topic_data_file[-13:] == 'rcv1_2000.pkl':
dataset = CustomDataset_txt_ppl_rcv1_2000(topic_data_file)
if topic_data_file[-12:] == 'rcv1_new.pkl':
dataset = CustomDataset_txt_ppl_rcv1_10000(topic_data_file)
if topic_data_file[-6:] == 'r8.pkl':
dataset = CustomDataset_txt_cluster_r8(topic_data_file)
if topic_data_file[-13:] == 'r8_little.pkl':
dataset = CustomTrainDataset_r8_txt(topic_data_file, voc_size)
if topic_data_file[-6:] == 'ng.pkl':
dataset = CustomDataset_txt(topic_data_file, voc_size)
# dataset = CustomDataset_txt(topic_data_file, voc_size)
return DataLoader(dataset, batch_size=batch_size, shuffle=shuffle, num_workers=num_workers,
drop_last=True), voc_size, dataset.voc
def get_test_loader_txt(topic_data_file, batch_size=200, voc_size=36804, shuffle=True, num_workers=0):
if topic_data_file[-6:] == 'ng.pkl':
# dataset = CustomTest_ALL_Dataset_txt(topic_data_file, voc_size)
dataset = CustomTestDataset_txt(topic_data_file, voc_size)
if topic_data_file[-13:] == 'r8_little.pkl':
dataset = CustomTestDataset_r8_txt(topic_data_file, voc_size)
if topic_data_file[-6:] == 'r8.pkl':
dataset = CustomDataset_cluster_trec_6(topic_data_file)
if topic_data_file[-10:] == 'trec_6.pkl' or topic_data_file[-14:] == 'trec_train.pkl' or topic_data_file[-13:] == 'trec_test.pkl'\
or topic_data_file[-15:] == 'WebKB_train.pkl' or topic_data_file[-14:] == 'WebKB_test.pkl':
dataset = CustomDataset_cluster_trec_6(topic_data_file)
# if topic_data_file[-10:] == 'trec_train.pkl'
return DataLoader(dataset, batch_size=batch_size, shuffle=shuffle, num_workers=num_workers,
drop_last=True), voc_size, dataset.voc
def get_loader_txt_ppl(topic_data_file, batch_size=200, voc_size=36804, shuffle=True, num_workers=4):
if topic_data_file[-13:] == 'rcv1_2000.pkl':
dataset = CustomDataset_txt_ppl_rcv1_2000(topic_data_file)
if topic_data_file[-14:] == 'rcv1_10000.pkl':
dataset = CustomDataset_txt_ppl_rcv1_10000(topic_data_file)
if topic_data_file[-6:] == 'r8.pkl':
dataset = CustomDataset_txt_ppl_r8(topic_data_file)
if topic_data_file[-6:] == 'ng.pkl':
if voc_size == 36804:
dataset = CustomDataset_txt_ppl(topic_data_file)
else:
dataset = CustomDataset_txt_ppl_2000(topic_data_file)
return DataLoader(dataset, batch_size=batch_size, shuffle=shuffle, num_workers=num_workers,
drop_last=True), dataset.vocab_size, dataset.voc
# class CustomDataset(Dataset):
# def __init__(self, data_file):
# # with open(data_file, 'rb') as f:
# # data = pickle.load(f)
# # self.train_data = data['doc_bow'].toarray()
# # self.N, self.vocab_size = self.train_data.shape
# # self.voc = data['word2id']
# data = sio.loadmat('mnist_data/mnist')
# self.train_data = np.array(np.ceil(data['train_mnist'] * 5), order='C') # 0-1
# self.test_data = np.array(np.ceil(data['test_mnist'] * 5), order='C') # 0-1
# self.N, self.vocab_size = self.train_data.shape
#
# def __getitem__(self, index):
# topic_data = self.train_data[index, :]
# return np.squeeze(topic_data), 1
#
# def __len__(self):
# return self.N
#
# def get_loader(topic_data_file, batch_size=200, shuffle=True, num_workers=0):
# dataset = CustomDataset(topic_data_file)
# return DataLoader(dataset, batch_size=batch_size, shuffle=shuffle, num_workers=num_workers,
# drop_last=True), dataset.vocab_size
#
# class CustomDataset(Dataset):
# def __init__(self, data_file):
# with open(data_file, 'rb') as f:
# data = pickle.load(f)
# train_id = data['train_id']
# test_id = data['test_id']
# train_data = data['data_2000']
# test_data = data['data_2000'][test_id]
# train_label = np.array(data['label'])[train_id]
# test_label = np.array(data['label'])[test_id]
# voc = data['voc2000']
# self.train_data = train_data
# self.N, self.vocab_size = self.train_data.shape
# self.voc = voc
#
# def __getitem__(self, index):
# topic_data = self.train_data[index].toarray()
# return np.squeeze(topic_data), 1
#
# def __len__(self):
# return self.N
# def get_loader_txt_ppl_withLabel(topic_data_file, train=True, batch_size=200, voc_size=36804, shuffle=True, num_workers=0):
# if topic_data_file[-7:] == 'bow.pkl':
# dataset = CustomDataset_txt_ppl_rcv1(topic_data_file)
# if topic_data_file[-6:] == 'ng.pkl':
# if voc_size == 36804:
# dataset = CustomDataset_txt_ppl(topic_data_file)
# else:
# dataset = CustomDataset_ppl_ng2000_withLabel(topic_data_file, train)
#
# return DataLoader(dataset, batch_size=batch_size, shuffle=shuffle, num_workers=num_workers,
# drop_last=True), dataset.vocab_size, dataset.voc
class CustomDataset_ppl_ng2000_withLabel(Dataset):
def __init__(self, data_file, train=True):
with open(data_file, 'rb') as f:
data = pickle.load(f)
data_all = data['data_2000'].toarray()
self.train_data, self.test_data = gen_ppl_doc(data_all.astype("int32"))
total_len, _ = self.train_data.shape
self.spilit_sapce = int(total_len * 0.8)
self.train_classfication = self.train_data[:self.spilit_sapce]
self.test_classfication = self.train_data[self.spilit_sapce:]
self.voc = data['voc2000']
self.train = train
if train:
self.N, self.vocab_size = self.train_classfication.shape
else:
self.N, self.vocab_size = self.test_classfication.shape
self.label = data['label']
def __getitem__(self, index):
if self.train:
return torch.from_numpy(np.squeeze(self.train_classfication[index])).float(),\
torch.tensor(self.label[index])
else:
return torch.from_numpy(np.squeeze(self.test_classfication[index])).float(),\
torch.tensor(self.label[self.spilit_sapce+index])
def __len__(self):
return self.N