首页 > 解决方案 > 如何在 Python 中将文章推荐器建模为 Q 学习问题

问题描述

我想在 Python 中使用 Q-learning 实现一篇文章推荐器。例如,我们的数据集有四个类别的文章,包括健康、体育、新闻和生活方式,每个类别有 10 篇文章(总共 40 篇文章)。这个想法是向用户显示一些随机文章(例如,五篇文章,它们可以来自任何类别)并接收他/她的反馈。然后,代理了解用户的偏好(即文章的类别)并再次推荐一些相关的文章。

要将其表述为 RL 问题,我知道我应该定义动作、状态和奖励函数。研究了一些文章后,我想出了:

行动:推荐一篇文章;

状态:这个我不是很清楚,但是我从其他文章中了解到状态可以是:

a) 用户最近研究过的文章的踪迹;b) 用户兴趣(不确定这如何成为一种状态);

奖励:非常简单的奖励。如果用户研究推荐的文章,它可以是 +1 或无用的推荐 -1。

对于 Q 学习部分,我不确定如何制作包含状态作为行和动作作为列的 Q 表。

对于其他简单的 RL 问题,比如 MountainCar,开发 q-table 并不是那么困难,但是这里的状态不是很清楚的方式让我感到困惑。

如果您能帮助我提出一个解决方案,将其表述为 RL 问题,并用几行代码来启发我如何开始编写它,我将不胜感激。

标签: pythonreinforcement-learningq-learningrecommender-systems

解决方案


如果您不确定状态,您可以使用多臂老虎机算法,您只需采取行动并获得奖励。

如果您想使用用户最近研究过的文章的踪迹,那么您可以使用考虑状态的上下文强盗算法。因为这一集只是一步,所以它更多的是上下文强盗问题而不是强化学习。

虽然,你可以用这样的东西来训练。

state = env.reset()
state_buffer = []
# now append the history of the user in state_buffer list
# so now state_buffer has the most recent five states observed
# here state can be a vector of size four with entries being one if that article was read by the user in that time-step
# if the previous article read by the user is health then [1,0,0,0]
# if the previous article read by the user is sports then [0,1,0,0]
# action is which article to show next

# run for 1000 episodes
for i in range(1000):
    action = policy.select_action(state_buffer)
    next_state,reward,done,info = env.step(action)
    policy.update()
    state_buffer.pop(0)
    state_buffer.append(next_state)
# NOTE: you will have to implement policy class which has a function approximator (neural-network)
# policy.update() does the Q-learning update

另外,您可以浏览博客。


推荐阅读