<s> """<STR_LIT>""" <EOL> from math import exp , log <EOL> import random <EOL> import numpy as np <EOL> from mrec . mf . recommender import MatrixFactorizationRecommender <EOL> def g ( x ) : <EOL> """<STR_LIT>""" <EOL> return <NUM_LIT:1> / ( <NUM_LIT:1> + exp ( - x ) ) <EOL> def dg ( x ) : <EOL> """<STR_LIT>""" <EOL> return exp ( x ) / ( <NUM_LIT:1> + exp ( x ) ) ** <NUM_LIT:2> <EOL> class CLiMFRecommender ( MatrixFactorizationRecommender ) : <EOL> def __init__ ( self , d , lbda = <NUM_LIT> , gamma = <NUM_LIT> , max_iters = <NUM_LIT> ) : <EOL> self . d = d <EOL> self . lbda = lbda <EOL> self . gamma = gamma <EOL> self . max_iters = max_iters <EOL> def fit ( self , data ) : <EOL> self . U = <NUM_LIT> * np . random . random_sample ( ( data . shape [ <NUM_LIT:0> ] , self . d ) ) <EOL> self . V = <NUM_LIT> * np . random . random_sample ( ( data . shape [ <NUM_LIT:1> ] , self . d ) ) <EOL> for iter in xrange ( self . max_iters ) : <EOL> print '<STR_LIT>' . format ( iter + <NUM_LIT:1> ) <EOL> print '<STR_LIT>' . format ( self . objective ( data ) ) <EOL> self . update ( data ) <EOL> def precompute_f ( self , data , i ) : <EOL> """<STR_LIT>""" <EOL> items = data [ i ] . indices <EOL> f = dict ( ( j , np . dot ( self . U [ i ] , self . V [ j ] ) ) for j in items ) <EOL> return f <EOL> def objective ( self , data ) : <EOL> """<STR_LIT>""" <EOL> F = - <NUM_LIT:0.5> * self . lbda * ( np . sum ( self . U * self . U ) + np . sum ( self . V * self . V ) ) <EOL> for i in xrange ( len ( self . U ) ) : <EOL> f = self . precompute_f ( data , i ) <EOL> for j in f : <EOL> F += log ( g ( f [ j ] ) ) <EOL> for k in f : <EOL> F += log ( <NUM_LIT:1> - g ( f [ k ] - f [ j ] ) ) <EOL> return F <EOL> def update ( self , data ) : <EOL> """<STR_LIT>""" <EOL> for i in xrange ( len ( self . U ) ) : <EOL> dU = - self . lbda * self . U [ i ] <EOL> f = self . precompute_f ( data , i ) <EOL> for j in f : <EOL> dV = g ( - f [ j ] ) - self . lbda * self . V [ j ] <EOL> for k in f : <EOL> dV += dg ( f [ j ] - f [ k ] ) * ( <NUM_LIT:1> / ( <NUM_LIT:1> - g ( f [ k ] - f [ j ] ) ) - <NUM_LIT:1> / ( <NUM_LIT:1> - g ( f [ j ] - f [ k ] ) ) ) * self . U [ i ] <EOL> self . V [ j ] += self . gamma * dV <EOL> dU += g ( - f [ j ] ) * self . V [ j ] <EOL> for k in f : <EOL> dU += ( self . V [ j ] - self . V [ k ] ) * dg ( f [ k ] - f [ j ] ) / ( <NUM_LIT:1> - g ( f [ k ] - f [ j ] ) ) <EOL> self . U [ i ] += self . gamma * dU <EOL> def compute_mrr ( self , data , test_users = None ) : <EOL> """<STR_LIT>""" <EOL> mrr = [ ] <EOL> if test_users is None : <EOL> test_users = range ( len ( self . U ) ) <EOL> for ix , i in enumerate ( test_users ) : <EOL> items = set ( data [ i ] . indices ) <EOL> if not items : <EOL> continue <EOL> predictions = np . sum ( np . tile ( self . U [ i ] , ( len ( self . V ) , <NUM_LIT:1> ) ) * self . V , axis = <NUM_LIT:1> ) <EOL> found = False <EOL> for rank , item in enumerate ( np . argsort ( predictions ) [ : : - <NUM_LIT:1> ] ) : <EOL> if item in items : <EOL> mrr . append ( <NUM_LIT:1.0> / ( rank + <NUM_LIT:1> ) ) <EOL> found = True <EOL> break <EOL> if not found : <EOL> print '<STR_LIT>' . format ( i + <NUM_LIT:1> ) <EOL> print '<STR_LIT>' . format ( items ) <EOL> assert ( len ( mrr ) == len ( test_users ) ) <EOL> return np . mean ( mrr ) <EOL> def main ( ) : <EOL> import sys <EOL> from mrec import load_sparse_matrix , save_recommender <EOL> from mrec . mf . climf import CLiMFRecommender <EOL> file_format = sys . argv [ <NUM_LIT:1> ] <EOL> filepath = sys . argv [ <NUM_LIT:2> ] <EOL> outfile = sys . argv [ <NUM_LIT:3> ] <EOL> train = load_sparse_matrix ( file_format , filepath ) <EOL> model = CLiMFRecommender ( d = <NUM_LIT:5> ) <EOL> model . fit ( train ) <EOL> save_recommender ( model , outfile ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> import cProfile <EOL> cProfile . run ( '<STR_LIT>' ) </s>
