-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
37 lines (31 loc) · 1.1 KB
/
utils.py
File metadata and controls
37 lines (31 loc) · 1.1 KB
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
import numpy as np
import theano
import theano.tensor as T
rng = np.random.RandomState(1234)
trng = T.shared_randomstreams.RandomStreams(1234)
def create_shared(X):
return theano.shared(X.astype(theano.config.floatX))
def create_embedding(vocab_size,hid_dim,embedding=None):
if embedding is not None:
return create_shared(embedding.astype(theano.config.floatX))
else:
return create_shared(np.asarray(
np.r_[np.zeros((1,hid_dim)), rng.uniform(low=-1,high=1,size=(vocab_size,hid_dim))]
))
def create_weight(vis_dim,hid_dim):
return create_shared(np.asarray(
rng.uniform(
low=-4 * np.sqrt(6. / (vis_dim + hid_dim)),
high=4 * np.sqrt(6. / (vis_dim + hid_dim)),
size=(vis_dim,hid_dim)
)
))
def create_bias(dim):
return create_shared(np.zeros((dim,)))
def prop(layers,x):
for i, layer in enumerate(layers):
if i == 0:
layer_out = layer.fprop(x)
else:
layer_out = layer.fprop(layer_out)
return layers[-1].h