Skip to content

Commit 54d96d1

Browse files
authored
Merge pull request #41 from Azure-Samples/tyclintw/add-workshop-demo
Initial commit of the workshop demo
2 parents 070b458 + 2de521d commit 54d96d1

File tree

3 files changed

+129
-0
lines changed

3 files changed

+129
-0
lines changed

demos/workshop-demo/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Real World Reinforcement Learning Workshop
2+
3+
## Abstract
4+
Microsoft recently announced the Azure Cognitive Service, Personalizer, aimed at democratizing real world reinforcement learning for content personalization. Its goal is to make reinforcement learning accessible to everyone, not just machine learning experts. Personalizer is the result of a successful partnership between Microsoft Research and Azure Cognitive Services aimed at rapid technology transfer and innovation.
5+
6+
In this workshop you will learn the theory behind contextual bandits and how this applies to content personalization. We will walk you through setting up the service, writing your first application, and optimizing the policy using offline optimization.
7+
8+
## Workshop Instructions
9+
1. [Create free Azure/Microsoft account](https://azure.microsoft.com/en-us/free/)
10+
2. Provision free instance of Personalizer
11+
1. Go to [Azure Portal](https://portal.azure.com)
12+
2. Search for "Cogitive Services"
13+
3. On Cognitive Services page click "Add"
14+
4. Search for "Personalizer" and click "Create"
15+
3. Install Python Client
16+
```pip install azure.cognitiveservices.personalizer```
17+
3. Paste your endpoint into [line 34 of ./demo/demo.py](https://github.com/Azure-Samples/cognitive-services-personalizer-samples/blob/master/demos/workshop-demo/demo.py#L34)
18+
4. Paste your key into [line 35 of ./demo/demo.py](https://github.com/Azure-Samples/cognitive-services-personalizer-samples/blob/master/demos/workshop-demo/demo.py#L35)
19+
5. run `python ./demo/demo.py`
20+
21+
## Next steps
22+
- [Personalizer](https://azure.microsoft.com/en-us/services/cognitive-services/personalizer/)
23+
- [Personalizer docs](https://docs.microsoft.com/en-us/azure/cognitive-services/personalizer/)
24+
- How can personalization be integrated into what you work on day to day?

demos/workshop-demo/demo.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
from azure.cognitiveservices.personalizer import PersonalizerClient
2+
import azure.cognitiveservices.personalizer.models as models
3+
from msrest.authentication import CognitiveServicesCredentials
4+
from helpers import SlidingAverage
5+
6+
from datetime import datetime
7+
from random import randint
8+
9+
def run(user_preference, actions, client, duration_sec):
10+
start = datetime.now()
11+
ctr = SlidingAverage(window_size = 20)
12+
while (datetime.now() - start).total_seconds() < duration_sec:
13+
index = randint(0, len(user_preference) - 1)
14+
15+
user = user_preference[index][0]
16+
preference = user_preference[index][1]
17+
18+
request=models.RankRequest(
19+
context_features=user,
20+
actions=actions
21+
)
22+
response=client.rank(request)
23+
reward = 1.0 if response.reward_action_id==preference else 0.0
24+
client.events.reward(event_id=response.event_id, value=reward)
25+
26+
ctr.update(reward)
27+
print('CTR: ' + str(ctr.get()))
28+
for action in response.ranking:
29+
print(action.id + ': ' + str(action.probability))
30+
31+
32+
def main():
33+
34+
client = PersonalizerClient(endpoint="", # Put your endpoint here
35+
credentials=CognitiveServicesCredentials("")) # Put your credentials here
36+
37+
#Available content
38+
actions=[
39+
models.RankableAction(
40+
id='politics',
41+
features=[{'topic': 'politics'}]),
42+
models.RankableAction(
43+
id='sports',
44+
features=[{'topic': 'sports'}]),
45+
models.RankableAction(
46+
id='music',
47+
features=[{'topic': 'music'}]
48+
)]
49+
50+
#User features
51+
Tom = {'name': 'Tom'}
52+
Anna = {'name': 'Anna'}
53+
54+
#Time features
55+
Monday = {'day': 'Monday'}
56+
Sunday = {'day': 'Sunday'}
57+
58+
context = [Tom, Monday]
59+
60+
request=models.RankRequest(
61+
context_features=context,
62+
actions=actions
63+
)
64+
65+
response=client.rank(request)
66+
# Show content to user, evaluate and provide reward back to service
67+
reward = 1.0
68+
client.events.reward(event_id=response.event_id, value=reward)
69+
70+
# Since we are doing cold start and there is no model, all probabilities are the same
71+
for action in response.ranking:
72+
print(action.id + ': ' + str(action.probability))
73+
74+
75+
# Tom and Anna have certain preferences what to read on Monday and Sunday
76+
scenario = [([Tom, Monday], 'politics'),
77+
([Tom, Sunday], 'music'),
78+
([Anna, Monday], 'sports'),
79+
([Anna, Sunday], 'politics')]
80+
81+
run(scenario, actions, client, 5400)
82+
83+
# Olympics started and both Tom and Anna are following sport news during weekend
84+
scenario = [([Tom, Monday], 'politics'),
85+
([Tom, Sunday], 'sports'),
86+
([Anna, Monday], 'sports'),
87+
([Anna, Sunday], 'sports')]
88+
run(scenario, actions, client, 5400)
89+
90+
if __name__== '__main__':
91+
main()

demos/workshop-demo/helpers.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class SlidingAverage:
2+
def __init__(self, window_size):
3+
self.index = 0
4+
self.values = [0] * window_size
5+
6+
def _previous(self):
7+
return self.values[(self.index + len(self.values) - 1) % len(self.values)]
8+
9+
def update(self, value):
10+
self.values[self.index] = self._previous() + value
11+
self.index = (self.index + 1) % len(self.values)
12+
13+
def get(self):
14+
return (self._previous() - self.values[self.index]) / (len(self.values) - 1)

0 commit comments

Comments
 (0)