-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy paththeano_funcs.py
74 lines (55 loc) · 1.84 KB
/
theano_funcs.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
import theano
import theano.tensor as T
from lasagne.layers import get_output
from lasagne.layers import get_all_params
from lasagne.updates import adagrad
from theano.tensor.nnet import categorical_crossentropy
def create_train_func(layers, lr=0.01):
# dims: batch, sequence, vocabulary
X = T.tensor3('X')
X_batch = T.tensor3('X_batch')
# dims: target
y = T.ivector('y')
y_batch = T.ivector('y_batch')
y_hat = get_output(layers['l_out'], X, deterministic=False)
train_loss = T.mean(categorical_crossentropy(y_hat, y), axis=0)
params = get_all_params(layers['l_out'], trainable=True)
updates = adagrad(train_loss, params, lr)
train_func = theano.function(
inputs=[theano.In(X_batch), theano.In(y_batch)],
outputs=train_loss,
updates=updates,
givens={
X: X_batch,
y: y_batch,
},
)
return train_func
def create_sample_func(layers):
X = T.tensor3('X')
X_batch = T.tensor3('X_batch')
y_hat = get_output(layers['l_out'], X, deterministic=True)
sample_func = theano.function(
inputs=[theano.In(X_batch)],
outputs=y_hat,
updates=None,
givens={
X: X_batch,
},
)
return sample_func
def test_create_train_func():
import numpy as np
from char_rnn import build_model
batch_size, sequence_length, vocab_size = 16, 32, 64
layers = build_model((None, None, vocab_size), 128, vocab_size, 10.)
train_func = create_train_func(layers)
X = np.zeros((batch_size, sequence_length, vocab_size), dtype=np.float32)
X[:, :, 0] = 1.
y = np.random.randint(0, vocab_size, batch_size).astype(np.int32)
print('testing train_func')
loss = train_func(X, y)
print('loss = %.6f' % (loss))
print('done')
if __name__ == '__main__':
test_create_train_func()