Skip to content

Commit fc5bed0

Browse files
committed
adding matrix factorization
1 parent b078e3d commit fc5bed0

File tree

4 files changed

+169
-12
lines changed

4 files changed

+169
-12
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ data/mnist.pkl.gz
88
data/mnist_py3k.pkl.gz
99
data/Nottingham.zip
1010
data/Nottingham
11+
data/cnn-furniture
1112
data/midi.zip
13+
data/test_set.pkl
14+
data/train_set.pkl
15+
data/valid_set.pkl
1216
html
1317
*.pyc
1418
*~

code/convolutional_mlp_test.py

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
from theano.tensor.signal import downsample
3333
from theano.tensor.nnet import conv
3434

35-
from logistic_sgd import LogisticRegression, load_data
35+
from logistic_sgd_test import LogisticRegression, load_data
3636
from mlp import HiddenLayer
3737

3838

@@ -136,9 +136,8 @@ def evaluate_lenet5(learning_rate=0.1, n_epochs=200,
136136
rng = numpy.random.RandomState(23455)
137137

138138
datasets = load_data(dataset)
139-
print datasets
140139

141-
"""train_set_x, train_set_y = datasets[0]
140+
train_set_x, train_set_y = datasets[0]
142141
valid_set_x, valid_set_y = datasets[1]
143142
test_set_x, test_set_y = datasets[2]
144143

@@ -166,7 +165,8 @@ def evaluate_lenet5(learning_rate=0.1, n_epochs=200,
166165
# Reshape matrix of rasterized images of shape (batch_size, 28 * 28)
167166
# to a 4D tensor, compatible with our LeNetConvPoolLayer
168167
# (28, 28) is the size of MNIST images.
169-
layer0_input = x.reshape((batch_size, 1, 28, 28))
168+
#layer0_input = x.reshape((batch_size, 1, 28, 28))
169+
layer0_input = x.reshape((batch_size, 1, 256, 256))
170170

171171
# Construct the first convolutional pooling layer:
172172
# filtering reduces the image size to (28-5+1 , 28-5+1) = (24, 24)
@@ -175,10 +175,17 @@ def evaluate_lenet5(learning_rate=0.1, n_epochs=200,
175175
layer0 = LeNetConvPoolLayer(
176176
rng,
177177
input=layer0_input,
178-
image_shape=(batch_size, 1, 28, 28),
178+
image_shape=(batch_size, 1, 256, 256),
179179
filter_shape=(nkerns[0], 1, 5, 5),
180180
poolsize=(2, 2)
181181
)
182+
# layer0 = LeNetConvPoolLayer(
183+
# rng,
184+
# input=layer0_input,
185+
# image_shape=(batch_size, 1, 28, 28),
186+
# filter_shape=(nkerns[0], 1, 5, 5),
187+
# poolsize=(2, 2)
188+
# )
182189

183190
# Construct the second convolutional pooling layer
184191
# filtering reduces the image size to (12-5+1, 12-5+1) = (8, 8)
@@ -187,10 +194,25 @@ def evaluate_lenet5(learning_rate=0.1, n_epochs=200,
187194
layer1 = LeNetConvPoolLayer(
188195
rng,
189196
input=layer0.output,
190-
image_shape=(batch_size, nkerns[0], 12, 12),
197+
image_shape=(batch_size, nkerns[0], 126, 126),
191198
filter_shape=(nkerns[1], nkerns[0], 5, 5),
192199
poolsize=(2, 2)
193200
)
201+
# layer1 = LeNetConvPoolLayer(
202+
# rng,
203+
# input=layer0.output,
204+
# image_shape=(batch_size, nkerns[0], 12, 12),
205+
# filter_shape=(nkerns[1], nkerns[0], 5, 5),
206+
# poolsize=(2, 2)
207+
# )
208+
209+
# layer1 = LeNetConvPoolLayer(
210+
# rng,
211+
# input=layer0.output,
212+
# image_shape=(batch_size, nkerns[0], 126, 126),
213+
# filter_shape=(nkerns[1], nkerns[0], 5, 5),
214+
# poolsize=(2, 2)
215+
# )
194216

195217
# the HiddenLayer being fully-connected, it operates on 2D matrices of
196218
# shape (batch_size, num_pixels) (i.e matrix of rasterized images).
@@ -202,13 +224,22 @@ def evaluate_lenet5(learning_rate=0.1, n_epochs=200,
202224
layer2 = HiddenLayer(
203225
rng,
204226
input=layer2_input,
205-
n_in=nkerns[1] * 4 * 4,
227+
n_in=nkerns[1] * 61 * 61,
206228
n_out=500,
207229
activation=T.tanh
208230
)
209231

232+
# layer2 = HiddenLayer(
233+
# rng,
234+
# input=layer2_input,
235+
# n_in=nkerns[1] * 4 * 4,
236+
# n_out=500,
237+
# activation=T.tanh
238+
# )
239+
210240
# classify the values of the fully-connected sigmoidal layer
211-
layer3 = LogisticRegression(input=layer2.output, n_in=500, n_out=10)
241+
#layer3 = LogisticRegression(input=layer2.output, n_in=500, n_out=10)
242+
layer3 = LogisticRegression(input=layer2.output, n_in=500, n_out=2)
212243

213244
# the cost we minimize during training is the NLL of the model
214245
cost = layer3.negative_log_likelihood(y)
@@ -338,7 +369,6 @@ def evaluate_lenet5(learning_rate=0.1, n_epochs=200,
338369
print >> sys.stderr, ('The code for file ' +
339370
os.path.split(__file__)[1] +
340371
' ran for %.2fm' % ((end_time - start_time) / 60.))
341-
"""
342372
if __name__ == '__main__':
343373
evaluate_lenet5()
344374

code/logistic_sgd_test.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,9 +222,7 @@ def load_data(dataset):
222222

223223
pkl_file = open( '../data/test_set.pkl', 'rb')
224224
test_set = cPickle.load(pkl_file)
225-
226-
#print dataset
227-
#print train_set[0]
225+
228226
#train_set, valid_set, test_set format: tuple(input, target)
229227
#input is an numpy.ndarray of 2 dimensions (a matrix)
230228
#witch row's correspond to an example. target is a

code/mf.py

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
import theano.tensor as T
2+
from theano import function
3+
from theano.ifelse import ifelse
4+
import theano, time, numpy
5+
from theano import shared
6+
rng = numpy.random
7+
8+
state = shared(float(0))
9+
10+
x = T.dscalar('x')
11+
y = T.dscalar('y')
12+
#x = T.scalar(dtype= state.type)
13+
#y = T.scalar(dtype= state.type)
14+
z = x + y
15+
16+
17+
#state = 1
18+
19+
#f = function([x, y], z)
20+
f_updates = function([x, y], z , updates=[(state, state + x + y)])
21+
22+
# print f_updates(1,2)
23+
# z_switch = T.switch(T.lt(x,y) , T.pow(x,2) + y , x + y)
24+
# f_switch = function([x,y],z_switch)
25+
# print f_switch(4,3)
26+
27+
# for i in xrange(1,10):
28+
# f_updates(1,0)
29+
30+
# print state.get_value()
31+
32+
R = [
33+
[5,3,0,1],
34+
[4,0,0,1],
35+
[1,1,0,5],
36+
[1,0,0,4],
37+
[0,1,5,4],
38+
]
39+
40+
R = numpy.array(R).astype(theano.config.floatX)
41+
tR = theano.shared(R.astype(theano.config.floatX),name="R")
42+
print type(tR)
43+
ncols = len(R[0])
44+
nrows = len(R)
45+
#print
46+
#theano.printing.debugprint(tR.shape)
47+
#print
48+
row_values = T.dvector('row_values')
49+
column_values = T.dvector('column_values')
50+
row = T.dscalar('row')
51+
52+
total_squared_sum = shared(float(0))
53+
#sq_sum = pow(row_values.sum(),2) +
54+
dot_product = row + T.dot(row_values,row_values)
55+
f_test = function([row,row_values], dot_product , updates=[(total_squared_sum, total_squared_sum + dot_product)])
56+
57+
for row in xrange(0,nrows):
58+
f_test(row,R[row,:])
59+
60+
print total_squared_sum.get_value()
61+
62+
63+
def matrix_factorization(R, P, Q, K, steps=5000, alpha=0.0002, beta=0.02):
64+
Q = Q.T
65+
66+
# p_row = theano.shared(rng.random((1,4)).astype(theano.config.floatX))
67+
# q_col = theano.shared(rng.random((1,5)).astype(theano.config.floatX))
68+
69+
for step in xrange(steps):
70+
for i in xrange(len(R)):
71+
for j in xrange(len(R[i])):
72+
if R[i][j] > 0:
73+
eij = R[i][j] - numpy.dot(P[i,:],Q[:,j])
74+
for k in xrange(K):
75+
P[i][k] = P[i][k] + alpha * (2 * eij * Q[k][j] - beta * P[i][k])
76+
Q[k][j] = Q[k][j] + alpha * (2 * eij * P[i][k] - beta * Q[k][j])
77+
eR = numpy.dot(P,Q)
78+
e = 0
79+
for i in xrange(len(R)):
80+
for j in xrange(len(R[i])):
81+
if R[i][j] > 0:
82+
e = e + pow(R[i][j] - numpy.dot(P[i,:],Q[:,j]), 2)
83+
for k in xrange(K):
84+
e = e + (beta/2) * (pow(P[i][k],2) + pow(Q[k][j],2))
85+
if e < 0.001:
86+
break
87+
return P, Q.T
88+
89+
R = [
90+
[5,3,0,1],
91+
[4,0,0,1],
92+
[1,1,0,5],
93+
[1,0,0,4],
94+
[0,1,5,4],
95+
]
96+
97+
R = numpy.array(R)
98+
99+
N = len(R)
100+
M = len(R[0])
101+
K = 2
102+
103+
# P = theano.shared(
104+
# numpy.asarray(
105+
# numpy.random.rand(N,K),
106+
# dtype=theano.config.floatX
107+
# ),
108+
# borrow=True
109+
# )
110+
111+
# Q = theano.shared(
112+
# numpy.asarray(
113+
# numpy.random.rand(M,K),
114+
# dtype=theano.config.floatX
115+
# ),
116+
# borrow=True
117+
# )
118+
119+
#print Q
120+
121+
# P = numpy.random.rand(N,K)
122+
# Q = numpy.random.rand(M,K)
123+
124+
# nP, nQ = matrix_factorization(R, P, Q, K)
125+
# nR = numpy.dot(nP, nQ.T)

0 commit comments

Comments
 (0)