forked from lisovskey/filmach
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate.py
28 lines (24 loc) · 948 Bytes
/
generate.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
import numpy as np
def sample(preds, diffusion):
"""
Make a diffusion of chars predictions and take most possible
"""
preds = [pred + np.random.uniform(high=diffusion) for pred in preds]
return np.argmax(preds)
def sample_text(model, length, chars_indices, indices_chars, sequence,
seq_len, diffusion):
"""
Yield predicted char after `sequence`.
Shift `sequence` one char further `length` times.
"""
x_pred = np.zeros((1, seq_len, len(chars_indices)))
for i, char in enumerate(sequence):
x_pred[0, i, chars_indices[char]] = 1.0
for _ in range(length):
preds = model.predict(x_pred, verbose=0)[0]
next_index = sample(preds, diffusion)
next_char = indices_chars[next_index]
yield next_char
x_pred = np.roll(x_pred, -1, axis=1)
x_pred[0, -1] = np.zeros(len(chars_indices))
x_pred[0, -1, chars_indices[next_char]] = 1.0