Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
ml2_MiniAssignments
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
20200318029
ml2_MiniAssignments
Commits
10422f3e
Commit
10422f3e
authored
Aug 05, 2020
by
20200318029
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
homework7
parent
1f2efa54
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
6 additions
and
9 deletions
+6
-9
homework7/LinUCB-Homework.ipynb
+0
-0
homework7/LinUCB-Homework.py
+6
-9
No files found.
homework7/LinUCB-Homework.ipynb
View file @
10422f3e
This source diff could not be displayed because it is too large. You can
view the blob
instead.
homework7/LinUCB-Homework.py
View file @
10422f3e
...
...
@@ -223,7 +223,7 @@ class bandit_evaluator(object):
"""
def
__init__
(
self
):
self
.
bandit
s
=
[]
self
.
bandit
=
None
self
.
cum_rewards
=
0
self
.
ctr_history
=
[]
...
...
@@ -241,14 +241,11 @@ class bandit_evaluator(object):
"""
def
calc_ctr
(
self
,
x
,
action
,
t
):
assert
t
>
0
bandit
=
self
.
bandits
[
-
1
]
pred_act
=
bandit
.
predict
(
x
)
pred_act
=
self
.
bandit
.
predict
(
x
)
### todo
if
len
(
self
.
ctr_history
):
hist
=
self
.
ctr_history
[
-
1
]
*
(
t
-
1
)
else
:
hist
=
0
ctr
=
(
hist
+
int
(
pred_act
==
action
))
/
t
if
pred_act
==
action
:
self
.
cum_rewards
+=
1
ctr
=
self
.
cum_rewards
/
t
self
.
ctr_history
.
append
(
ctr
)
return
ctr
...
...
@@ -277,6 +274,7 @@ def train(file, steps, alpha, nArms, d):
bandit
=
LinUCB
(
alpha
,
d
,
nArms
)
# initialize bandit evaluator
evaluator
=
bandit_evaluator
()
evaluator
.
bandit
=
bandit
for
t
in
range
(
steps
):
x
=
getContext
(
data
,
t
)
...
...
@@ -290,7 +288,6 @@ def train(file, steps, alpha, nArms, d):
bandit
.
arms
[
arm
]
.
update_alpha
(
3
,
t
)
if
t
>
0
:
# evaluate current bandit algorithm
evaluator
.
bandits
.
append
(
bandit
)
ctr
=
evaluator
.
calc_ctr
(
x
,
action
,
t
)
if
t
%
100
==
0
:
print
(
"Step:"
,
t
,
end
=
""
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment