## a theano implementation
import theano
import theano.tensor as T
import numpy as np
from scipy import optimize
class MatrixFactorization(object):
def __init__(self, n, m, k, R, index, beta = 0.1):
self.theta = theano.shared(
value = np.random.randn(n*k + m*k),
borrow = True,
name = 'theta')
self.P = self.theta[:n*k].reshape((n, k))
self.Q = self.theta[n*k:].reshape((m, k))
self.R_tilde = T.dot(self.P, self.Q.T)
## index -- boolean matrix
self.mse = T.mean((self.R_tilde[index.nonzero()] - R[index]) ** 2)
self.l2 = T.mean(self.P**2) + T.mean(self.Q**2)
self.cost = self.mse + beta * self.l2
self.gparams = T.grad(self.cost, self.theta)
def share_data(data):
shared_data = theano.shared(value = data, borrow = True)
return share_data
## test case
R = np.asarray([
[5,3,0,1],
[4,0,0,1],
[1,1,0,5],
[1,0,0,4],
[0,1,5,4],
])
n, m = R.shape
k = 2
index = R != 0
mf = MatrixFactorization(n, m, k, R, index)
def f(theta_value):
mf.theta.set_value(theta_value, borrow = True)
return mf.cost.eval()
def fprime(theta_value):
mf.theta.set_value(theta_value, borrow = True)
return mf.gparams.eval()
theta0 = np.random.randn(n*k + m*k)
best_theta = optimize.fmin_l_bfgs_b(func = f, x0 = theta0, fprime = fprime, iprint=1)[0]
print best_theta
mf.theta.set_value(best_theta, borrow = True)
print mf.P.eval()
print mf.Q.eval()
print mf.R_tilde.eval()
[ 0.05211058 -2.24068509 -0.0544423 -1.7784696 -2.25868965 -0.56335438 -1.78642043 -0.53384437 -1.87349095 -0.62452512 0.09904446 -2.19400344 -0.10852147 -1.2999684 -2.30587502 -0.76852742 -2.02677146 -0.49542942] [[ 0.05211058 -2.24068509] [-0.0544423 -1.7784696 ] [-2.25868965 -0.56335438] [-1.78642043 -0.53384437] [-1.87349095 -0.62452512]] [[ 0.09904446 -2.19400344] [-0.10852147 -1.2999684 ] [-2.30587502 -0.76852742] [-2.02677146 -0.49542942]] [[ 4.92123205 2.9071647 1.60186745 1.00448509] [ 3.89657619 2.31786244 1.49233978 0.99144826] [ 1.01229074 0.9774592 5.64120933 4.85695006] [ 0.99432133 0.88784578 4.52953627 3.88514814] [ 1.18465135 1.01517691 4.80000066 4.1065461 ]]
https://github.com/clementfarabet/ipam-tutorials/tree/master/py_tutorials
https://github.com/CamDavidsonPilon/Probabilistic-Programming-and-Bayesian-Methods-for-Hackers
https://github.com/JuliaLang/IJulia.jl
http://lanyrd.com/2013/scipy/video/
https://github.com/mutantturkey/PyFeast
http://www.cs.man.ac.uk/~gbrown/fstoolbox/
http://conference.scipy.org/scipy2013/presentation_detail.php?id=192
http://www.iro.umontreal.ca/~memisevr/code.html
http://www.iro.umontreal.ca/~memisevr/
http://www.iro.umontreal.ca/~memisevr/cifar2013/index.html
https://www.ipam.ucla.edu/schedule.aspx?pc=gss2012
http://www.cs.toronto.edu/~rfm/multiview-feature-learning-cvpr/