-
Notifications
You must be signed in to change notification settings - Fork 834
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #78 from Maximophone/54-epsilon_greedy
54 epsilon greedy
- Loading branch information
Showing
19 changed files
with
705 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import random | ||
import numpy as np | ||
|
||
__version__ = "v1.1" | ||
|
||
def n_success_failures(features,reward): | ||
n_predictions = features.shape[0] | ||
n_success = int(reward*n_predictions) | ||
n_failures = n_predictions - n_success | ||
return n_success, n_failures | ||
|
||
class EpsilonGreedy(object): | ||
|
||
def __init__(self,n_branches=None,epsilon=0.1,verbose=False): | ||
print "Starting Epsilon Greedy Microservice, version {}".format(__version__) | ||
if n_branches is None: | ||
raise Exception("n_branches parameter must be given") | ||
self.verbose = verbose | ||
self.epsilon = epsilon | ||
self.best_branch = 0 | ||
self.branches_success = [0 for _ in range(n_branches)] | ||
self.branches_tries = [0 for _ in range(n_branches)] | ||
self.n_branches = n_branches | ||
if self.verbose: | ||
print "Router initialised" | ||
print "# branches:",self.n_branches | ||
print "Epsilon:",self.epsilon | ||
|
||
def route(self,features,feature_names): | ||
x = random.random() | ||
best_branch = self.best_branch | ||
other_branches = [i for i in range(self.n_branches) if i!=best_branch] | ||
selected_branch = best_branch if x>self.epsilon else random.choice(other_branches) | ||
if self.verbose: | ||
print "Routing" | ||
print "Current best branch:",best_branch | ||
print "Selected branch:",selected_branch | ||
return selected_branch | ||
|
||
def send_feedback(self,features,feature_names,routing,reward,truth): | ||
if self.verbose: | ||
print "Training" | ||
print "Prev success #", self.branches_success | ||
print "Prev tries #", self.branches_tries | ||
print "Prev best branch:", self.best_branch | ||
n_success, n_failures = n_success_failures(features,reward) | ||
self.branches_success[routing] += n_success | ||
self.branches_tries[routing] += n_success + n_failures | ||
perfs = [ | ||
(self.branches_success[i]+1)/float(self.branches_tries[i]+1) | ||
for i | ||
in range(self.n_branches) | ||
] | ||
self.best_branch = np.argmax(perfs) | ||
if self.verbose: | ||
print "New success #", self.branches_success | ||
print "New tries #", self.branches_tries | ||
print "New best branch:",self.best_branch | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
numpy==1.11.2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.