forked from sherjilozair/char-rnn-tensorflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
147 lines (122 loc) · 5.23 KB
/
utils.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
import codecs
import os
import random
from six.moves import cPickle
import numpy as np
class TextLoader:
def __init__(self, name, shared_vocabulary, batch_size, seq_length):
self.tensor = shared_vocabulary.tensors[name]
self.split_mode = isinstance(self.tensor, list)
self.batch_size = batch_size
self.seq_length = seq_length
self.partition_size = batch_size * seq_length
self.name = name
self.num_batches = 0
self.x_batches = None
self.y_batches = None
self.pointer = None
self.create_batches()
self.reset_batch_pointer()
def create_batches(self):
if self.split_mode:
tensor = []
indices = list(range(len(self.tensor)))
random.shuffle(indices)
for i in indices:
tensor.extend(self.tensor[i])
tensor = np.array(tensor)
else:
tensor = self.tensor
# return zero batches if tensor is empty
if tensor.size == 0:
self.x_batches = np.zeros((self.batch_size, 0))
self.y_batches = np.zeros((self.batch_size, 0))
return
# create batches
self.num_batches = int((tensor.size - 1) / self.partition_size)
# When the data (tensor) is too small,
# let's give them a better error message
assert self.num_batches != 0, "Not enough data in %s. Make batch_size smaller." % self.name
clipped_tensor = tensor[:self.num_batches * self.partition_size + 1]
x_data = clipped_tensor[:-1]
y_data = np.copy(clipped_tensor[1:])
self.x_batches = np.split(x_data.reshape(self.batch_size, -1),
self.num_batches, 1)
self.y_batches = np.split(y_data.reshape(self.batch_size, -1),
self.num_batches, 1)
def next_batch(self):
x, y = self.x_batches[self.pointer], self.y_batches[self.pointer]
self.pointer += 1
return x, y
def reset_batch_pointer(self):
self.pointer = 0
class SharedVocabulary:
def __init__(self, data_dir, names, encoding='utf-8'):
self.chars = set()
self.vocab = None
self.vocab_size = 0
self.data = dict()
self.tensors = dict()
self.encoding = encoding
self.split_mode = False
self.vocab_file = os.path.join(data_dir, "vocab.pkl")
self.tensor_file = os.path.join(data_dir, "data.npy")
if not (os.path.exists(self.vocab_file) and os.path.exists(self.tensor_file)):
for name in names:
input_file = os.path.join(data_dir, name + ".txt")
input_folder = os.path.join(data_dir, name + "s")
if os.path.exists(input_file):
print("reading text file")
self.read_file(name, input_file)
elif os.path.exists(input_folder):
print("reading text files")
self.split_mode = True
self.read_folder(name, input_folder)
elif name == 'test':
print('no test data found. ignoring')
self.data[name] = None
else:
raise EnvironmentError('neither {} nor {} exist'.format(input_file, input_folder))
self.process_data()
else:
with open(self.vocab_file, 'rb') as f:
self.chars, self.vocab_size, self.vocab, self.split_mode = cPickle.load(f)
with open(self.tensor_file, 'rb') as f:
self.tensors = cPickle.load(f)
def read_file(self, name, input_file):
with codecs.open(input_file, "r", encoding=self.encoding) as f:
data = f.read()
self.chars.update(data)
self.data[name] = data
def read_folder(self, name, input_folder):
data = []
# load multiple files with start-of-text and end-of-text chars
for filename in os.listdir(input_folder):
with codecs.open(os.path.join(input_folder, filename),
"r", encoding=self.encoding) as f:
data.append('\x02' + f.read() + '\x03')
self.chars.update(data[-1])
self.data[name] = data
def process_data(self):
self.chars = list(self.chars)
self.vocab_size = len(self.chars)
self.vocab = dict(zip(self.chars, range(len(self.chars))))
with open(self.vocab_file, 'wb') as f:
cPickle.dump((self.chars, self.vocab_size, self.vocab, self.split_mode), f)
for name, data in self.data.items():
if isinstance(data, list):
# split mode:
tensors = []
for single_example in data:
tensors.append(np.array(list(map(self.vocab.get, single_example))))
self.tensors[name] = tensors
elif data is None:
self.tensors[name] = np.array([])
else:
if self.split_mode:
data.insert(0, '\x02')
data.append('\x03')
self.tensors[name] = np.array(list(map(self.vocab.get, data)))
# save tensors
with open(self.tensor_file, 'wb') as f:
cPickle.dump(self.tensors, f)