|
0 |
import os
|
|
1 |
#import collections
|
|
2 |
from six.moves import cPickle
|
|
3 |
import numpy as np
|
|
4 |
from word2vec_helper import Word2Vec
|
|
5 |
import math
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
class DataLoader():
|
|
10 |
def __init__(self, data_dir, batch_size,seq_max_length,w2v,data_type):
|
|
11 |
self.data_dir = data_dir
|
|
12 |
self.batch_size = batch_size
|
|
13 |
self.seq_max_length = seq_max_length
|
|
14 |
self.w2v = w2v
|
|
15 |
self.trainingSamples = []
|
|
16 |
self.validationSamples = []
|
|
17 |
self.testingSamples = []
|
|
18 |
self.train_frac = 0.85
|
|
19 |
self.valid_frac = 0.05
|
|
20 |
|
|
21 |
self.load_corpus(self.data_dir)
|
|
22 |
|
|
23 |
if data_type == 'train':
|
|
24 |
self.create_batches(self.trainingSamples)
|
|
25 |
elif data_type == 'test':
|
|
26 |
self.create_batches(self.testingSamples)
|
|
27 |
elif data_type == 'valid':
|
|
28 |
self.create_batches(self.validationSamples)
|
|
29 |
|
|
30 |
self.reset_batch_pointer()
|
|
31 |
|
|
32 |
def _print_stats(self):
|
|
33 |
print('Loaded {}: training samples:{} ,validationSamples:{},testingSamples:{}'.format(
|
|
34 |
self.data_dir, len(self.trainingSamples),len(self.validationSamples),len(self.testingSamples)))
|
|
35 |
|
|
36 |
def load_corpus(self,base_path):
|
|
37 |
"""读/创建 对话数据:
|
|
38 |
在训练文件创建的过程中,由两个文件
|
|
39 |
1. self.fullSamplePath
|
|
40 |
2. self.filteredSamplesPath
|
|
41 |
"""
|
|
42 |
tensor_file = os.path.join(base_path,'poem_ids.txt')
|
|
43 |
print('tensor_file:%s' % tensor_file)
|
|
44 |
|
|
45 |
datasetExist = os.path.isfile(tensor_file)
|
|
46 |
# 如果处理过的对话数据文件不存在,创建数据文件
|
|
47 |
if not datasetExist:
|
|
48 |
print('训练样本不存在。从原始样本数据集创建训练样本...')
|
|
49 |
|
|
50 |
fullSamplesPath = os.path.join(self.data_dir,'poems_edge_split.txt')
|
|
51 |
# 创建/读取原始对话样本数据集: self.trainingSamples
|
|
52 |
print('fullSamplesPath:%s' % fullSamplesPath)
|
|
53 |
self.load_from_text_file(fullSamplesPath)
|
|
54 |
|
|
55 |
else:
|
|
56 |
self.load_dataset(tensor_file)
|
|
57 |
|
|
58 |
self.padToken = self.w2v.ix('<pad>')
|
|
59 |
self.goToken = self.w2v.ix('[')
|
|
60 |
self.eosToken = self.w2v.ix(']')
|
|
61 |
self.unknownToken = self.w2v.ix('<unknown>')
|
|
62 |
|
|
63 |
self._print_stats()
|
|
64 |
# assert self.padToken == 0
|
|
65 |
|
|
66 |
def load_from_text_file(self,in_file):
|
|
67 |
# base_path = 'F:\BaiduYunDownload\chatbot_lecture\lecture2\data\ice_and_fire_zh'
|
|
68 |
# in_file = os.path.join(base_path,'poems_edge.txt')
|
|
69 |
fr = open(in_file, "r",encoding='utf-8')
|
|
70 |
poems = fr.readlines()
|
|
71 |
fr.close()
|
|
72 |
|
|
73 |
print("唐诗总数: %d"%len(poems))
|
|
74 |
# self.seq_max_length = max([len(poem) for poem in poems])
|
|
75 |
# print("seq_max_length: %d"% (self.seq_max_length))
|
|
76 |
|
|
77 |
poem_ids = DataLoader.get_text_idx(poems,self.w2v.vocab_hash,self.seq_max_length)
|
|
78 |
|
|
79 |
# # 后续处理
|
|
80 |
# # 1. 单词过滤,去掉不常见(<=filterVocab)的单词,保留最常见的vocabSize个单词
|
|
81 |
# print('Filtering words (vocabSize = {} and wordCount > {})...'.format(
|
|
82 |
# self.args.vocabularySize,
|
|
83 |
# self.args.filterVocab
|
|
84 |
# ))
|
|
85 |
# self.filterFromFull()
|
|
86 |
|
|
87 |
# 2. 分割数据
|
|
88 |
print('分割数据为 train, valid, test 数据集...')
|
|
89 |
n_samples = len(poem_ids)
|
|
90 |
train_size = int(self.train_frac * n_samples)
|
|
91 |
valid_size = int(self.valid_frac * n_samples)
|
|
92 |
test_size = n_samples - train_size - valid_size
|
|
93 |
|
|
94 |
print('n_samples=%d, train-size=%d, valid_size=%d, test_size=%d' % (
|
|
95 |
n_samples, train_size, valid_size, test_size))
|
|
96 |
self.testingSamples = poem_ids[-test_size:]
|
|
97 |
self.validationSamples = poem_ids[-valid_size-test_size : -test_size]
|
|
98 |
self.trainingSamples = poem_ids[:train_size]
|
|
99 |
|
|
100 |
# 保存处理过的训练数据集
|
|
101 |
print('Saving dataset...')
|
|
102 |
poem_ids_file = os.path.join(self.data_dir,'poem_ids.txt')
|
|
103 |
self.save_dataset(poem_ids_file)
|
|
104 |
|
|
105 |
# 2. utility 函数,使用pickle写文件
|
|
106 |
def save_dataset(self, filename):
|
|
107 |
"""使用pickle保存数据文件。
|
|
108 |
|
|
109 |
数据文件包含词典和对话样本。
|
|
110 |
|
|
111 |
Args:
|
|
112 |
filename (str): pickle 文件名
|
|
113 |
"""
|
|
114 |
with open(filename, 'wb') as handle:
|
|
115 |
data = {
|
|
116 |
'trainingSamples': self.trainingSamples
|
|
117 |
}
|
|
118 |
|
|
119 |
if len(self.validationSamples)>0:
|
|
120 |
data['validationSamples'] = self.validationSamples
|
|
121 |
data['testingSamples'] = self.testingSamples
|
|
122 |
data['maxSeqLen'] = self.seq_max_length
|
|
123 |
|
|
124 |
cPickle.dump(data, handle, -1) # Using the highest protocol available
|
|
125 |
|
|
126 |
# 3. utility 函数,使用pickle读文件
|
|
127 |
def load_dataset(self, filename):
|
|
128 |
"""使用pickle读入数据文件
|
|
129 |
Args:
|
|
130 |
filename (str): pickle filename
|
|
131 |
"""
|
|
132 |
|
|
133 |
print('Loading dataset from {}'.format(filename))
|
|
134 |
with open(filename, 'rb') as handle:
|
|
135 |
data = cPickle.load(handle)
|
|
136 |
self.trainingSamples = data['trainingSamples']
|
|
137 |
|
|
138 |
if 'validationSamples' in data:
|
|
139 |
self.validationSamples = data['validationSamples']
|
|
140 |
self.testingSamples = data['testingSamples']
|
|
141 |
|
|
142 |
print('file maxSeqLen = {}'.format( data['maxSeqLen']))
|
|
143 |
|
|
144 |
|
|
145 |
@classmethod
|
|
146 |
def get_text_idx(text,vocab,max_document_length):
|
|
147 |
text_array = []
|
|
148 |
for i,x in enumerate(text):
|
|
149 |
line = []
|
|
150 |
for j, w in enumerate(x):
|
|
151 |
if (w not in vocab):
|
|
152 |
w = '<unknown>'
|
|
153 |
line.append(vocab[w])
|
|
154 |
text_array.append(line)
|
|
155 |
# else :
|
|
156 |
# print w,'not exist'
|
|
157 |
|
|
158 |
return text_array
|
|
159 |
|
|
160 |
def create_batches(self,samples):
|
|
161 |
|
|
162 |
sample_size = len(samples)
|
|
163 |
self.num_batches = math.ceil(sample_size /self.batch_size)
|
|
164 |
new_sample_size = self.num_batches * self.batch_size
|
|
165 |
|
|
166 |
# Create the batch tensor
|
|
167 |
# x_lengths = [len(sample) for sample in samples]
|
|
168 |
|
|
169 |
x_lengths = []
|
|
170 |
x_seqs = np.ndarray((new_sample_size,self.seq_max_length),dtype=np.int32)
|
|
171 |
y_seqs = np.ndarray((new_sample_size,self.seq_max_length),dtype=np.int32)
|
|
172 |
self.x_lengths = []
|
|
173 |
for i,sample in enumerate(samples):
|
|
174 |
# fill with padding to align batchSize samples into one 2D list
|
|
175 |
x_lengths.append(len(sample))
|
|
176 |
x_seqs[i] = sample + [self.padToken] * (self.seq_max_length - len(sample))
|
|
177 |
|
|
178 |
for i in range(sample_size,new_sample_size):
|
|
179 |
copyi = i - sample_size
|
|
180 |
x_seqs[i] = x_seqs[copyi]
|
|
181 |
x_lengths.append(x_lengths[copyi])
|
|
182 |
|
|
183 |
y_seqs[:,:-1] = x_seqs[:,1:]
|
|
184 |
y_seqs[:,-1] = x_seqs[:,0]
|
|
185 |
x_len_array = np.array(x_lengths)
|
|
186 |
|
|
187 |
|
|
188 |
|
|
189 |
self.x_batches = np.split(x_seqs.reshape(self.batch_size, -1), self.num_batches, 1)
|
|
190 |
self.x_len_batches = np.split(x_len_array.reshape(self.batch_size, -1), self.num_batches, 1)
|
|
191 |
self.y_batches = np.split(y_seqs.reshape(self.batch_size, -1), self.num_batches, 1)
|
|
192 |
|
|
193 |
def next_batch_dynamic(self):
|
|
194 |
x,x_len, y = self.x_batches[self.pointer], self.x_len_batches[self.pointer],self.y_batches[self.pointer]
|
|
195 |
self.pointer += 1
|
|
196 |
return x,x_len, y
|
|
197 |
|
|
198 |
def next_batch(self):
|
|
199 |
x, y = self.x_batches[self.pointer], self.y_batches[self.pointer]
|
|
200 |
self.pointer += 1
|
|
201 |
return x,y
|
|
202 |
|
|
203 |
def reset_batch_pointer(self):
|
|
204 |
self.pointer = 0
|
|
205 |
|
|
206 |
@staticmethod
|
|
207 |
def get_text_idx(text,vocab,max_document_length):
|
|
208 |
max_document_length_without_end = max_document_length - 1
|
|
209 |
text_array = []
|
|
210 |
for i,x in enumerate(text):
|
|
211 |
line = []
|
|
212 |
if len(x) > max_document_length:
|
|
213 |
x_parts = x[:max_document_length_without_end]
|
|
214 |
idx = x_parts.rfind('。')
|
|
215 |
if idx > -1 :
|
|
216 |
x_parts = x_parts[0:idx + 1] + ']'
|
|
217 |
x = x_parts
|
|
218 |
|
|
219 |
for j, w in enumerate(x):
|
|
220 |
# if j >= max_document_length:
|
|
221 |
# break
|
|
222 |
|
|
223 |
if (w not in vocab):
|
|
224 |
w = '<unknown>'
|
|
225 |
line.append(vocab[w])
|
|
226 |
text_array.append(line)
|
|
227 |
# else :
|
|
228 |
# print w,'not exist'
|
|
229 |
|
|
230 |
return text_array
|
|
231 |
|
|
232 |
if __name__ == '__main__':
|
|
233 |
base_path = './data/poem'
|
|
234 |
# poem = '风急云轻鹤背寒,洞天谁道却归难。千山万水瀛洲路,何处烟飞是醮坛。是的'
|
|
235 |
# idx = poem.rfind('。')
|
|
236 |
# poem_part = poem[:idx + 1]
|
|
237 |
w2v_file = os.path.join(base_path, "vectors_poem.bin")
|
|
238 |
w2v = Word2Vec(w2v_file)
|
|
239 |
|
|
240 |
# vect = w2v_model['['][:10]
|
|
241 |
# print(vect)
|
|
242 |
#
|
|
243 |
# vect = w2v_model['春'][:10]
|
|
244 |
# print(vect)
|
|
245 |
|
|
246 |
in_file = os.path.join(base_path,'poems_edge.txt')
|
|
247 |
# fr = open(in_file, "r",encoding='utf-8')
|
|
248 |
# poems = fr.readlines()
|
|
249 |
# fr.close()
|
|
250 |
#
|
|
251 |
#
|
|
252 |
#
|
|
253 |
# print("唐诗总数: %d"%len(poems))
|
|
254 |
#
|
|
255 |
# poem_ids = get_text_idx(poems,w2v.model.vocab_hash,100)
|
|
256 |
# poem_ids_file = os.path.join(base_path,'poem_ids.txt')
|
|
257 |
# with open(poem_ids_file, 'wb') as f:
|
|
258 |
# cPickle.dump(poem_ids, f)
|
|
259 |
|
|
260 |
dataloader = DataLoader(base_path,20,w2v.model,'train')
|
|
261 |
|