forked from tdozat/Parser-v1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataset.py
215 lines (188 loc) · 6.58 KB
/
dataset.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
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# Copyright 2016 Timothy Dozat
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import numpy as np
import tensorflow as tf
from collections import Counter
from lib.etc.k_means import KMeans
from configurable import Configurable
from vocab import Vocab
from metabucket import Metabucket
#***************************************************************
class Dataset(Configurable):
""""""
#=============================================================
def __init__(self, filename, vocabs, builder, *args, **kwargs):
""""""
super(Dataset, self).__init__(*args, **kwargs)
self._file_iterator = self.file_iterator(filename)
self._train = (filename == self.train_file)
self._metabucket = Metabucket(self._config, n_bkts=self.n_bkts)
self._data = None
self.vocabs = vocabs
self.rebucket()
self.inputs = tf.placeholder(dtype=tf.int32, shape=(None,None,None), name='inputs')
self.targets = tf.placeholder(dtype=tf.int32, shape=(None,None,None), name='targets')
self.builder = builder()
#=============================================================
def file_iterator(self, filename):
""""""
with open(filename) as f:
if self.lines_per_buffer > 0:
buff = [[]]
while True:
line = f.readline()
while line:
line = line.strip().split()
if line:
buff[-1].append(line)
else:
if len(buff) < self.lines_per_buffer:
if buff[-1]:
buff.append([])
else:
break
line = f.readline()
if not line:
f.seek(0)
else:
buff = self._process_buff(buff)
yield buff
line = line.strip().split()
if line:
buff = [[line]]
else:
buff = [[]]
else:
buff = [[]]
for line in f:
line = line.strip().split()
if line:
buff[-1].append(line)
else:
if buff[-1]:
buff.append([])
if buff[-1] == []:
buff.pop()
buff = self._process_buff(buff)
while True:
yield buff
#=============================================================
def _process_buff(self, buff):
""""""
words, tags, rels = self.vocabs
for i, sent in enumerate(buff):
for j, token in enumerate(sent):
word, tag1, tag2, head, rel = token[words.conll_idx], token[tags.conll_idx[0]], token[tags.conll_idx[1]], token[6], token[rels.conll_idx]
buff[i][j] = (word,) + words[word] + tags[tag1] + tags[tag2] + (int(head),) + rels[rel]
sent.insert(0, ('root', Vocab.ROOT, Vocab.ROOT, Vocab.ROOT, Vocab.ROOT, 0, Vocab.ROOT))
return buff
#=============================================================
def reset(self, sizes):
""""""
self._data = []
self._targets = []
self._metabucket.reset(sizes)
return
#=============================================================
def rebucket(self):
""""""
buff = self._file_iterator.next()
len_cntr = Counter()
for sent in buff:
len_cntr[len(sent)] += 1
self.reset(KMeans(self.n_bkts, len_cntr).splits)
for sent in buff:
self._metabucket.add(sent)
self._finalize()
return
#=============================================================
def _finalize(self):
""""""
self._metabucket._finalize()
return
#=============================================================
def get_minibatches(self, batch_size, input_idxs, target_idxs, shuffle=True):
""""""
minibatches = []
for bkt_idx, bucket in enumerate(self._metabucket):
if batch_size == 0:
n_splits = 1
#elif not self.minimize_pads:
# n_splits = max(len(bucket) // batch_size, 1)
# if bucket.size > 100:
# n_splits *= 2
else:
n_tokens = len(bucket) * bucket.size
n_splits = max(n_tokens // batch_size, 1)
if shuffle:
range_func = np.random.permutation
else:
range_func = np.arange
arr_sp = np.array_split(range_func(len(bucket)), n_splits)
for bkt_mb in arr_sp:
minibatches.append( (bkt_idx, bkt_mb) )
if shuffle:
np.random.shuffle(minibatches)
for bkt_idx, bkt_mb in minibatches:
data = self[bkt_idx].data[bkt_mb]
sents = self[bkt_idx].sents[bkt_mb]
maxlen = np.max(np.sum(np.greater(data[:,:,0], 0), axis=1))
feed_dict = {
self.inputs: data[:,:maxlen,input_idxs],
self.targets: data[:,:maxlen,target_idxs]
}
yield feed_dict, sents
#=============================================================
def get_minibatches2(self, batch_size, input_idxs, target_idxs):
""""""
bkt_lens = np.empty(len(self._metabucket))
for i, bucket in enumerate(self._metabucket):
bkt_lens[i] = len(bucket)
total_sents = np.sum(bkt_lens)
bkt_probs = bkt_lens / total_sents
n_sents = 0
while n_sents < total_sents:
n_sents += batch_size
bkt = np.random.choice(self._metabucket._buckets, p=bkt_probs)
data = bkt.data[np.random.randint(len(bkt), size=batch_size)]
if bkt.size > 100:
for data_ in np.array_split(data, 2):
feed_dict = {
self.inputs: data_[:,:,input_idxs],
self.targets: data_[:,:,target_idxs]
}
yield feed_dict
else:
feed_dict = {
self.inputs: data[:,:,input_idxs],
self.targets: data[:,:,target_idxs]
}
yield feed_dict
#=============================================================
@property
def n_bkts(self):
if self._train:
return super(Dataset, self).n_bkts
else:
return super(Dataset, self).n_valid_bkts
#=============================================================
def __getitem__(self, key):
return self._metabucket[key]
def __len__(self):
return len(self._metabucket)