lolamontes69

Ch4 Ex7-Collective Intelligence (Different training options)

Jun 28th, 2013
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.72 KB | None | 0 0
  1. """ Chapter 4 Exercise 7: Different training options.
  2.  
  3.    The neural network is trained with a set of 0s for all the URLs that a user did not click, and a 1 for a URL that she did click. Alter the training function so that it works instead for an application where users get to rate results from 1 to 5.
  4.  
  5.    In this version of trainquery the user has the option of rating the one selected url.
  6.  
  7.    Example usage: mynet.trainquery([wWorld,wBank],[uWorldBank,uRiver,uEarth],uWorldBank,3)
  8. """
  9.  
  10.     def trainquery(self,wordids,urlids,selectedurl,rating=1):
  11.         ratingvalue = [1.0, 0.8, 0.6, 0.4, 0.2]
  12.         self.generatehiddennode(wordids,urlids)
  13.         self.setupnetwork(wordids,urlids)
  14.         self.feedforward()
  15.         targets=[0.0]*len(urlids)
  16.         targets[urlids.index(selectedurl)]=ratingvalue[int(rating)-1]
  17.         error = self.backPropagate(targets)
  18.         self.updatedatabase()
  19.  
  20. """ In the next version of trainquery the user has the option of rating all urls. (Unrated URLs are trained with 0)
  21.    (The rated urls and their ratings are passed here as seperate lists but could also be passed as a dictionary)
  22.  
  23.    Example usage: mynet.trainquery([wWorld,wBank],[uWorldBank,uRiver,uEarth],[uWorldBank,uEarth],[1,4])
  24. """
  25.  
  26.     def trainquery(self,wordids,urlids,ratedurls,ratings):
  27.         ratingvalue = [1.0, 0.8, 0.6, 0.4, 0.2]
  28.         self.generatehiddennode(wordids,urlids)
  29.         self.setupnetwork(wordids,urlids)
  30.         self.feedforward()
  31.         targets=[0.0]*len(urlids)
  32.         for selectedurl in range(len(ratedurls)):
  33.             targets[urlids.index(ratedurls[selectedurl])]=ratingvalue[int(ratings[selectedurl])-1]
  34.         error = self.backPropagate(targets)
  35.         self.updatedatabase()
Advertisement
Add Comment
Please, Sign In to add comment