-
Notifications
You must be signed in to change notification settings - Fork 0
/
Helpers.py
221 lines (163 loc) · 7.53 KB
/
Helpers.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
216
217
218
219
220
221
import numpy as np
class Iris(object):
def shuffle_records(features, labels):
idx = np.arange(features.shape[0])
np.random.seed(42)
np.random.shuffle(idx)
return features[idx], labels[idx]
def convert_to_one_hot(vector, num_classes):
result = np.zeros(shape=(len(vector), num_classes))
result[np.arange(len(vector)), vector] = 1
return result.astype(int)
class Exquisite_Corpse(object):
def __init__(self):
with open('data/frankenstein.txt') as f:
text = f.read()
sentences = re.split(r' *[\.\!\?][\'"\)\]]* *', text)
for i in range(len(sentences)):
sentences[i] = sentences[i].replace("\n", " ") + "."
self.sentences = sentences[1000:2500]
def generate_response(model, lexicon_lookup, idx_seq):
end_of_sent_tokens = [".", "!","/",";","?",":"]
generated_ending = []
if len(idx_seq) == 0:
return [3]
for word in idx_seq:
p_next_word = model.predict(np.array(word)[None, None])[0,0]
while not generated_ending or lexicon_lookup[next_word] not in end_of_sent_tokens:
next_word = np.random.choice(a=p_next_word.shape[-1], p=p_next_word)
if next_word != 1:
generated_ending.append(next_word)
p_next_word = model.predict(np.array(next_word)[None, None])[0,0]
model.reset_states()
generated_ending = " ".join( [lexicon_lookup[word] if word in lexicon_lookup else "" for word in generated_ending] )
return generated_ending
def text_to_tokens(lines, encoder):
tokens = [ [word.lower_ for word in encoder(line)] for line in lines]
return tokens
def make_lexicon(token_seqs, min_freq=4):
token_counts = {}
for seq in token_seqs:
for token in seq:
if token in token_counts:
token_counts[token] += 1
else:
token_counts[token] = 1
lexicon = [token for token, count in token_counts.items() if count >= min_freq]
lexicon = {token:idx + 2 for idx, token in enumerate(lexicon)}
lexicon[u'<UNK>'] = 1
lexicon_size = len(lexicon)
print("lexicon sample ({} total items):".format(len(lexicon)))
print(list(lexicon.items())[400:405])
return lexicon
def get_lexicon_lookup(lexicon):
lexicon_lookup = { idx: lexicon_item for lexicon_item, idx in lexicon.items()}
lexicon_lookup[0] = ""
return lexicon_lookup
def tokens_to_ids(all_tokens, lexicon):
ids = [[lexicon[token] if token in lexicon else lexicon['<UNK>'] for token in token_line] for token_line in all_tokens]
return ids
def test(self):
answer = self.test()
return answer * answer
def test2():
return 20
def generate_text(self, model, length, vocab_size, ix_to_char):
# starting with random character
ix = [np.random.randint(vocab_size)]
y_char = [ix_to_char[ix[-1]]]
X = np.zeros((1, length, vocab_size))
for i in range(length):
# appending the last predicted character to sequence
X[0, i, :][ix[-1]] = 1
print(ix_to_char[ix[-1]], end="")
ix = np.argmax(model.predict(X[:, :i+1, :])[0], 1)
y_char.append(ix_to_char[ix[-1]])
return ('').join(y_char)
def load_data(self, data_dir, seq_length):
data = open(data_dir, 'r').read()
chars = list(set(data))
VOCAB_SIZE = len(chars)
print('Data length: {} characters'.format(len(data)))
print('Vocabulary size: {} characters'.format(VOCAB_SIZE))
ix_to_char = {ix:char for ix, char in enumerate(chars)}
char_to_ix = {char:ix for ix, char in enumerate(chars)}
print(type(seq_length))
print(type(VOCAB_SIZE))
X = np.zeros( (int(len(data)/seq_length), seq_length, VOCAB_SIZE))
y = np.zeros( (int(len(data)/seq_length), seq_length, VOCAB_SIZE))
for i in range(0, int(len(data)/seq_length) ):
X_sequence = data[i*seq_length:(i+1)*seq_length]
X_sequence_ix = [char_to_ix[value] for value in X_sequence]
input_sequence = np.zeros((seq_length, VOCAB_SIZE))
for j in range(seq_length):
input_sequence[j][X_sequence_ix[j]] = 1.
X[i] = input_sequence
y_sequence = data[i*seq_length+1:(i+1)*seq_length+1]
y_sequence_ix = [char_to_ix[value] for value in y_sequence]
target_sequence = np.zeros((seq_length, VOCAB_SIZE))
for j in range(seq_length):
target_sequence[j][y_sequence_ix[j]] = 1.
y[i] = target_sequence
return X, y, VOCAB_SIZE, ix_to_char
class Style_Transfer(object):
def __init__(self,
K,
width,
height):
self.K = K
self.width = width
self.height = height
self.VGG_MEAN = [103.939, 116.779, 123.68]
self.adjustments_needed = None
self.loss_value = None
self.gradient_values = None
def to_VGG_format(self, _image):
_image = np.expand_dims(_image, axis=0)
_image[:,:,:,0] -= self.VGG_MEAN[0]
_image[:,:,:,1] -= self.VGG_MEAN[1]
_image[:,:,:,2] -= self.VGG_MEAN[2]
_image = _image[:,:,:,::-1]
return _image
def from_VGG_format(self, _x):
_x = _x.reshape( (self.width, self.height, 3) )
_x = _x[:,:, ::-1]
_x[:, :, 0] += self.VGG_MEAN[0]
_x[:, :, 1] += self.VGG_MEAN[1]
_x[:, :, 2] += self.VGG_MEAN[2]
_x = np.clip(_x, 0, 255).astype('uint8')
return _x
def content_loss(self, _content, _result):
return self.K.sum(self.K.square(_result - _content))
def gram_matrix(self, x):
features = self.K.batch_flatten(self.K.permute_dimensions(x, (2, 0, 1)))
gram = self.K.dot(features, self.K.transpose(features))
return gram
def style_loss(self, _style, _result):
S = self.gram_matrix(_style)
C = self.gram_matrix(_result)
channels = 3
size = 256 * 256
return self.K.sum(self.K.square(S - C)) / (4. * (channels ** 2) * (size ** 2))
def noisy_loss(self, x):
a = self.K.square(x[:, :self.width-1, :self.height-1, :] - x[:, 1:, :self.height-1, :])
b = self.K.square(x[:, :self.width-1, :self.height-1, :] - x[:, :self.width-1, 1:, :])
return self.K.sum(self.K.pow(a + b, 1.25))
def setup_gradients_from_loss_chain(self, loss_chain, result_image):
gradients = self.K.gradients(loss_chain, result_image)
loss_and_gradients = [loss_chain] + gradients
# these adjustments_needed is the derivative of the loss calculations
self.adjustments_needed = self.K.function([result_image], loss_and_gradients)
def loss(self, x):
assert self.loss_value is None
x = x.reshape( (1, self.width, self.height, 3) )
tweaks = self.adjustments_needed([x])
self.loss_value = tweaks[0]
self.gradient_values = tweaks[1].flatten().astype('float64')
return self.loss_value
def gradients(self, x):
assert self.loss_value is not None
gradient_values = np.copy(self.gradient_values)
self.loss_value = None
self.gradient_values = None
return gradient_values