forked from zackchase/PyRNN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
48 lines (35 loc) · 1.07 KB
/
run.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
from char_rnn import CharRNN
from lib import one_hot, one_hot_to_string, floatX
import numpy as np
import theano
import theano.tensor as T
import sys
import random
###############################
#
# Prepare the data
#
###############################
# f = open("../data/reuters21578/reut2-002.sgm")
f = open("../data/tinyshakespeare/input.txt")
text = f.read()
f.close()
rnn = CharRNN()
seq_len = 150
def train(eta, iters):
for it in xrange(iters):
i = random.randint(0, len(text)/seq_len)
j = i * seq_len
X = text[j:(j+seq_len)]
Y = text[(j+1):(j+1+seq_len)]
print "iteration: %s, cost: %s" % (str(it), str(rnn.train(one_hot(X), one_hot(Y), eta, 1.0)))
def infer_stochastic(rnn, k, temperature, start_char=" "):
x = [one_hot(start_char).flatten()]
for i in xrange(k):
probs = rnn.predict_char(x, temperature)
p = np.asarray(probs[0], dtype="float64")
p /= p.sum()
sample = np.random.multinomial(1, p)
sys.stdout.write(one_hot_to_string(sample))
x = [sample]
rnn.reset_state()