-
Notifications
You must be signed in to change notification settings - Fork 78
/
data.py
170 lines (140 loc) · 5.29 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import utils
import random
import pickle
from tensorflow.python import keras
import numpy as np
import params as par
class Data:
def __init__(self, dir_path):
self.files = list(utils.find_files_by_extensions(dir_path, ['.pickle']))
self.file_dict = {
'train': self.files[:int(len(self.files) * 0.8)],
'eval': self.files[int(len(self.files) * 0.8): int(len(self.files) * 0.9)],
'test': self.files[int(len(self.files) * 0.9):],
}
self._seq_file_name_idx = 0
self._seq_idx = 0
pass
def __repr__(self):
return '<class Data has "'+str(len(self.files))+'" files>'
def batch(self, batch_size, length, mode='train'):
batch_files = random.sample(self.file_dict[mode], k=batch_size)
batch_data = [
self._get_seq(file, length)
for file in batch_files
]
return np.array(batch_data) # batch_size, seq_len
def seq2seq_batch(self, batch_size, length, mode='train'):
data = self.batch(batch_size, length * 2, mode)
x = data[:, :length]
y = data[:, length:]
return x, y
def smallest_encoder_batch(self, batch_size, length, mode='train'):
data = self.batch(batch_size, length * 2, mode)
x = data[:, :length//100]
y = data[:, length//100:length//100+length]
return x, y
def slide_seq2seq_batch(self, batch_size, length, mode='train'):
data = self.batch(batch_size, length+1, mode)
x = data[:, :-1]
y = data[:, 1:]
return x, y
def random_sequential_batch(self, batch_size, length):
batch_files = random.sample(self.files, k=batch_size)
batch_data = []
for i in range(batch_size):
data = self._get_seq(batch_files[i])
for j in range(len(data) - length):
batch_data.append(data[j:j+length])
if len(batch_data) == batch_size:
return batch_data
def sequential_batch(self, batch_size, length):
batch_data = []
data = self._get_seq(self.files[self._seq_file_name_idx])
while len(batch_data) < batch_size:
while self._seq_idx < len(data) - length:
batch_data.append(data[self._seq_idx: self._seq_idx + length])
self._seq_idx += 1
if len(batch_data) == batch_size:
return batch_data
self._seq_idx = 0
self._seq_file_name_idx = self._seq_file_name_idx + 1
if self._seq_file_name_idx == len(self.files):
self._seq_file_name_idx = 0
print('iter intialized')
def _get_seq(self, fname, max_length=None):
with open(fname, 'rb') as f:
data = pickle.load(f)
if max_length is not None:
if max_length <= len(data):
start = random.randrange(0,len(data) - max_length)
data = data[start:start + max_length]
else:
data = np.append(data, par.token_eos)
while len(data) < max_length:
data = np.append(data, par.pad_token)
return data
class PositionalY:
def __init__(self, data, idx):
self.data = data
self.idx = idx
def position(self):
return self.idx
def data(self):
return self.data
def __repr__(self):
return '<Label located in {} position.>'.format(self.idx)
def add_noise(inputs: np.array, rate:float = 0.01): # input's dim is 2
seq_length = np.shape(inputs)[-1]
num_mask = int(rate * seq_length)
for inp in inputs:
rand_idx = random.sample(range(seq_length), num_mask)
inp[rand_idx] = random.randrange(0, par.pad_token)
return inputs
if __name__ == '__main__':
import pprint
def count_dict(max_length, data):
cnt_arr = [0] * max_length
cnt_dict = {}
# print(cnt_arr)
for batch in data:
for index in batch:
try:
cnt_arr[int(index)] += 1
except:
print(index)
try:
cnt_dict['index-'+str(index)] += 1
except KeyError:
cnt_dict['index-'+str(index)] = 1
return cnt_arr
# print(add_noise(np.array([[1,2,3,3,4,5,6]]), rate=0.2))
# print(par.vocab_size)
# data = Data('dataset/processed')
# # ds = DataSequence('dataset/processed', 10, 2048)
# sample = data.seq2seq_batch(1000, 100)[0]
# pprint.pprint(list(sample))
# arr = count_dict(par.vocab_size+3,sample)
# pprint.pprint(
# arr)
#
# from sequence import EventSeq, Event
#
# event_cnt = {
# 'note_on': 0,
# 'note_off': 0,
# 'velocity': 0,
# 'time_shift': 0
# }
# for event_index in range(len(arr)):
# for event_type, feat_range in EventSeq.feat_ranges().items():
#
# if feat_range.start <= event_index < feat_range.stop:
# print(event_type+':'+str(arr[event_index])+' event cnt: '+str(event_cnt))
# event_cnt[event_type] += arr[event_index]
#
# print(event_cnt)
# print(np.max(sample), np.min(sample))
# print([data._get_seq(file).shape for file in data.files])
#while True:
# print(ds.__getitem__(10)[1].argmax(-1))