-
Notifications
You must be signed in to change notification settings - Fork 36
/
sfo_demo.py
78 lines (67 loc) · 2.61 KB
/
sfo_demo.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
"""
Train an autoencoder using SFO.
Demonstrates usage of the Sum of Functions Optimizer (SFO) Python
package. See sfo.py and
https://github.com/Sohl-Dickstein/Sum-of-Functions-Optimizer
for additional documentation.
Copyright 2014 Jascha Sohl-Dickstein
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import matplotlib.pyplot as plt
import numpy as np
from numpy.random import randn
from sfo import SFO
# define an objective function and gradient
def f_df(theta, v):
"""
Calculate reconstruction error and gradient for an autoencoder with sigmoid
nonlinearity.
v contains the training data, and will be different for each subfunction.
"""
h = 1./(1. + np.exp(-(np.dot(theta['W'], v) + theta['b_h'])))
v_hat = np.dot(theta['W'].T, h) + theta['b_v']
f = np.sum((v_hat - v)**2) / v.shape[1]
dv_hat = 2.*(v_hat - v) / v.shape[1]
db_v = np.sum(dv_hat, axis=1).reshape((-1,1))
dW = np.dot(h, dv_hat.T)
dh = np.dot(theta['W'], dv_hat)
db_h = np.sum(dh*h*(1.-h), axis=1).reshape((-1,1))
dW += np.dot(dh*h*(1.-h), v.T)
dfdtheta = {'W':dW, 'b_h':db_h, 'b_v':db_v}
return f, dfdtheta
# set model and training data parameters
M = 20 # number visible units
J = 10 # number hidden units
D = 100000 # full data batch size
N = int(np.sqrt(D)/10.) # number minibatches
# generate random training data
v = randn(M,D)
# create the array of subfunction specific arguments
sub_refs = []
for i in range(N):
# extract a single minibatch of training data.
sub_refs.append(v[:,i::N])
# initialize parameters
theta_init = {'W':randn(J,M), 'b_h':randn(J,1), 'b_v':randn(M,1)}
# initialize the optimizer
optimizer = SFO(f_df, theta_init, sub_refs)
# # uncomment the following line to test the gradient of f_df
# optimizer.check_grad()
# run the optimizer for 1 pass through the data
theta = optimizer.optimize(num_passes=1)
# continue running the optimizer for another 20 passes through the data
theta = optimizer.optimize(num_passes=20)
# plot the convergence trace
plt.plot(np.array(optimizer.hist_f_flat))
plt.xlabel('Iteration')
plt.ylabel('Minibatch Function Value')
plt.title('Convergence Trace')
plt.show()