-
Notifications
You must be signed in to change notification settings - Fork 25
/
model.py
executable file
·186 lines (133 loc) · 4.55 KB
/
model.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import math
import tensorflow as tf
def ae(input_size, code_size,
corruption=0.0, tight=False,
enc=tf.nn.tanh, dec=tf.nn.tanh):
"""
Autoencoder model: input_size -> code_size -> input_size
Supports tight weights and corruption.
"""
# Define data input placeholder
x = tf.placeholder(tf.float32, [None, input_size])
if corruption > 0.0:
# Corrupt data based on random sampling
_x = tf.multiply(x, tf.cast(tf.random_uniform(shape=tf.shape(x),
minval=0,
maxval=1 - corruption,
dtype=tf.float32), tf.float32))
else:
_x = x
# Initialize encoder bias
b_enc = tf.Variable(tf.zeros([code_size]))
# Initialize encoder weights using Glorot method
W_enc = tf.Variable(tf.random_uniform(
[input_size, code_size],
-6.0 / math.sqrt(input_size + code_size),
6.0 / math.sqrt(input_size + code_size))
)
# Compute activation for encoding
encode = tf.matmul(_x, W_enc) + b_enc
if enc is not None:
encode = enc(encode)
# Initialize decoder bias
b_dec = tf.Variable(tf.zeros([input_size]))
if tight:
# Tightening using encoder weights
W_dec = tf.transpose(W_enc)
else:
# Initialize decoder weights using Glorot method
W_dec = tf.Variable(tf.random_uniform(
[code_size, input_size],
-6.0 / math.sqrt(code_size + input_size),
6.0 / math.sqrt(code_size + input_size))
)
# Compute activation for decoding
decode = tf.matmul(encode, W_dec) + b_dec
if dec is not None:
decode = enc(decode)
model = {
# Input placeholder
"input": x,
# Encode function
"encode": encode,
# Decode function
"decode": decode,
# Cost function: mean squared error
"cost": tf.sqrt(tf.reduce_mean(tf.square(x - decode))),
# Model parameters
"params": {
"W_enc": W_enc,
"b_enc": b_enc,
"b_dec": b_dec,
}
}
# Add weight decoder parameters
if not tight:
model["params"]["W_dec"] = W_dec
return model
def nn(input_size, n_classes, layers, init=None):
"""
Multi-layer model
Supports tight weights and corruption.
"""
# Define data input placeholder
input = x = tf.placeholder(tf.float32, [None, input_size])
# Define expected output placeholder
y = tf.placeholder("float", [None, n_classes])
actvs = []
dropouts = []
params = {}
for i, layer in enumerate(layers):
# Define dropout placeholder
dropout = tf.placeholder(tf.float32)
if init is None:
# Initialize empty weights
W = tf.Variable(tf.zeros([input_size, layer["size"]]))
b = tf.Variable(tf.zeros([layer["size"]]))
else:
# Initialize weights with pre-training
W = tf.Variable(init[i]["W"])
b = tf.Variable(init[i]["b"])
# Compute layer activation
x = tf.matmul(x, W) + b
if "actv" in layer and layer["actv"] is not None:
x = layer["actv"](x)
# Compute layer dropout
x = tf.nn.dropout(x, dropout)
# Store parameters
params.update({
"W_" + str(i+1): W,
"b_" + str(i+1): b,
})
actvs.append(x)
dropouts.append(dropout)
input_size = layer["size"]
# Initialize output weights
W = tf.Variable(tf.random_uniform(
[input_size, n_classes],
-3.0 / math.sqrt(input_size + n_classes),
3.0 / math.sqrt(input_size + n_classes)))
b = tf.Variable(tf.zeros([n_classes]))
# Compute logits output
y_hat = tf.matmul(x, W) + b
# Add layer parameters
params.update({"W_out": W, "b_out": b})
actvs.append(y_hat)
return {
# Input placeholder
"input": input,
# Expected output placeholder
"expected": y,
# NN output function
"output": tf.nn.softmax(y_hat),
# Cost function: cross-entropy
"cost": tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=y_hat, labels=y)),
# Droupout placeholders
"dropouts": dropouts,
# Layer activations
"actvs": actvs,
# Model parameters
"params": params,
}