-
Notifications
You must be signed in to change notification settings - Fork 2
/
util.py
56 lines (40 loc) · 1.82 KB
/
util.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
import numpy as np
import theano
import theano.tensor as T
import matplotlib
matplotlib.use('Agg')
from matplotlib import pyplot as plt
from scipy.misc import imsave
def initialize_weight(n_vis, n_hid, W_name, numpy_rng, rng_dist):
if 'uniform' in rng_dist:
W = numpy_rng.uniform(low=-np.sqrt(6. / (n_vis + n_hid)),\
high=np.sqrt(6. / (n_vis + n_hid)),
size=(n_vis, n_hid)).astype(theano.config.floatX)
elif rng_dist == 'normal':
W = 0.01 * numpy_rng.normal(size=(n_vis, n_hid)).astype(theano.config.floatX)
return theano.shared(value = W, name=W_name, borrow=True)
'''decaying learning rate'''
def get_epsilon(epsilon, n, i):
return float(epsilon / ( 1 + i/float(n)))
def broadcasted_switch(a, b, c):
return T.switch(a.dimshuffle(0, 1, 'x'), b, c)
def transNorm(transM, vec):
transN = T.zeros_like(vec)
transN = T.set_subtensor(transN[:,:,0], vec[:,:,0] * transM[0][0] \
+ vec[:,:,1] * transM[1][0] + vec[:,:,2] * transM[2][0])
transN = T.set_subtensor(transN[:,:,1], vec[:,:,0] * transM[0][1] \
+ vec[:,:,1] * transM[1][1] + vec[:,:,2] * transM[2][1])
transN = T.set_subtensor(transN[:,:,2], vec[:,:,0] * transM[0][2] \
+ vec[:,:,1] * transM[1][2] + vec[:,:,2] * transM[2][2])
return transN
def drawWithMarkers(fname, im):
fig = plt.figure()
ax = fig.add_subplot(111)
ax.imshow(im, interpolation='nearest')
ax.add_patch(plt.Rectangle((85-3, 90-3), 6, 6, color='red',
linewidth=2, fill=False))
ax.add_patch(plt.Rectangle((90-3, 50-3), 6, 6, color='red',
linewidth=2, fill=False))
fig.savefig(fname, bbox_inches='tight', pad_inches=0)
def draw(fname, im):
imsave(fname, im)