Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
G
gm_mab
Project
Project
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
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
rank
gm_mab
Commits
4d237157
Commit
4d237157
authored
Apr 22, 2020
by
段英荣
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
调试
parent
1b433ee2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
31 additions
and
27 deletions
+31
-27
Linucb.py
linucb/core/Linucb.py
+20
-18
generate_content_user_feature.py
tools/generate_content_user_feature.py
+11
-9
No files found.
linucb/core/Linucb.py
View file @
4d237157
...
...
@@ -21,7 +21,7 @@ import time
class
LinUCB
:
d
=
3
d
=
2
alpha
=
0.1
r1
=
10
r0
=
-
0.1
...
...
@@ -181,30 +181,32 @@ class LinUCB:
else
:
r
=
cls
.
r0
if
user_features_array
:
xaT
=
np
.
array
(
user_features_array
)
else
:
xaT
=
np
.
array
([
user_features
])
# if user_features_array:
# xaT = np.array(user_features_array)
# else:
# xaT = np.array([user_features])
xaT
=
np
.
array
([
user_features
])
xa
=
np
.
transpose
(
xaT
)
ori_redis_tag_data
=
redis_cli
.
hget
(
redis_name_linucb_matrix
,
content_id
)
if
not
ori_redis_tag_data
:
LinUCB
.
init_device_id_linucb_info
(
redis_client
,
redis_name_linucb_matrix
,[
content_id
])
else
:
ori_redis_tag_dict
=
pickle
.
loads
(
ori_redis_tag_data
)
new_Aa_matrix
=
ori_redis_tag_dict
[
"Aa"
]
+
np
.
dot
(
xa
,
xaT
)
new_AaI_matrix
=
np
.
linalg
.
solve
(
new_Aa_matrix
,
np
.
identity
(
cls
.
d
))
new_ba_matrix
=
ori_redis_tag_dict
[
"ba"
]
+
r
*
xa
user_tag_dict
=
{
"Aa"
:
new_Aa_matrix
,
"ba"
:
new_ba_matrix
,
"AaI"
:
new_AaI_matrix
,
"theta"
:
np
.
dot
(
new_AaI_matrix
,
new_ba_matrix
)
}
ori_redis_tag_data
=
redis_cli
.
hget
(
redis_name_linucb_matrix
,
content_id
)
ori_redis_tag_dict
=
pickle
.
loads
(
ori_redis_tag_data
)
new_Aa_matrix
=
ori_redis_tag_dict
[
"Aa"
]
+
np
.
dot
(
xa
,
xaT
)
new_AaI_matrix
=
np
.
linalg
.
solve
(
new_Aa_matrix
,
np
.
identity
(
cls
.
d
))
new_ba_matrix
=
ori_redis_tag_dict
[
"ba"
]
+
r
*
xa
user_tag_dict
=
{
"Aa"
:
new_Aa_matrix
,
"ba"
:
new_ba_matrix
,
"AaI"
:
new_AaI_matrix
,
"theta"
:
np
.
dot
(
new_AaI_matrix
,
new_ba_matrix
)
}
redis_cli
.
hset
(
redis_name_linucb_matrix
,
content_id
,
pickle
.
dumps
(
user_tag_dict
))
redis_cli
.
hset
(
redis_name_linucb_matrix
,
content_id
,
pickle
.
dumps
(
user_tag_dict
))
else
:
logging
.
warning
(
"not standard linucb reward"
)
return
True
...
...
tools/generate_content_user_feature.py
View file @
4d237157
...
...
@@ -139,7 +139,8 @@ class Generate_Feature_Info(object):
if
len
(
offi_tags_v3
)
>
0
:
try
:
tag_label
=
label_encoder
.
transform
(
offi_tags_v3
)[
0
]
diary_feature_list
=
onehot_encoder
.
transform
([[
tag_label
]])
.
toarray
()
.
tolist
()
# diary_feature_list = onehot_encoder.transform([[tag_label]]).toarray().tolist()
diary_feature_list
=
[
tag_label
]
redis_client
.
hset
(
redis_name_content_linucb_feature
,
diary_id
,
json
.
dumps
(
diary_feature_list
))
except
:
...
...
@@ -171,7 +172,8 @@ class Generate_Feature_Info(object):
user_max_score_tag
=
sorted_user_portrait_list
[
0
][
0
]
.
encode
(
"utf-8"
)
try
:
user_tag_label
=
label_encoder
.
transform
([
user_max_score_tag
])[
0
]
user_feature_list
=
onehot_encoder
.
transform
([[
user_tag_label
]])
.
toarray
()
.
tolist
()
# user_feature_list = onehot_encoder.transform([[user_tag_label]]).toarray().tolist()
user_feature_list
=
[
user_tag_label
]
redis_client
.
hset
(
redis_name_user_linucb_feature
,
device_id
,
json
.
dumps
(
user_feature_list
))
except
:
logging
.
error
(
"catch exception,err_msg:
%
s"
%
traceback
.
format_exc
())
...
...
@@ -218,7 +220,7 @@ if __name__ == "__main__":
onehot_encoder
=
Generate_Feature_Info
.
get_tagv3_label_onehot
(
label_encoder
=
label_encoder
,
tagv3_name_list
=
list
(
tagv3_name_set
))
#
Generate_Feature_Info.generate_content_feature_to_redis(label_encoder,onehot_encoder)
Generate_Feature_Info
.
generate_content_feature_to_redis
(
label_encoder
,
onehot_encoder
)
linucb_matrix_redis_name
=
"strategy:linucb:matrix:content_type:diary"
...
...
@@ -235,20 +237,20 @@ if __name__ == "__main__":
item_list
=
line
.
split
(
","
)
device_id
=
item_list
[
3
]
diary_id
=
item_list
[
4
]
#
Generate_Feature_Info.generate_user_feature_to_redis(device_id, label_encoder,onehot_encoder)
Generate_Feature_Info
.
generate_user_feature_to_redis
(
device_id
,
label_encoder
,
onehot_encoder
)
user_feature_list
=
Generate_Feature_Info
.
get_user_feature_by_device_id
(
device_id
)
content_feature_list
=
Generate_Feature_Info
.
get_content_feature
(
diary_id
)
offi_context_feature_list
=
[
1
]
offi_context_feature_list
=
[]
if
len
(
user_feature_list
)
>
0
and
len
(
content_feature_list
)
>
0
:
offi_context_feature_list
.
append
(
user_feature_list
[
0
])
offi_context_feature_list
.
append
(
content_feature_list
[
0
])
print
(
offi_context_feature_list
)
print
(
"*"
*
50
)
LinUCB
.
update_linucb_info
(
user_features_array
=
offi_context_feature_list
,
reward
=
1
,
content_id
=
diary_id
,
redis_name_linucb_matrix
=
linucb_matrix_redis_name
,
redis_cli
=
redis_client
)
print
(
offi_context_feature_list
)
print
(
"*"
*
50
)
LinUCB
.
update_linucb_info
(
user_features
=
offi_context_feature_list
,
reward
=
1
,
content_id
=
diary_id
,
redis_name_linucb_matrix
=
linucb_matrix_redis_name
,
redis_cli
=
redis_client
)
diary_fd
.
close
()
...
...
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