-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlenet.py
More file actions
59 lines (51 loc) · 1.92 KB
/
Copy pathlenet.py
File metadata and controls
59 lines (51 loc) · 1.92 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from layers import *
def base_softmax(x, y, training, name='base_softmax', reuse=None):
x = tf.reshape(x, [-1, 1, 28, 28])
x = conv(x, 20, 5, name=name+'/conv1', reuse=reuse)
x = relu(x)
x = pool(x, name=name+'/pool1')
x = conv(x, 50, 5, name=name+'/conv2', reuse=reuse)
x = relu(x)
x = pool(x, name=name+'/pool2')
x = flatten(x)
h = dense(x, 500, activation=relu, name=name+'/dense', reuse=reuse)
o = dense(h, 10, name=name+'/logits', reuse=reuse)
net = {}
net['cent'] = cross_entropy(exp(o), y)
net['acc'] = accuracy(exp(o), y)
all_vars = tf.get_collection('variables', scope=name)
net['weights'] = all_vars
net['wd'] = weight_decay(1e-4, var_list=net['weights'])
return net
def dropmax(x, y, training, name='dropmax', reuse=None):
x = tf.reshape(x, [-1, 1, 28, 28])
x = conv(x, 20, 5, name=name+'/conv1', reuse=reuse)
x = relu(x)
x = pool(x, name=name+'/pool1')
x = conv(x, 50, 5, name=name+'/conv2', reuse=reuse)
x = relu(x)
x = pool(x, name=name+'/pool2')
x = flatten(x)
h = dense(x, 500, activation=relu, name=name+'/dense', reuse=reuse)
# dropmax branches
o = dense(h, 10, name=name+'/logits', reuse=reuse)
ph = dense(h, 10, name=name+'/ph_branch', reuse=reuse)
rh = dense(h, 10, name=name+'/rh_branch', reuse=reuse)
qh = tf.stop_gradient(ph) + rh
p = sigmoid(ph)
r = sigmoid(rh)
q = sigmoid(qh)
# sampling the dropout masks
z = genmask(q, y)
net = {}
net['cent'] = cross_entropy(((z if training else p)+eps)*exp(o), y)
net['acc'] = accuracy((p+eps)*exp(o), y)
all_vars = tf.get_collection('variables', scope=name)
net['weights'] = all_vars
net['wd'] = weight_decay(1e-4,
var_list=[v for v in all_vars if 'branch' not in v.name])
# dropmax modules
net['kl'] = kl_divergence(p, q, y)
net['aux'] = auxloss(r, y)
net['neg_ent'] = neg_entropy(p)
return net