-
Notifications
You must be signed in to change notification settings - Fork 0
/
ae.py
137 lines (104 loc) · 6.23 KB
/
ae.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
import os
import sys
import tensorflow as tf
import ops2
sys.path.append(os.getcwd())
class AttrDict(dict):
__getattr__ = dict.__getitem__
__setattr__ = dict.__setitem__
class NPCS_AE:
def __init__(self, config):
self.X, self.X_val, self.d_dim, self.code_dim = ops2.get_data(config.data, config.fill_points, 1.0, config)
self.config = config
self.limit = 1.0
self.ev = None
def positive(self,l):
l = tf.assign(l,self.limit)
return l
def neg(self,l,delta_l):
l = tf.assign(l, l+delta_l )
l = tf.cond(l >= 1.0, true_fn= lambda: self.positive(l),false_fn= lambda: l )
return l
def update_l(self,l,delta_l):
l = tf.cond(l >= 1.0, true_fn= lambda: self.positive(l), false_fn= lambda: self.neg(l,delta_l) )
return l
def autoencoder8_svd(self,x,l):
with tf.variable_scope('network'):
encoder = ops2.activation(self.config.use_act, ops2.etlinear2(x,200,self.ev,scope ='encoder1'),l )
encoder = ops2.activation(self.config.use_act, ops2.etlinear2(encoder,100,self.ev,scope ='encoder2'),l )
encoder = ops2.activation(self.config.use_act, ops2.etlinear2(encoder,50,self.ev,scope ='encoder3'),l)
code = ops2.etlinear2(encoder,2,self.ev,scope ='code')
decoder = ops2.activation(self.config.use_act, ops2.etlinear2(code,50,self.ev,scope ='decoder3'),l)
decoder = ops2.activation(self.config.use_act, ops2.etlinear2(decoder,100,self.ev,scope ='decoder2'),l)
decoder = ops2.activation(self.config.use_act, ops2.etlinear2(decoder,200,self.ev,scope ='decoder1'),l)
out = ops2.etlinear2(decoder, self.d_dim, self.ev, scope='output')
loss = tf.reduce_mean(tf.square(x - out))
return loss, out, code
def autoencoder16_svd(self,x,l):
with tf.variable_scope('network'):
encoder = ops2.activation(self.config.use_act, ops2.stlinear2(x,500,self.ev,scope ='encoder1'),l )
encoder = ops2.activation(self.config.use_act, ops2.stlinear2(encoder,200,self.ev,scope ='encoder2'),l )
encoder = ops2.activation(self.config.use_act, ops2.stlinear2(encoder,100,self.ev,scope ='encoder3'),l)
encoder = ops2.activation(self.config.use_act, ops2.stlinear2(encoder,50,self.ev,scope ='encoder4'),l)
encoder = ops2.activation(self.config.use_act, ops2.stlinear2(encoder,50,self.ev,scope ='encoder5'),l)
encoder = ops2.activation(self.config.use_act, ops2.stlinear2(encoder,5,self.ev,scope ='encoder6'),l)
encoder = ops2.activation(self.config.use_act, ops2.stlinear2(encoder,5,self.ev,scope ='encoder7'),l)
code = ops2.stlinear2(encoder,2,self.ev,scope ='code')
decoder = ops2.activation(self.config.use_act, ops2.stlinear2(code,5,self.ev,scope ='decoder7'),l)
decoder = ops2.activation(self.config.use_act, ops2.stlinear2(decoder,5,self.ev,scope ='decoder6'),l)
decoder = ops2.activation(self.config.use_act, ops2.stlinear2(decoder,50,self.ev,scope ='decoder5'),l)
decoder = ops2.activation(self.config.use_act, ops2.stlinear2(decoder,50,self.ev,scope ='decoder4'),l)
decoder = ops2.activation(self.config.use_act, ops2.stlinear2(decoder,100,self.ev,scope ='decoder3'),l)
decoder = ops2.activation(self.config.use_act, ops2.stlinear2(decoder,200,self.ev,scope ='decoder2'),l)
decoder = ops2.activation(self.config.use_act, ops2.stlinear2(decoder,500,self.ev,scope ='decoder1'),l)
out = ops2.stlinear2(decoder, self.d_dim, self.ev, scope='output')
loss = tf.reduce_mean(tf.square(x - out))
return loss, out, code
def master_graph(self):
tf.reset_default_graph()
x = tf.placeholder(tf.float32, shape=[None,self.d_dim], name='x')
l = tf.Variable(self.config.l_init, dtype=tf.float32,trainable=False,name='lamda')
delta_l = tf.placeholder(dtype=tf.float32,shape=[],name='delta_l' )
l_prev = tf.placeholder( dtype=tf.float32,shape=[],name='lamda_prev')
omega = tf.placeholder(dtype=tf.float32,shape=[],name='omega' )
lnorm = tf.placeholder(dtype=tf.float32,shape=[],name='lnorm' )
if self.config.svd == True:
self.X = ops2.center_data(self.X)
if self.config.depth == 8:
self.ev = ops2.get_v_n(self.X)
with tf.variable_scope('current'):
loss_c, output_c, code = self.autoencoder8_svd(x,l)
with tf.variable_scope('prev'):
loss_p, output_p, _ = self.autoencoder8_svd(x,l)
with tf.variable_scope('prev2'):
loss_p2, output_p2, _ = self.autoencoder8_svd(x,l)
else:
self.ev = ops2.get_v_n16(self.X)
with tf.variable_scope('current'):
loss_c, output_c, code = self.autoencoder16_svd(x,l)
with tf.variable_scope('prev'):
loss_p, output_p, _ = self.autoencoder16_svd(x,l)
with tf.variable_scope('prev2'):
loss_p2, output_p2, _ = self.autoencoder16_svd(x,l)
if self.config.opt == 'adam':
optimizer = tf.train.AdamOptimizer(self.config.lr)
elif self.config.opt == 'rmsprop':
optimizer = tf.train.RMSPropOptimizer(self.config.lr)
else:
optimizer = tf.train.GradientDescentOptimizer(self.config.lr)
grads_and_vars = optimizer.compute_gradients(loss_c)
opt = optimizer.apply_gradients(grads_and_vars)
grads, _ = list(zip(*grads_and_vars))
norms = tf.global_norm(grads)
#lambda update NPC
l_new = self.update_l(l,delta_l)
#secant update for lambda NPCS
A = tf.trainable_variables(scope='current/network')
B = tf.trainable_variables(scope='prev/network')
C = tf.trainable_variables(scope='prev2/network')
copy_op = ops2.copy_g(A,B)
copy_op1 = ops2.copy_g(A,C)
copy_op2 = ops2.copy_g(C,B)
diff_op = ops2.diff_l(A,B,self.config)
secant_op = ops2.secant_l(A,B,self.config)
return AttrDict(locals())