forked from vvanirudh/deep-ensembles-uncertainty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
154 lines (101 loc) · 6.34 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
import tensorflow as tf
import numpy as np
class MLPGaussianRegressor():
def __init__(self, args, sizes, model_scope):
self.input_data = tf.placeholder(tf.float32, [None, sizes[0]])
self.target_data = tf.placeholder(tf.float32, [None, sizes[0]])
with tf.variable_scope(model_scope+'learning_rate'):
self.lr = tf.Variable(args.learning_rate, trainable=False, name='learning_rate')
with tf.variable_scope(model_scope+'target_stats'):
self.output_mean = tf.Variable(0., trainable=False, dtype=tf.float32)
self.output_std = tf.Variable(0.1, trainable=False, dtype=tf.float32)
self.weights = []
self.biases = []
with tf.variable_scope(model_scope+'MLP'):
for i in range(1, len(sizes)):
self.weights.append(tf.Variable(tf.random_normal([sizes[i-1], sizes[i]], stddev=0.1), name='weights_'+str(i-1)))
self.biases.append(tf.Variable(tf.random_normal([sizes[i]], stddev=0.1), name='biases_'+str(i-1)))
x = self.input_data
for i in range(0, len(sizes)-2):
x = tf.nn.relu(tf.add(tf.matmul(x, self.weights[i]), self.biases[i]))
self.output = tf.add(tf.matmul(x, self.weights[-1]), self.biases[-1])
self.mean, self.raw_var = tf.split(self.output, [1,1], axis=1)
# Output transform
self.mean = self.mean * self.output_std + self.output_mean
self.var = (tf.log(1 + tf.exp(self.raw_var)) + 1e-6) * (self.output_std**2)
def gaussian_nll(mean_values, var_values, y):
y_diff = tf.subtract(y, mean_values)
return 0.5*tf.reduce_mean(tf.log(var_values)) + 0.5*tf.reduce_mean(tf.div(tf.square(y_diff), var_values)) + 0.5*tf.log(2*np.pi)
self.nll = gaussian_nll(self.mean, self.var, self.target_data)
self.nll_gradients = tf.gradients(args.alpha * self.nll, self.input_data)[0]
self.adversarial_input_data = tf.add(self.input_data, args.epsilon * tf.sign(self.nll_gradients))
x_at = self.adversarial_input_data
for i in range(0, len(sizes)-2):
x_at = tf.nn.relu(tf.add(tf.matmul(x_at, self.weights[i]), self.biases[i]))
output_at = tf.add(tf.matmul(x_at, self.weights[-1]), self.biases[-1])
mean_at, raw_var_at = tf.split(output_at, [1, 1], axis=1)
# Output transform
mean_at = mean_at * self.output_std + self.output_mean
var_at = (tf.log(1 + tf.exp(raw_var_at)) + 1e-6) * (self.output_std**2)
self.nll_at = gaussian_nll(mean_at, var_at, self.target_data)
tvars = tf.trainable_variables()
for v in tvars:
print v.name
print v.get_shape()
self.gradients = tf.gradients(args.alpha * self.nll + (1 - args.alpha) * self.nll_at, tvars)
self.clipped_gradients, _ = tf.clip_by_global_norm(self.gradients, args.grad_clip)
optimizer = tf.train.RMSPropOptimizer(self.lr)
self.train_op = optimizer.apply_gradients(zip(self.clipped_gradients, tvars))
class MLPDropoutGaussianRegressor():
def __init__(self, args, sizes, model_scope):
self.input_data = tf.placeholder(tf.float32, [None, sizes[0]])
self.target_data = tf.placeholder(tf.float32, [None, sizes[0]])
with tf.variable_scope(model_scope+'learning_rate'):
self.lr = tf.Variable(args.learning_rate, trainable=False, name='learning_rate')
with tf.variable_scope(model_scope+'target_stats'):
self.output_mean = tf.Variable(0., trainable=False, dtype=tf.float32)
self.output_std = tf.Variable(0.1, trainable=False, dtype=tf.float32)
self.dr = tf.placeholder(tf.float32)
self.weights = []
self.biases = []
with tf.variable_scope(model_scope+'MLP'):
for i in range(1, len(sizes)):
self.weights.append(tf.Variable(tf.random_normal([sizes[i-1], sizes[i]], stddev=0.1), name='weights_'+str(i-1)))
self.biases.append(tf.Variable(tf.random_normal([sizes[i]], stddev=0.1), name='biases_'+str(i-1)))
x = self.input_data
for i in range(0, len(sizes)-2):
x = tf.nn.relu(tf.nn.xw_plus_b(x, self.weights[i], self.biases[i]))
x = tf.nn.dropout(x, self.dr, noise_shape=[1, sizes[i+1]], name='dropout_layer'+str(i))
self.output = tf.add(tf.matmul(x, self.weights[-1]), self.biases[-1])
self.mean, self.raw_var = tf.split(1, 2, self.output)
# Output transform
self.mean = self.mean * self.output_std + self.output_mean
self.var = (tf.log(1 + tf.exp(self.raw_var)) + 1e-6) * (self.output_std**2)
def gaussian_nll(mean_values, var_values, y):
y_diff = tf.subtract(y, mean_values)
return 0.5*tf.reduce_mean(tf.log(var_values)) + 0.5*tf.reduce_mean(tf.div(tf.square(y_diff), var_values)) + 0.5*tf.log(2*np.pi)
self.nll = gaussian_nll(self.mean, self.var, self.target_data)
self.nll_gradients = tf.gradients(args.alpha * self.nll, self.input_data)[0]
self.adversarial_input_data = tf.add(self.input_data, args.epsilon * tf.sign(self.nll_gradients))
x_at = self.adversarial_input_data
for i in range(0, len(sizes)-2):
x_at = tf.nn.relu(tf.nn.xw_plus_b(x_at, self.weights[i], self.biases[i]))
# We need to apply the same dropout mask as before
# so that we maintain the same model and not change the network
graph = tf.get_default_graph()
mask = graph.get_tensor_by_name('dropout_layer'+str(i)+'/Floor:0')
x_at = tf.mul(x_at, mask)
output_at = tf.add(tf.matmul(x_at, self.weights[-1]), self.biases[-1])
mean_at, raw_var_at = tf.split(1, 2, output_at)
# Output transform
mean_at = mean_at * self.output_std + self.output_mean
var_at = (tf.log(1 + tf.exp(raw_var_at)) + 1e-6) * (self.output_std**2)
self.nll_at = gaussian_nll(mean_at, var_at, self.target_data)
tvars = tf.trainable_variables()
for v in tvars:
print v.name
print v.get_shape()
self.gradients = tf.gradients(args.alpha * self.nll + (1 - args.alpha) * self.nll_at, tvars)
self.clipped_gradients, _ = tf.clip_by_global_norm(self.gradients, args.grad_clip)
optimizer = tf.train.RMSPropOptimizer(self.lr)
self.train_op = optimizer.apply_gradients(zip(self.clipped_gradients, tvars))