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
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
15 additions
and
11 deletions
+15
-11
Linucb.py
linucb/core/Linucb.py
+7
-5
generate_content_user_feature.py
tools/generate_content_user_feature.py
+8
-6
No files found.
linucb/core/Linucb.py
View file @
4d237157
...
@@ -21,7 +21,7 @@ import time
...
@@ -21,7 +21,7 @@ import time
class
LinUCB
:
class
LinUCB
:
d
=
3
d
=
2
alpha
=
0.1
alpha
=
0.1
r1
=
10
r1
=
10
r0
=
-
0.1
r0
=
-
0.1
...
@@ -181,9 +181,10 @@ class LinUCB:
...
@@ -181,9 +181,10 @@ class LinUCB:
else
:
else
:
r
=
cls
.
r0
r
=
cls
.
r0
if
user_features_array
:
# if user_features_array:
xaT
=
np
.
array
(
user_features_array
)
# xaT = np.array(user_features_array)
else
:
# else:
# xaT = np.array([user_features])
xaT
=
np
.
array
([
user_features
])
xaT
=
np
.
array
([
user_features
])
xa
=
np
.
transpose
(
xaT
)
xa
=
np
.
transpose
(
xaT
)
...
@@ -191,7 +192,8 @@ class LinUCB:
...
@@ -191,7 +192,8 @@ class LinUCB:
if
not
ori_redis_tag_data
:
if
not
ori_redis_tag_data
:
LinUCB
.
init_device_id_linucb_info
(
redis_client
,
redis_name_linucb_matrix
,[
content_id
])
LinUCB
.
init_device_id_linucb_info
(
redis_client
,
redis_name_linucb_matrix
,[
content_id
])
else
:
ori_redis_tag_data
=
redis_cli
.
hget
(
redis_name_linucb_matrix
,
content_id
)
ori_redis_tag_dict
=
pickle
.
loads
(
ori_redis_tag_data
)
ori_redis_tag_dict
=
pickle
.
loads
(
ori_redis_tag_data
)
new_Aa_matrix
=
ori_redis_tag_dict
[
"Aa"
]
+
np
.
dot
(
xa
,
xaT
)
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_AaI_matrix
=
np
.
linalg
.
solve
(
new_Aa_matrix
,
np
.
identity
(
cls
.
d
))
...
...
tools/generate_content_user_feature.py
View file @
4d237157
...
@@ -139,7 +139,8 @@ class Generate_Feature_Info(object):
...
@@ -139,7 +139,8 @@ class Generate_Feature_Info(object):
if
len
(
offi_tags_v3
)
>
0
:
if
len
(
offi_tags_v3
)
>
0
:
try
:
try
:
tag_label
=
label_encoder
.
transform
(
offi_tags_v3
)[
0
]
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
,
redis_client
.
hset
(
redis_name_content_linucb_feature
,
diary_id
,
json
.
dumps
(
diary_feature_list
))
json
.
dumps
(
diary_feature_list
))
except
:
except
:
...
@@ -171,7 +172,8 @@ class Generate_Feature_Info(object):
...
@@ -171,7 +172,8 @@ class Generate_Feature_Info(object):
user_max_score_tag
=
sorted_user_portrait_list
[
0
][
0
]
.
encode
(
"utf-8"
)
user_max_score_tag
=
sorted_user_portrait_list
[
0
][
0
]
.
encode
(
"utf-8"
)
try
:
try
:
user_tag_label
=
label_encoder
.
transform
([
user_max_score_tag
])[
0
]
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
))
redis_client
.
hset
(
redis_name_user_linucb_feature
,
device_id
,
json
.
dumps
(
user_feature_list
))
except
:
except
:
logging
.
error
(
"catch exception,err_msg:
%
s"
%
traceback
.
format_exc
())
logging
.
error
(
"catch exception,err_msg:
%
s"
%
traceback
.
format_exc
())
...
@@ -218,7 +220,7 @@ if __name__ == "__main__":
...
@@ -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
))
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"
linucb_matrix_redis_name
=
"strategy:linucb:matrix:content_type:diary"
...
@@ -235,19 +237,19 @@ if __name__ == "__main__":
...
@@ -235,19 +237,19 @@ if __name__ == "__main__":
item_list
=
line
.
split
(
","
)
item_list
=
line
.
split
(
","
)
device_id
=
item_list
[
3
]
device_id
=
item_list
[
3
]
diary_id
=
item_list
[
4
]
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
)
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
)
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
:
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
(
user_feature_list
[
0
])
offi_context_feature_list
.
append
(
content_feature_list
[
0
])
offi_context_feature_list
.
append
(
content_feature_list
[
0
])
print
(
offi_context_feature_list
)
print
(
offi_context_feature_list
)
print
(
"*"
*
50
)
print
(
"*"
*
50
)
LinUCB
.
update_linucb_info
(
user_features_array
=
offi_context_feature_list
,
reward
=
1
,
content_id
=
diary_id
,
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
)
redis_name_linucb_matrix
=
linucb_matrix_redis_name
,
redis_cli
=
redis_client
)
diary_fd
.
close
()
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