@@ -13,8 +13,10 @@ def train():
13
13
# allow your sparse autoencoder to get good filters; you do not need to
14
14
# change the parameters below.
15
15
16
- visible_size = 8 * 8 # number of input units
17
- hidden_size = 25 # number of hidden units
16
+ patch_size = 8
17
+ num_patches = 10000
18
+ visible_size = patch_size ** 2 # number of input units
19
+ hidden_size = 25 # number of hidden units
18
20
sparsity_param = 0.01 # desired average activation of the hidden units.
19
21
# (This was denoted by the Greek alphabet rho, which looks like a lower-case "p",
20
22
# in the lecture notes).
@@ -25,12 +27,12 @@ def train():
25
27
# After implementing sampleIMAGES, the display_network command should
26
28
# display a random sample of 200 patches from the dataset
27
29
28
- patches = sample_images ()
30
+ patches = sample_images (patch_size , num_patches )
29
31
# list = [randint(0, patches.shape[0]-1) for i in xrange(64)]
30
32
# display_network(patches[list, :], 8)
31
33
32
34
# Obtain random parameters theta
33
- theta = initialize_parameters (hidden_size , visible_size )
35
+ # theta = initialize_parameters(visible_size, hidden_size )
34
36
35
37
# STEP 2: Implement sparseAutoencoderCost
36
38
#
@@ -57,8 +59,8 @@ def train():
57
59
# and/or lambda to zero may be helpful for debugging.) However, in your
58
60
# final submission of the visualized weights, please use parameters we
59
61
# gave in Step 0 above.
60
- cost , grad = sparse_autoencoder_cost_and_grad (theta , visible_size , hidden_size ,
61
- decay_lambda , sparsity_param , beta , patches )
62
+ # cost, grad = sparse_autoencoder_cost_and_grad(theta, visible_size, hidden_size,
63
+ # decay_lambda, sparsity_param, beta, patches)
62
64
63
65
# STEP 3: Gradient Checking
64
66
#
@@ -69,35 +71,36 @@ def train():
69
71
# First, lets make sure your numerical gradient computation is correct for a
70
72
# simple function. After you have implemented compute_numerical_gradient,
71
73
# run the following:
72
- check_numerical_gradient ()
74
+ # check_numerical_gradient()
73
75
74
76
# Now we can use it to check your cost function and derivative calculations
75
77
# for the sparse autoencoder.
76
- func = lambda x : sparse_autoencoder_cost (x , visible_size , hidden_size ,
77
- decay_lambda , sparsity_param , beta , patches )
78
- numgrad = compute_numerical_gradient (func , theta )
78
+ # func = lambda x: sparse_autoencoder_cost(x, visible_size, hidden_size,
79
+ # decay_lambda, sparsity_param, beta, patches)
80
+ # numgrad = compute_numerical_gradient(func, theta)
79
81
80
82
# Use this to visually compare the gradients side by side
81
- print numgrad , grad
83
+ # print numgrad, grad
82
84
83
85
# Compare numerically computed gradients with the ones obtained from backpropagation
84
- diff = np .linalg .norm (numgrad - grad )/ np .linalg .norm (numgrad + grad )
86
+ # diff = np.linalg.norm(numgrad-grad)/np.linalg.norm(numgrad+grad)
85
87
# Should be small. In our implementation, these values are usually less than 1e-9.
86
- print diff
88
+ # print diff
87
89
88
90
# STEP 4: After verifying that your implementation of
89
- # sparse_autoencoder_cost is correct, You can start training your sparse
90
- # autoencoder with minFunc (L-BFGS).
91
+ # sparse_autoencoder_cost is correct, You can start training your sparse
92
+ # autoencoder with minFunc (L-BFGS).
91
93
92
- # Randomly initialize the parameters
93
- theta = initialize_parameters (hidden_size , visible_size )
94
+ # Randomly initialize the parameters
95
+ # Use minimize interface, and set jac=True, so it can accept cost and grad together
96
+ theta = initialize_parameters (visible_size , hidden_size )
94
97
func_args = (visible_size , hidden_size , decay_lambda , sparsity_param , beta , patches )
95
98
res = minimize (sparse_autoencoder_cost_and_grad , x0 = theta , args = func_args , method = 'L-BFGS-B' ,
96
99
jac = True , options = {'maxiter' : 400 , 'disp' : True })
97
100
98
101
# STEP 5: Visualization
99
102
w1 = res .x [0 : hidden_size * visible_size ].reshape ((hidden_size , visible_size ))
100
- # display_network(w1, 12 )
103
+ display_network (w1 , 5 )
101
104
102
105
103
106
if __name__ == "__main__" :
0 commit comments