-
Notifications
You must be signed in to change notification settings - Fork 126
/
Copy pathutils.py
138 lines (119 loc) · 3.22 KB
/
utils.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
"""
Helper functions for multimodal-ranking
"""
import theano
import theano.tensor as tensor
import numpy
from collections import OrderedDict
def zipp(params, tparams):
"""
Push parameters to Theano shared variables
"""
for kk, vv in params.iteritems():
tparams[kk].set_value(vv)
def unzip(zipped):
"""
Pull parameters from Theano shared variables
"""
new_params = OrderedDict()
for kk, vv in zipped.iteritems():
new_params[kk] = vv.get_value()
return new_params
def itemlist(tparams):
"""
Get the list of parameters.
Note that tparams must be OrderedDict
"""
return [vv for kk, vv in tparams.iteritems()]
def _p(pp, name):
"""
Make prefix-appended name
"""
return '%s_%s'%(pp, name)
def init_tparams(params):
"""
Initialize Theano shared variables according to the initial parameters
"""
tparams = OrderedDict()
for kk, pp in params.iteritems():
tparams[kk] = theano.shared(params[kk], name=kk)
return tparams
def load_params(path, params):
"""
Load parameters
"""
pp = numpy.load(path)
for kk, vv in params.iteritems():
if kk not in pp:
warnings.warn('%s is not in the archive'%kk)
continue
params[kk] = pp[kk]
return params
def ortho_weight(ndim):
"""
Orthogonal weight init, for recurrent layers
"""
W = numpy.random.randn(ndim, ndim)
u, s, v = numpy.linalg.svd(W)
return u.astype('float32')
def norm_weight(nin,nout=None, scale=0.1, ortho=True):
"""
Uniform initalization from [-scale, scale]
If matrix is square and ortho=True, use ortho instead
"""
if nout == None:
nout = nin
if nout == nin and ortho:
W = ortho_weight(nin)
else:
W = numpy.random.uniform(low=-scale, high=scale, size=(nin, nout))
return W.astype('float32')
def xavier_weight(nin,nout=None):
"""
Xavier init
"""
if nout == None:
nout = nin
r = numpy.sqrt(6.) / numpy.sqrt(nin + nout)
W = numpy.random.rand(nin, nout) * 2 * r - r
return W.astype('float32')
def tanh(x):
"""
Tanh activation function
"""
return tensor.tanh(x)
def linear(x):
"""
Linear activation function
"""
return x
def l2norm(X):
"""
Compute L2 norm, row-wise
"""
norm = tensor.sqrt(tensor.pow(X, 2).sum(1))
X /= norm[:, None]
return X
def concatenate(tensor_list, axis=0):
"""
Alternative implementation of `theano.tensor.concatenate`.
"""
concat_size = sum(tt.shape[axis] for tt in tensor_list)
output_shape = ()
for k in range(axis):
output_shape += (tensor_list[0].shape[k],)
output_shape += (concat_size,)
for k in range(axis + 1, tensor_list[0].ndim):
output_shape += (tensor_list[0].shape[k],)
out = tensor.zeros(output_shape)
offset = 0
for tt in tensor_list:
indices = ()
for k in range(axis):
indices += (slice(None),)
indices += (slice(offset, offset + tt.shape[axis]),)
for k in range(axis + 1, tensor_list[0].ndim):
indices += (slice(None),)
out = tensor.set_subtensor(out[indices], tt)
offset += tt.shape[axis]
return out