-
Notifications
You must be signed in to change notification settings - Fork 40
/
data.py
123 lines (115 loc) · 5.26 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
import os
import random
import pickle
import numpy as np
import torch
import scipy.io
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
def _fetch(path, name):
if name == 'train':
token_file = os.path.join(path, 'bow_tr_tokens.mat')
count_file = os.path.join(path, 'bow_tr_counts.mat')
elif name == 'valid':
token_file = os.path.join(path, 'bow_va_tokens.mat')
count_file = os.path.join(path, 'bow_va_counts.mat')
else:
token_file = os.path.join(path, 'bow_ts_tokens.mat')
count_file = os.path.join(path, 'bow_ts_counts.mat')
tokens = scipy.io.loadmat(token_file)['tokens'].squeeze()
counts = scipy.io.loadmat(count_file)['counts'].squeeze()
if name == 'test':
token_1_file = os.path.join(path, 'bow_ts_h1_tokens.mat')
count_1_file = os.path.join(path, 'bow_ts_h1_counts.mat')
token_2_file = os.path.join(path, 'bow_ts_h2_tokens.mat')
count_2_file = os.path.join(path, 'bow_ts_h2_counts.mat')
tokens_1 = scipy.io.loadmat(token_1_file)['tokens'].squeeze()
counts_1 = scipy.io.loadmat(count_1_file)['counts'].squeeze()
tokens_2 = scipy.io.loadmat(token_2_file)['tokens'].squeeze()
counts_2 = scipy.io.loadmat(count_2_file)['counts'].squeeze()
return {'tokens': tokens, 'counts': counts, 'tokens_1': tokens_1, 'counts_1': counts_1, 'tokens_2': tokens_2, 'counts_2': counts_2}
return {'tokens': tokens, 'counts': counts}
def _fetch_temporal(path, name):
if name == 'train':
token_file = os.path.join(path, 'bow_tr_tokens.mat')
count_file = os.path.join(path, 'bow_tr_counts.mat')
time_file = os.path.join(path, 'bow_tr_timestamps.mat')
elif name == 'valid':
token_file = os.path.join(path, 'bow_va_tokens.mat')
count_file = os.path.join(path, 'bow_va_counts.mat')
time_file = os.path.join(path, 'bow_va_timestamps.mat')
else:
token_file = os.path.join(path, 'bow_ts_tokens.mat')
count_file = os.path.join(path, 'bow_ts_counts.mat')
time_file = os.path.join(path, 'bow_ts_timestamps.mat')
tokens = scipy.io.loadmat(token_file)['tokens'].squeeze()
counts = scipy.io.loadmat(count_file)['counts'].squeeze()
times = scipy.io.loadmat(time_file)['timestamps'].squeeze()
if name == 'test':
token_1_file = os.path.join(path, 'bow_ts_h1_tokens.mat')
count_1_file = os.path.join(path, 'bow_ts_h1_counts.mat')
token_2_file = os.path.join(path, 'bow_ts_h2_tokens.mat')
count_2_file = os.path.join(path, 'bow_ts_h2_counts.mat')
tokens_1 = scipy.io.loadmat(token_1_file)['tokens'].squeeze()
counts_1 = scipy.io.loadmat(count_1_file)['counts'].squeeze()
tokens_2 = scipy.io.loadmat(token_2_file)['tokens'].squeeze()
counts_2 = scipy.io.loadmat(count_2_file)['counts'].squeeze()
return {'tokens': tokens, 'counts': counts, 'times': times,
'tokens_1': tokens_1, 'counts_1': counts_1,
'tokens_2': tokens_2, 'counts_2': counts_2}
return {'tokens': tokens, 'counts': counts, 'times': times}
def get_data(path, temporal=False):
### load vocabulary
with open(os.path.join(path, 'vocab.pkl'), 'rb') as f:
vocab = pickle.load(f)
if not temporal:
train = _fetch(path, 'train')
valid = _fetch(path, 'valid')
test = _fetch(path, 'test')
else:
train = _fetch_temporal(path, 'train')
valid = _fetch_temporal(path, 'valid')
test = _fetch_temporal(path, 'test')
return vocab, train, valid, test
def get_batch(tokens, counts, ind, vocab_size, emsize=300, temporal=False, times=None):
"""fetch input data by batch."""
batch_size = len(ind)
data_batch = np.zeros((batch_size, vocab_size))
if temporal:
times_batch = np.zeros((batch_size, ))
for i, doc_id in enumerate(ind):
doc = tokens[doc_id]
count = counts[doc_id]
if temporal:
timestamp = times[doc_id]
times_batch[i] = timestamp
L = count.shape[1]
if len(doc) == 1:
doc = [doc.squeeze()]
count = [count.squeeze()]
else:
doc = doc.squeeze()
count = count.squeeze()
if doc_id != -1:
for j, word in enumerate(doc):
data_batch[i, word] = count[j]
data_batch = torch.from_numpy(data_batch).float().to(device)
if temporal:
times_batch = torch.from_numpy(times_batch).to(device)
return data_batch, times_batch
return data_batch
def get_rnn_input(tokens, counts, times, num_times, vocab_size, num_docs):
indices = torch.randperm(num_docs)
indices = torch.split(indices, 1000)
rnn_input = torch.zeros(num_times, vocab_size).to(device)
cnt = torch.zeros(num_times, ).to(device)
for idx, ind in enumerate(indices):
data_batch, times_batch = get_batch(tokens, counts, ind, vocab_size, temporal=True, times=times)
for t in range(num_times):
tmp = (times_batch == t).nonzero()
docs = data_batch[tmp].squeeze().sum(0)
rnn_input[t] += docs
cnt[t] += len(tmp)
if idx % 20 == 0:
print('idx: {}/{}'.format(idx, len(indices)))
rnn_input = rnn_input / cnt.unsqueeze(1)
return rnn_input