TASK:冷启动热度推荐回退;

This commit is contained in:
shahaibo
2025-06-10 13:38:28 +08:00
parent 18c95a88b9
commit cee4f033e5
2 changed files with 43 additions and 33 deletions

View File

@@ -40,31 +40,41 @@ def softmax(scores):
sum_exp = sum(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]:
"""根据预加载热度向量推荐(冷启动)"""
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]

View File

@@ -527,12 +527,12 @@ def update_heat_matrices():
if __name__ == "__main__":
try:
update_user_matrices()
update_heat_matrices()
# scheduler = BlockingScheduler()
# scheduler.add_job(update_user_matrices, 'cron', hour=12, timezone='Asia/Shanghai')
# logging.info("定时任务已启动每天12:00执行")
# scheduler.start()
# update_user_matrices()
# update_heat_matrices()
scheduler = BlockingScheduler()
scheduler.add_job(update_user_matrices, 'cron', hour=12, timezone='Asia/Shanghai')
logging.info("定时任务已启动每天12:00执行")
scheduler.start()
except KeyboardInterrupt:
logging.info("定时任务已停止")
except Exception as e: