-
Notifications
You must be signed in to change notification settings - Fork 4
/
Functions_homework1_question3_25.py
196 lines (137 loc) · 6.15 KB
/
Functions_homework1_question3_25.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
187
188
189
190
191
192
193
194
from numpy import exp
import numpy as np
from sklearn.model_selection import train_test_split
import tensorflow as tf
from time import time
import matplotlib.pyplot as plt
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D
from sklearn.cluster import KMeans
def franke2d_tensor(x):
t1 = 0.75*exp(-(9*x[0]-2)**2/4-(9*x[1]-2)**2/4)
t2 = 0.75*exp(-(9*x[0]+1)**2/49 -(9*x[1]+1)/10)
t3 = 0.5*exp(-(9*x[0]-7)**2/4-(9*x[1]-3)**2/4)
t4 = -0.2*exp(-(9*x[0]-4)**2-(9*x[1]-7)**2)
return t1+t2+t3+t4
def g(t, sigma=4.0):
return (1-tf.exp(-sigma*t))/(1+tf.exp(-sigma*t))
def generate_train_test(n=100, p=0.7, a=-1e-1, b=1e-1, seed=17434):
np.random.seed(seed)
# Generate the Columns x1 x2 from uniform([0,1]x[0,1])
X = np.random.uniform(size=[n, 2],
low=0,
high=1)
# Generate 1 as franke2d(x1, x2)
y = np.reshape(list(map(franke2d_tensor, X)), n)
# Generate noise shape (100, 1) from uniform(a, b)
noise = np.random.uniform(size=n,
low=a,
high=b)
# Adding noise to y
y = y + noise
return train_test_split(X, y, train_size=p)
def init_weights(shape, trainable=True, seed=174373):
""" Weight initialization """
# weights = tf.random_normal(shape, mean=0.0847, stddev=0.4737, seed=seed
weights = tf.truncated_normal(shape, seed=seed)
return tf.Variable(weights, trainable=trainable)
def generate_decomposition_variables(INPUT_UNITS, OUTPUT_UNITS, HIDDEN_UNITS, RHO_CONSTANT, SIGMA_CONSTANT):
X = tf.placeholder("float", shape=[None, INPUT_UNITS])
y = tf.placeholder("float", shape=[None, OUTPUT_UNITS])
# Weight initializations
omega = {
'w': init_weights([INPUT_UNITS, HIDDEN_UNITS]),
'v': init_weights([HIDDEN_UNITS, OUTPUT_UNITS]),
'b': init_weights([HIDDEN_UNITS])
}
pi = {
'rho': tf.constant(RHO_CONSTANT),
'sigma': SIGMA_CONSTANT
}
con = tf.concat([tf.reshape(omega['w'], shape=[1, INPUT_UNITS * HIDDEN_UNITS]),
tf.reshape(omega['v'], shape=[1, HIDDEN_UNITS * OUTPUT_UNITS]),
tf.reshape(omega['b'], shape=[1, HIDDEN_UNITS])], 1)
return X, y, omega, pi, con
def generate_decomposition_y_hat_function(X, omega, pi):
h = g((tf.matmul(X, omega['w']) - omega['b']), sigma=pi['sigma'])
y_hat = tf.matmul(h, omega['v'])
return y_hat
def generate_generic_loss_functions(y_hat, y, pi, con, SAMPLE_SIZE=100, TRAIN_TEST_PROPORTION=0.7):
regularization = pi['rho'] * tf.reduce_sum(tf.square(tf.norm(con)))
# Forward propagation
train_loss = tf.reduce_sum(tf.square(tf.norm(tf.subtract(y_hat, y)))) / ((SAMPLE_SIZE * TRAIN_TEST_PROPORTION) * 2.0 )+ regularization
test_loss = tf.reduce_sum(tf.square(tf.norm(tf.subtract(y_hat, y)))) / (SAMPLE_SIZE * (1-TRAIN_TEST_PROPORTION)) * 2.0
return train_loss, test_loss
def generate_mse_functions(y_hat, y, SAMPLE_SIZE, TRAIN_TEST_PROPORTION):
mse_test = tf.reduce_sum(tf.square(tf.subtract(y_hat, y))) / (SAMPLE_SIZE * (1 - TRAIN_TEST_PROPORTION))
mse_training = tf.reduce_sum(tf.square(tf.subtract(y_hat, y))) / (SAMPLE_SIZE * TRAIN_TEST_PROPORTION)
return mse_test, mse_training
def generate_decomposition_optimizers(loss_function, omega, LEARNING_RATE=0.01):
convex = tf.train.AdamOptimizer(LEARNING_RATE).minimize(loss_function, var_list=[omega['v']])
non_convex = tf.train.AdamOptimizer(LEARNING_RATE).minimize(loss_function, var_list=[omega['w'], omega['b']])
return convex, non_convex
def init_tf_session():
sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)
return sess
def train_decomposition_model(sess, convex, non_convex, train_loss, omega, X, y, train_X, train_y,
EPOCHS=10000, COMPUTE_TIME=True, tol=1e-6, target="MLP"):
toc = None
if COMPUTE_TIME:
tic = time()
train_loss_actual = 0
for epoch in range(1, EPOCHS+1):
# Train with each example
_, = sess.run([convex], feed_dict={X: train_X, y: train_y})
_, = sess.run([non_convex], feed_dict={X: train_X, y: train_y})
train_loss_new = sess.run([train_loss], feed_dict={X: train_X, y: train_y})
if abs(train_loss_actual-train_loss_new[0]) < tol:
break
else:
train_loss_actual = train_loss_new
if COMPUTE_TIME:
toc = time()-tic
if target == "MLP":
w_opt, b_opt, v_opt = sess.run([omega['w'], omega['b'], omega['v']])
return w_opt, b_opt, v_opt, epoch, toc
def compute_gradient(loss_function, omega):
return tf.gradients(loss_function,[omega['w'], omega['b']])
def compute_norm_gradient(gradients):
return tf.norm(gradients)
def predict_decomposition(sess, y_hat, X, test_X):
return sess.run([y_hat], feed_dict={X: test_X})
def compute_mse(sess, mse_function, X, y, X_input, y_input):
return sess.run([mse_function], feed_dict={X: X_input, y: y_input})
def compute_loss(sess, loss_function, X, y, input_X, input_y):
return sess.run([loss_function], feed_dict={X: input_X, y: input_y})
def plot_pred_real(y, y_pred,
x_lim=(-0.2,1.2), y_lim=(-0.2, 1.2), xlab="", ylab="",title=""):
plt.scatter(np.array(y), np.array(y_pred))
plt.plot([x_lim[0], x_lim[1]], [y_lim[0], y_lim[1]], 'k-', lw=3)
plt.xlabel(xlab)
plt.ylabel(ylab)
plt.title(title)
plt.show()
plt.close()
def plot_3d_data(train_x1_x2, pred_y, test_y=None, save=False, name=None, title=""):
train_x1, train_x2 = np.split(train_x1_x2,[1], 1)
n = train_x1.shape[0]
train_x1 = train_x1.reshape([n])
train_x2 = train_x2.reshape([n])
pred_y = pred_y.reshape([n])
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot_trisurf(train_x1,
train_x2,
pred_y,
linewidth=0.2,
antialiased=True,
cmap=cm.coolwarm)
ax.scatter(train_x1, train_x2, pred_y)
plt.title(title)
if save:
plt.savefig('C:\\Users\\Giulia\\fernando\\optimization\\neural-networks\\img\\{}.png'.format(name))
else:
plt.show()
plt.close()