Skip to content

Commit 9f062bb

Browse files
author
Joseph Perla
committed
working without sparsity params
1 parent 7c765c7 commit 9f062bb

File tree

4 files changed

+47
-31
lines changed

4 files changed

+47
-31
lines changed

display.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@ def array_to_file(filename, a):
2828
i = Image.fromarray(a.astype('uint8'))
2929
return i.save(filename)
3030

31-
def display_network(filename, images):
31+
def display_network(filename, images, padding=1):
3232
"""Accepts filename string,
33-
2-d numpy array of images.
33+
2-d numpy array of images,
34+
and padding (default 1) number of black pixels between images.
3435
Each column of images is a filter.
3536
3637
This function visualizes filters in matrix images.
@@ -58,13 +59,17 @@ def display_network(filename, images):
5859
cols = int(math.sqrt(n))
5960
rows = int(n / cols) + (1 if n % cols > 0 else 0)
6061

62+
# black background in output
63+
p = padding
64+
output = np.zeros((p + rows * (d + p), p + cols * (d + p)))
65+
output += np.min(images.flatten())
6166
# then fill in the output
62-
output = np.empty((rows * d, cols * d))
6367
for i in xrange(n):
6468
r,c = int(i / cols), i % cols
6569
image = images[:,i]
6670
image.shape = (d,d)
67-
output[r*d:(r*d+d),c*d:(c*d+d)] = image
71+
x,y = (r*(d+p))+p, (c*(d+p))+p
72+
output[x:x+d,y:y+d] = image
6873

6974
# and save it
7075
return array_to_file(filename, output)

numerical_gradient.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ def offset(size, epsilon, i):
2626
y[i] += epsilon
2727
return y
2828

29-
e = partial(offset, size, epsilon)
29+
o = partial(offset, size, epsilon)
3030
for i in xrange(size):
31-
q = e(i)
32-
grad[i] = (J(theta + q) - J(theta - q)) / (2 * epsilon)
31+
e = o(i)
32+
grad[i] = (J(theta + e) - J(theta - e)) / (2 * epsilon)
3333

3434
return grad
3535

sparse_autoencoder.py

+19-13
Original file line numberDiff line numberDiff line change
@@ -28,27 +28,31 @@ def cost(theta, visible_size, hidden_size,
2828
"""
2929
hv = hidden_size * visible_size
3030

31-
W1 = theta[1:hv].reshape(hidden_size, visible_size)
32-
W2 = theta[hv+1:2*hv].reshape(visible_size, hidden_size)
33-
b1 = theta[2*hv+1:2*hv+hidden_size]
34-
b2 = theta[2*hv+hidden_size+1:]
31+
W1 = theta[:hv].reshape(hidden_size, visible_size)
32+
W2 = theta[hv:2*hv].reshape(visible_size, hidden_size)
33+
b1 = theta[2*hv:2*hv+hidden_size]
34+
b2 = theta[2*hv+hidden_size:]
3535

3636
# Cost and gradient variables (your code needs to compute these values).
3737

3838
# Here, we initialize them to zeros.
39-
cost = 0
40-
4139
W1grad = np.zeros(W1.shape)
4240
W2grad = np.zeros(W2.shape)
4341
b1grad = np.zeros(b1.shape)
4442
b2grad = np.zeros(b2.shape)
4543

44+
def T(a):
45+
"""Given 1-d array. Make it a column vector.
46+
Returns 2d array with Nx1 size.
47+
"""
48+
return a.reshape(len(a), 1)
49+
4650
num_data = data.shape[1]
4751
# do a feed forward pass
4852
# a2: (hidden_size, num_data)
49-
a2 = sigmoid(np.dot(W1, data) + b1.T)
53+
a2 = sigmoid(np.dot(W1, data) + T(b1))
5054
# a2: (visible_size, num_data)
51-
a3 = sigmoid(np.dot(W2, a2) + b2.T)
55+
a3 = sigmoid(np.dot(W2, a2) + T(b2))
5256
assert a2.shape == (hidden_size, num_data)
5357
assert a3.shape == (visible_size, num_data)
5458

@@ -60,20 +64,22 @@ def cost(theta, visible_size, hidden_size,
6064
# delta2: (hidden, num_data)
6165
delta2 = np.dot(W2.T, delta3) * (a2 * (1 - a2))
6266

63-
W1grad = np.dot(delta2, data.T)
64-
W2grad = np.dot(delta3, a2.T)
65-
b1grad = delta2
66-
b2grad = delta3
67+
W1grad[:,:] = np.dot(delta2, data.T) / float(num_data)
68+
W2grad[:,:] = np.dot(delta3, a2.T) / float(num_data)
69+
b1grad[:] = np.sum(delta2, axis=1) / float(num_data)
70+
b2grad[:] = np.sum(delta3, axis=1) / float(num_data)
6771

6872
grad = flatten_params(W1grad, W2grad, b1grad, b2grad)
6973
return cost, grad
7074

71-
def initialize_parameters(hidden_size, visible_size):
75+
def initialize_params(hidden_size, visible_size):
7276
"""Accepts number of hidde states in sparse encoder,
7377
and number of input states in sparse encoder..
7478
Initialize parameters randomly based on layer sizes.
7579
Returns a new flat array of size 2*visisble_size + hidden_size
7680
"""
81+
assert hidden_size < visible_size
82+
7783
#we'll choose weights uniformly from the interval [-r, r]
7884
r = np.sqrt(6) / np.sqrt(hidden_size + visible_size + 1)
7985
W1 = np.random.rand(hidden_size, visible_size) * 2 * r - r

test_sparse_autoencoder.py

+16-11
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
from test_numerical_gradient import diff_grad
1010

1111
def test_sae_cost():
12-
patch_size = (3,3)
12+
patch_size = (8,8)
1313
visible_size = patch_size[0] * patch_size[1]
14-
hidden_size = 3
14+
hidden_size = 25
1515
weight_decay, sparsity_param, beta = 0, 0, 0
1616

17-
num_samples = 10
17+
num_samples = 50
1818
images = sample_images.load_matlab_images('IMAGES.mat')
1919
patches = sample_images.sample(images, num_samples, patch_size)
2020

@@ -26,18 +26,23 @@ def test_sae_cost():
2626
beta=beta,
2727
data=patches)
2828

29-
theta = sparse_autoencoder.initialize_params()
29+
theta = sparse_autoencoder.initialize_params(hidden_size, visible_size)
3030
cost, grad = sae_cost(theta)
31-
ncost, ngrad = numerical_gradient.compute(theta,
32-
lambda x: sae_cost(x)[0],
33-
epsilon=0.0001)
34-
35-
print ncost, cost
31+
ngrad = numerical_gradient.compute(theta,
32+
lambda x: sae_cost(x)[0],
33+
epsilon=0.0001)
34+
print cost
35+
print ngrad.shape, grad.shape
3636
print ngrad, grad
3737

3838
diff = diff_grad(ngrad, grad)
3939
print diff
4040

41-
less_than = 1e-9
42-
assert diff < less_than
41+
threshold = 1e-9
42+
assert diff < threshold
43+
44+
45+
grad[8] = 1000
46+
assert diff_grad(ngrad, grad) > threshold
47+
4348

0 commit comments

Comments
 (0)