台灣線上娛樂城假期交流評價論壇

標題: 强化學習(一):多臂老虎機 [打印本頁]

作者: admin    時間: 2023-9-19 13:09
標題: 强化學習(一):多臂老虎機
向用户随機展現两张圖片,一段時候後,哪张圖片的點击率高,就用哪张,這就是A/B 塑形收腹背心,Testing。下面用代码来摹拟這個進程。

假如每张圖片是不是被點击從命伯努利散布:

  1. class BernoulliArm(object):
  2. def __init__(self, p):
  3. self.p = p
  4. def step(self):
  5. if random.random() < self.p:
  6. return 1.0
  7. else:
  8. return 0.0
複製代碼

現有5张圖片,别離從命几率為0.1,0.1,0.1,0.1,0.9的伯努利散布,利用A/B Testing摹拟以下

横轴為展現次数,纵轴為點击率。此中點击率远低于0.9。

A/B Testing中的很多名词,均可與多臂山君機中的對應:

設 R_n 為利用n次山君機後的均匀回報, r_{n+1} 為第n+1次的回報,则

\begin{align} R_{n+1} &= \frac{R_n \times n + r_{n+1}}{n + 1}\\ &= R_n + \frac{r_{n+1} - R_n}{n + 1}\\ \end{align}\\

實現以下

  1. class EpsilonGreedy(object):
  2. def __init__(self, arm_n, epsilon):
  3. self.arm_n = arm_n
  4. self.epsilon = epsilon
  5. self.values = [0.0 for _ in range(self.arm_n)]
  6. self.counts = [0.0 for _ in range(self.arm_n)]
  7. def update(self, arm, reward):
  8. self.counts[arm] += 1
  9. self.values[arm] += (reward - self.values[arm]) / self.counts[arm]
複製代碼

與A/B Testing比拟,Epsilon Greedy節制了随機利用山君機的次数:

  1. class EpsilonGreedy(object):
  2. def pull(self):
  3. if random.random() < self.epsilon:
  4. return random.choice(rang去疤藥膏,e(self.arm_n))
  5. else:
  6. m = max(self.values)
  7. return self.values.index(m)
複製代碼

此中有 1-\epsilon 的几率,Epsilon Greedy将會利用汗青均匀回報最高的山君機。

下面摹拟 \epsilon = 0.1, 0.25, 0.75, 1的情景:

此中A/B Testing等价于 \epsilon = 1 的情景。

softmax和UCB1的代码位于algorithm.py。下面比力A/B Testing,Epsilon Greedy,softmax和UCB1:

設多臂山君機所有arm组成的调集為 \mathcal{A} ,而且對應的reward從命散布 \{R_t|A_t=a\}_{a\in\mathcal{A}} 。記 \mu_a:=\mathbb{E}[R_t|A_t=a],~\mu^*:=\max_{a\in\mathcal{A}}\mathbb{E}[R_t|A_t=a],\Delta_a:=\mu^*-u_a 。假如有以下進程:

A_1,R_1,\cdots,A_t,R_t,\cdots\\

则多臂山君機算法的目標是最小化total regret:

\begin{align} L_t&=\mathbb{E}[t\mu^*-\sum_{\tau=1}^t\mathbb{E}[R_\tau|A_\tau]]\\ &=(\sum_{\tau=1}^t1)\mu^*-\sum_{\tau=1}^t\mathbb{E}[\mathbb{E}\mathbb[R_\tau|A_\tau]]\\ &=\sum_{\tau=1}^t\sum_{a\in\mat素描,hcal{A}}\pi_\tau(a)\mu^*-\sum_{\tau=1}^t\sum_{a\in\mathcal{A}}\pi_\tau(a)\mathbb{E}[R_\tau|A_\tau=a]\\ &=\sum_{a\in\mathcal{A}}(\sum_{\tau=1}^t\pi_\tau(a))\Delta_a \end{align}\\

\begin{align} L_t&\geq\sum_{a\in\mathcal{A}}\sum_{\tau=1}^t\frac{\epsilon}{|\mathcal{A}|}\Delta_a\\ &=(\frac{\epsilon}{\mathcal{|A|}})(\sum_{a\in\mathcal{A}}\Delta_a)t  \end{align}\\

\begin{align} L_t&\leq\sum_{a\in\mathcal{A}}\sum_{\tau=1}^t(1-\epsilon+\frac{\epsilon}{|\mathcal{A}|})\Delta_a\\ &=(1-\epsilon+\frac{\epsilon}{\mathcal{|A|}})(\sum_{a\in\mathcal{A}}\Delta_a)t \end{align}\\ 即Epsilon Greedy知足 L_t=\Theta(t) 。

强化進修的方针因此最快的速率,找到最優计谋。在强化進修的概念下,多臂山君機算法有

此中reinforcement comparison的思绪雷同于policy gradient。設有parameterized的policy \pi(a|\theta) ,则给定 \theta ,有 \mu^\pi=\sum_a\pi(a|\theta)\mu_a\\

可使用gradient descent更新 \theta 以下

\begin{align} \theta:&=\theta + \nabla_\theta\mu^\pi\\ &=\theta+\sum_a\mu_a\nabla_\theta\pi(a|\theta) \end{align}\\

详细的例子可以拜见:

本文比力了四種多臂山君機的算法:A/B Testin玄關門尺寸,g,Epsilon Greedy,softmax,UCB1。後面三種算法是為了补充A/B Testing的不足,這也阐明了多臂山君機是一門均衡摸索操纵台灣生活, 的藝術。

本文代码位于multi-armed bandit。




歡迎光臨 台灣線上娛樂城假期交流評價論壇 (http://nareatour.com.tw/) Powered by Discuz! X3.3