-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcharacters.py
174 lines (145 loc) · 5.4 KB
/
characters.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
# -*- coding: utf-8 -*-
'''
# An implementation of deep learning for replacing non-alphanumeric characters with space
Input: 'h' 'e' 'l' 'l' 'o' ' ' 'w' 'o' 'r' 'l' 'd' '!' ' ' 'F' 'o' 'o' '-' 'B' 'a' 'r' ' ' 'h' 'e' 'r' 'e' '.'
v v v
Output: 'h' 'e' 'l' 'l' 'o' ' ' 'w' 'o' 'r' 'l' 'd' ' ' ' ' 'F' 'o' 'o' ' ' 'B' 'a' 'r' ' ' 'h' 'e' 'r' 'e' ' '
''' # noqa
from __future__ import print_function
from keras.models import Sequential
from keras import layers
from keras.utils import plot_model
import numpy as np
from six.moves import range
import string
class CharacterTable(object):
"""Given a set of characters:
+ Encode them to a one-hot integer representation
+ Decode the one-hot or integer representation to their character output
+ Decode a vector of probabilities to their character output
"""
def __init__(self, chars):
"""Initialize character table.
# Arguments
chars: Characters that can appear in the input.
"""
self.chars = sorted(set(chars))
self.char_indices = dict((c, i) for i, c in enumerate(self.chars))
self.indices_char = dict((i, c) for i, c in enumerate(self.chars))
def encode(self, C):
"""One-hot encode given character C.
# Arguments
C: character, to be encoded.
"""
x = np.zeros(len(self.chars))
x[self.char_indices[c]] = 1
return x
def decode(self, x, calc_argmax=True):
"""Decode the given vector or 1D array to their character output.
# Arguments
x: A vector or a 2D array of probabilities or one-hot representations;
or a vector of character indices (used with `calc_argmax=False`).
calc_argmax: Whether to find the character index with maximum
probability, defaults to `True`.
"""
if calc_argmax:
x = x.argmax(axis=-1)
return self.indices_char[x]
class colors:
ok = '\033[92m'
fail = '\033[91m'
close = '\033[0m'
# All the printable characters.
chars = string.printable
ctable = CharacterTable(chars)
# Parameters for the model and dataset.
TRAINING_SIZE = len(chars)
def generate_characters():
questions = []
expected = []
for c in chars:
query = c
ans = ' ' if query in string.punctuation else query
questions.append(query)
expected.append(ans)
print('Total addition questions:', len(questions))
return questions, expected
print('Generating data...')
questions, expected = generate_characters()
print('Vectorization...')
x = np.zeros((len(questions), len(chars)), dtype=np.bool)
y = np.zeros((len(questions), len(chars)), dtype=np.bool)
for i, c in enumerate(questions):
x[i] = ctable.encode(c)
for i, c in enumerate(expected):
y[i] = ctable.encode(c)
# Shuffle (x, y) in unison as the later parts of x will almost all be larger
# digits.
indices = np.arange(len(y))
np.random.shuffle(indices)
x = x[indices]
y = y[indices]
# Validation data is the same as training data.
(x_train, x_val) = x, x
(y_train, y_val) = y, y
print('Training Data:')
print(x_train.shape)
print(y_train.shape)
print('Validation Data:')
print(x_val.shape)
print(y_val.shape)
BATCH_SIZE = 50
def model_ff1():
print('Build model...')
epochs = 400
model = Sequential()
model.add(layers.Dense(len(chars), input_shape=(len(chars), ), activation='softmax'))
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
return model, epochs, "ff1-{}b-{}ep".format(BATCH_SIZE, epochs)
def model_ff2():
print('Build model...')
epochs = 100
model = Sequential()
model.add(layers.Dense(len(chars), input_shape=(len(chars), )))
model.add(layers.Dense(len(chars), activation='softmax'))
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
return model, epochs, "ff2-{}b-{}ep".format(BATCH_SIZE, epochs)
def model_ff3():
print('Build model...')
epochs = 80
model = Sequential()
model.add(layers.Dense(len(chars), input_shape=(len(chars), )))
model.add(layers.Dense(len(chars)))
model.add(layers.Dense(len(chars), activation='softmax'))
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
return model, epochs, "ff3-{}b-{}ep".format(BATCH_SIZE, epochs)
model, epochs, name = model_ff1()
# Train the model each generation and show predictions against the validation
# dataset.
for iteration in range(1, epochs):
print()
print('-' * 50)
print('Iteration', iteration)
model.fit(x_train, y_train,
batch_size=BATCH_SIZE,
epochs=1,
validation_data=(x_val, y_val))
# Select 10 samples from the validation set at random so we can visualize
# errors.
for i in range(10):
ind = np.random.randint(0, len(x_val))
rowx, rowy = x_val[np.array([ind])], y_val[np.array([ind])]
preds = model.predict_classes(rowx, verbose=0)
q = ctable.decode(rowx[0])
correct = ctable.decode(rowy[0])
guess = ctable.decode(preds[0], calc_argmax=False)
print('Q', q, ' T', correct, ' G', guess)
model.summary()
model.save(name + '.h5')
plot_model(model, to_file=name + '.png', show_shapes=True)