Commit 4d237157 authored by 段英荣's avatar 段英荣

调试

parent 1b433ee2
...@@ -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,30 +181,32 @@ class LinUCB: ...@@ -181,30 +181,32 @@ 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)
ori_redis_tag_data = redis_cli.hget(redis_name_linucb_matrix, content_id) ori_redis_tag_data = redis_cli.hget(redis_name_linucb_matrix, content_id)
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)
new_Aa_matrix = ori_redis_tag_dict["Aa"] + np.dot(xa, xaT) ori_redis_tag_dict = pickle.loads(ori_redis_tag_data)
new_AaI_matrix = np.linalg.solve(new_Aa_matrix, np.identity(cls.d)) new_Aa_matrix = ori_redis_tag_dict["Aa"] + np.dot(xa, xaT)
new_ba_matrix = ori_redis_tag_dict["ba"] + r*xa 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, user_tag_dict = {
"ba": new_ba_matrix, "Aa": new_Aa_matrix,
"AaI": new_AaI_matrix, "ba": new_ba_matrix,
"theta": np.dot(new_AaI_matrix, 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: else:
logging.warning("not standard linucb reward") logging.warning("not standard linucb reward")
return True return True
......
...@@ -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,20 +237,20 @@ if __name__ == "__main__": ...@@ -235,20 +237,20 @@ 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()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment