#NEWLINE# def train( self , sessions ) : #NEWLINE# #INDENT# max_query_id = self . config . get ( 'MAX_QUERY_ID' ) #NEWLINE# if ( max_query_id is None ) : #NEWLINE# #INDENT# ( ( print >> sys . stderr ) , 'WARNING: no MAX_QUERY_ID specified for' , self ) #NEWLINE# max_query_id = 100000 #NEWLINE# #UNINDENT# possibleIntents = ( [ False ] if self . ignoreIntents else [ False , True ] ) #NEWLINE# self . alpha = dict ( ( ( i , [ defaultdict ( ( lambda : self . config . get ( 'DEFAULT_REL' , DEFAULT_REL ) ) ) for q in xrange ( max_query_id ) ] ) for i in possibleIntents ) ) #NEWLINE# self . gamma = [ [ [ 0.5 for d in xrange ( self . config . get ( 'MAX_DOCS_PER_QUERY' , MAX_DOCS_PER_QUERY ) ) ] for r in xrange ( self . config . get ( 'MAX_DOCS_PER_QUERY' , MAX_DOCS_PER_QUERY ) ) ] for g in xrange ( self . gammaTypesNum ) ] #NEWLINE# if self . explorationBias : #NEWLINE# #INDENT# self . e = [ 0.5 for p in xrange ( self . config . get ( 'MAX_DOCS_PER_QUERY' , MAX_DOCS_PER_QUERY ) ) ] #NEWLINE# #UNINDENT# if ( not self . config . get ( 'PRETTY_LOG' , PRETTY_LOG ) ) : #NEWLINE# #INDENT# ( ( print >> sys . stderr ) , ( '-' * 80 ) ) #NEWLINE# ( ( print >> sys . stderr ) , 'Start. Current time is' , datetime . now ( ) ) #NEWLINE# #UNINDENT# for iteration_count in xrange ( self . config . get ( 'MAX_ITERATIONS' , MAX_ITERATIONS ) ) : #NEWLINE# #INDENT# self . queryIntentsWeights = defaultdict ( ( lambda : [ ] ) ) #NEWLINE# alphaFractions = dict ( ( ( i , [ defaultdict ( ( lambda : [ 1.0 , 2.0 ] ) ) for q in xrange ( max_query_id ) ] ) for i in possibleIntents ) ) #NEWLINE# gammaFractions = [ [ [ [ 1.0 , 2.0 ] for d in xrange ( self . config . get ( 'MAX_DOCS_PER_QUERY' , MAX_DOCS_PER_QUERY ) ) ] for r in xrange ( self . config . get ( 'MAX_DOCS_PER_QUERY' , MAX_DOCS_PER_QUERY ) ) ] for g in xrange ( self . gammaTypesNum ) ] #NEWLINE# if self . explorationBias : #NEWLINE# #INDENT# eFractions = [ [ 1.0 , 2.0 ] for p in xrange ( self . config . get ( 'MAX_DOCS_PER_QUERY' , MAX_DOCS_PER_QUERY ) ) ] #NEWLINE# #UNINDENT# for s in sessions : #NEWLINE# #INDENT# query = s . query #NEWLINE# layout = ( ( [ False ] * len ( s . layout ) ) if self . ignoreLayout else s . layout ) #NEWLINE# if self . explorationBias : #NEWLINE# #INDENT# explorationBiasPossible = any ( ( ( l and c ) for ( l , c ) in zip ( s . layout , s . clicks ) ) ) #NEWLINE# firstVerticalPos = ( ( - 1 ) if ( not any ( s . layout [ : ( - 1 ) ] ) ) else [ k for ( k , l ) in enumerate ( s . layout ) if l ] [ 0 ] ) #NEWLINE# #UNINDENT# if self . ignoreIntents : #NEWLINE# #INDENT# p_I__C_G = { #NEWLINE# #INDENT# False : 1.0 , #NEWLINE# True : 0 , #NEWLINE# #UNINDENT# } #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# a = ( self . _getSessionProb ( s ) * ( 1 - s . intentWeight ) ) #NEWLINE# b = ( 1 * s . intentWeight ) #NEWLINE# p_I__C_G = { #NEWLINE# #INDENT# False : ( a / ( a + b ) ) , #NEWLINE# True : ( b / ( a + b ) ) , #NEWLINE# #UNINDENT# } #NEWLINE# #UNINDENT# self . queryIntentsWeights [ query ] . append ( p_I__C_G [ True ] ) #NEWLINE# prevClick = ( - 1 ) #NEWLINE# for ( rank , c ) in enumerate ( s . clicks ) : #NEWLINE# #INDENT# url = s . results [ rank ] #NEWLINE# for intent in possibleIntents : #NEWLINE# #INDENT# a = self . alpha [ intent ] [ query ] [ url ] #NEWLINE# if ( self . explorationBias and explorationBiasPossible ) : #NEWLINE# #INDENT# e = self . e [ firstVerticalPos ] #NEWLINE# #UNINDENT# if ( c == 0 ) : #NEWLINE# #INDENT# g = self . getGamma ( self . gamma , rank , prevClick , layout , intent ) #NEWLINE# gCorrection = 1 #NEWLINE# if ( self . explorationBias and explorationBiasPossible and ( not s . layout [ k ] ) ) : #NEWLINE# #INDENT# gCorrection = ( 1 - e ) #NEWLINE# g *= gCorrection #NEWLINE# #UNINDENT# alphaFractions [ intent ] [ query ] [ url ] [ 0 ] += ( ( ( a * ( 1 - g ) ) / ( 1 - ( a * g ) ) ) * p_I__C_G [ intent ] ) #NEWLINE# self . getGamma ( gammaFractions , rank , prevClick , layout , intent ) [ 0 ] += ( ( ( ( g / gCorrection ) * ( 1 - a ) ) / ( 1 - ( a * g ) ) ) * p_I__C_G [ intent ] ) #NEWLINE# if ( self . explorationBias and explorationBiasPossible ) : #NEWLINE# #INDENT# eFractions [ firstVerticalPos ] [ 0 ] += ( ( e if s . layout [ k ] else ( e / ( 1 - ( a * g ) ) ) ) * p_I__C_G [ intent ] ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# alphaFractions [ intent ] [ query ] [ url ] [ 0 ] += ( 1 * p_I__C_G [ intent ] ) #NEWLINE# self . getGamma ( gammaFractions , rank , prevClick , layout , intent ) [ 0 ] += ( 1 * p_I__C_G [ intent ] ) #NEWLINE# if ( self . explorationBias and explorationBiasPossible ) : #NEWLINE# #INDENT# eFractions [ firstVerticalPos ] [ 0 ] += ( ( e if s . layout [ k ] else 0 ) * p_I__C_G [ intent ] ) #NEWLINE# #UNINDENT# #UNINDENT# alphaFractions [ intent ] [ query ] [ url ] [ 1 ] += ( 1 * p_I__C_G [ intent ] ) #NEWLINE# self . getGamma ( gammaFractions , rank , prevClick , layout , intent ) [ 1 ] += ( 1 * p_I__C_G [ intent ] ) #NEWLINE# if ( self . explorationBias and explorationBiasPossible ) : #NEWLINE# #INDENT# eFractions [ firstVerticalPos ] [ 1 ] += ( 1 * p_I__C_G [ intent ] ) #NEWLINE# #UNINDENT# #UNINDENT# if ( c != 0 ) : #NEWLINE# #INDENT# prevClick = rank #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# if ( not self . config . get ( 'PRETTY_LOG' , PRETTY_LOG ) ) : #NEWLINE# #INDENT# sys . stderr . write ( 'E' ) #NEWLINE# #UNINDENT# sum_square_displacement = 0.0 #NEWLINE# for i in possibleIntents : #NEWLINE# #INDENT# for q in xrange ( max_query_id ) : #NEWLINE# #INDENT# for ( url , aF ) in alphaFractions [ i ] [ q ] . iteritems ( ) : #NEWLINE# #INDENT# new_alpha = ( aF [ 0 ] / aF [ 1 ] ) #NEWLINE# sum_square_displacement += ( ( self . alpha [ i ] [ q ] [ url ] - new_alpha ) ** 2 ) #NEWLINE# self . alpha [ i ] [ q ] [ url ] = new_alpha #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# for g in xrange ( self . gammaTypesNum ) : #NEWLINE# #INDENT# for r in xrange ( self . config . get ( 'MAX_DOCS_PER_QUERY' , MAX_DOCS_PER_QUERY ) ) : #NEWLINE# #INDENT# for d in xrange ( self . config . get ( 'MAX_DOCS_PER_QUERY' , MAX_DOCS_PER_QUERY ) ) : #NEWLINE# #INDENT# gF = gammaFractions [ g ] [ r ] [ d ] #NEWLINE# new_gamma = ( gF [ 0 ] / gF [ 1 ] ) #NEWLINE# sum_square_displacement += ( ( self . gamma [ g ] [ r ] [ d ] - new_gamma ) ** 2 ) #NEWLINE# self . gamma [ g ] [ r ] [ d ] = new_gamma #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# if self . explorationBias : #NEWLINE# #INDENT# for p in xrange ( self . config . get ( 'MAX_DOCS_PER_QUERY' , MAX_DOCS_PER_QUERY ) ) : #NEWLINE# #INDENT# new_e = ( eFractions [ p ] [ 0 ] / eFractions [ p ] [ 1 ] ) #NEWLINE# sum_square_displacement += ( ( self . e [ p ] - new_e ) ** 2 ) #NEWLINE# self . e [ p ] = new_e #NEWLINE# #UNINDENT# #UNINDENT# if ( not self . config . get ( 'PRETTY_LOG' , PRETTY_LOG ) ) : #NEWLINE# #INDENT# sys . stderr . write ( 'M\n' ) #NEWLINE# #UNINDENT# rmsd = math . sqrt ( sum_square_displacement ) #NEWLINE# if self . config . get ( 'PRETTY_LOG' , PRETTY_LOG ) : #NEWLINE# #INDENT# sys . stderr . write ( ( '%d..' % ( iteration_count + 1 ) ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# ( ( print >> sys . stderr ) , ( 'Iteration: %d, ERROR: %f' % ( ( iteration_count + 1 ) , rmsd ) ) ) #NEWLINE# #UNINDENT# #UNINDENT# if self . config . get ( 'PRETTY_LOG' , PRETTY_LOG ) : #NEWLINE# #INDENT# sys . stderr . write ( '\n' ) #NEWLINE# #UNINDENT# for ( q , intentWeights ) in self . queryIntentsWeights . iteritems ( ) : #NEWLINE# #INDENT# self . queryIntentsWeights [ q ] = ( sum ( intentWeights ) / len ( intentWeights ) )