TASK:冷启动热度推荐回退;
This commit is contained in:
@@ -40,31 +40,41 @@ def softmax(scores):
|
|||||||
sum_exp = sum(exp_scores)
|
sum_exp = sum(exp_scores)
|
||||||
return [s / sum_exp for s in exp_scores]
|
return [s / sum_exp for s in exp_scores]
|
||||||
|
|
||||||
|
# def get_random_recommendations(category: str, num: int) -> List[str]:
|
||||||
|
# """根据预加载热度向量推荐(冷启动)"""
|
||||||
|
# try:
|
||||||
|
# heat_data = matrix_data.get("heat_data", {})
|
||||||
|
#
|
||||||
|
# if category not in heat_data:
|
||||||
|
# raise ValueError(f"热度数据缺少类别 {category},使用随机推荐")
|
||||||
|
#
|
||||||
|
# heat_dict = heat_data[category] # {url: score}
|
||||||
|
# urls = list(heat_dict.keys())
|
||||||
|
# scores = list(heat_dict.values())
|
||||||
|
#
|
||||||
|
# if not urls:
|
||||||
|
# raise ValueError("该类别下无热度记录,使用随机推荐")
|
||||||
|
#
|
||||||
|
# probs = softmax(scores)
|
||||||
|
# sample_size = min(num, len(urls))
|
||||||
|
# sampled_urls = random.choices(urls, weights=probs, k=sample_size)
|
||||||
|
#
|
||||||
|
# return sampled_urls
|
||||||
|
#
|
||||||
|
# except Exception as e:
|
||||||
|
# # 回退:完全随机推荐
|
||||||
|
# all_iids = list(matrix_data["iid_to_sketch"].keys())
|
||||||
|
# category_iids = matrix_data["category_to_iids"].get(category, all_iids)
|
||||||
|
# sample_size = min(num, len(category_iids))
|
||||||
|
# sampled = np.random.choice(category_iids, size=sample_size, replace=False)
|
||||||
|
# return [matrix_data["iid_to_sketch"][iid] for iid in sampled]
|
||||||
|
|
||||||
def get_random_recommendations(category: str, num: int) -> List[str]:
|
def get_random_recommendations(category: str, num: int) -> List[str]:
|
||||||
"""根据预加载热度向量推荐(冷启动)"""
|
"""全品类随机推荐"""
|
||||||
try:
|
|
||||||
heat_data = matrix_data.get("heat_data", {})
|
|
||||||
|
|
||||||
if category not in heat_data:
|
|
||||||
raise ValueError(f"热度数据缺少类别 {category},使用随机推荐")
|
|
||||||
|
|
||||||
heat_dict = heat_data[category] # {url: score}
|
|
||||||
urls = list(heat_dict.keys())
|
|
||||||
scores = list(heat_dict.values())
|
|
||||||
|
|
||||||
if not urls:
|
|
||||||
raise ValueError("该类别下无热度记录,使用随机推荐")
|
|
||||||
|
|
||||||
probs = softmax(scores)
|
|
||||||
sample_size = min(num, len(urls))
|
|
||||||
sampled_urls = random.choices(urls, weights=probs, k=sample_size)
|
|
||||||
|
|
||||||
return sampled_urls
|
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
# 回退:完全随机推荐
|
|
||||||
all_iids = list(matrix_data["iid_to_sketch"].keys())
|
all_iids = list(matrix_data["iid_to_sketch"].keys())
|
||||||
|
# 优先从当前品类选择
|
||||||
category_iids = matrix_data["category_to_iids"].get(category, all_iids)
|
category_iids = matrix_data["category_to_iids"].get(category, all_iids)
|
||||||
|
# 确保不超出实际数量
|
||||||
sample_size = min(num, len(category_iids))
|
sample_size = min(num, len(category_iids))
|
||||||
sampled = np.random.choice(category_iids, size=sample_size, replace=False)
|
sampled = np.random.choice(category_iids, size=sample_size, replace=False)
|
||||||
return [matrix_data["iid_to_sketch"][iid] for iid in sampled]
|
return [matrix_data["iid_to_sketch"][iid] for iid in sampled]
|
||||||
|
|||||||
@@ -527,12 +527,12 @@ def update_heat_matrices():
|
|||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
try:
|
try:
|
||||||
update_user_matrices()
|
# update_user_matrices()
|
||||||
update_heat_matrices()
|
# update_heat_matrices()
|
||||||
# scheduler = BlockingScheduler()
|
scheduler = BlockingScheduler()
|
||||||
# scheduler.add_job(update_user_matrices, 'cron', hour=12, timezone='Asia/Shanghai')
|
scheduler.add_job(update_user_matrices, 'cron', hour=12, timezone='Asia/Shanghai')
|
||||||
# logging.info("定时任务已启动,每天12:00执行")
|
logging.info("定时任务已启动,每天12:00执行")
|
||||||
# scheduler.start()
|
scheduler.start()
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
logging.info("定时任务已停止")
|
logging.info("定时任务已停止")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|||||||
Reference in New Issue
Block a user