[Chapter 4] Reinforcement Learning (2) Model-Free Method

Model-Free RL Method

In model-based method, we need firstly model the environment by learning/estimating the transition and reward functions. However, in model-free method, we consider learning the value/utility functions V(s) or U(s) or action-value functions Q(s,a) directly without learning P(s^′│s,a) and R(s,a,s^′). There are also two main kinds of approaches, one is called Monte Carlo (MC) Learning and another one is called Temporal Difference (TD) Learning. The main difference between them is how often to update the policy.

Monte Carlo Learning

Firstly, we consider the prediction problem, how to compute/estimate the U(s) or Q(s,a)?

In Monte Carlo learning, the agent executes a set of trails based on the GLIE scheme (trading the exploration and exploitation off). In our example environment, a trail starts from location (1,1) and ends in either terminal states (4,2) or (4,3), e.g., shown below with the rewards in subscripts:

image

The trails collected by the agent are samples or data, on which we can learn based. You must remember the Bellman equation for the utility function, and we have another version of it for the Q-function:

U(s)=R(s)+{\gamma} max_{a \in A(s)}⁡ \sum_{s^′}{P(s^′│s,aV(s^′)}

Q(s,a)=R(s)+{\gamma} \sum_{s^′}{P(s^′│s,a) max_{a^′}⁡{Q(s^′,a^′)}}

where {\gamma} is the discounted factor.

Using these two equations, we can compute the utility or Q-value for each state, for example, in the trail 3, the return or reward-to-go of state (1,1) can be computed as (suppose {\gamma}=0.9 here):

U([1,1])=−0.04+0.9 \times (−0.04+0.9 \times (−0.04+0.9 \times (−0.04+0.9 \times (−1))))=−0.794

It can be an estimation of the return of state (1,1), while we have many many trials/samples, take an average value of all estimations, then we can get an approximate value for each state. And in infinitely many trails, sample average will converge to the expected value.

Here, actually you can see that in trail 1, location (1,2) appears two times, in this case, if we only use the first one, it becomes a first visit method, or if we use both of the samples, it becomes a every visit method, we will focus on the first visit method in this section.

Assume state s is encountered k times giving k returns. We can rewrite the average, U_k(s) of k returns G_1(s), ..., G_k(s) as:

U_k(s)=U_{k−1}(s)+ \frac{1}{k} (G_k(s)−U_{k−1}(s))

where U_{k−1}(s) is the estimate after receiving k−1 returns, so G_k(s)−U_{k−1}(s) can be considered the prediction error for the k-th return.

In a similar way, we can also choose to learn the Q-functions instead of learning the value functions. After that, for the control problem, we only need to greedy choose the policy by:

{\pi}(s)=arg⁡ max_{a′}{Q(s,a^′)}

There is another problem you may want to know, how to sample trails in MC learning? As we have stated above, it's better to using a GLIE scheme, for example, {\epsilon}-greedy, which means that for a probability of {\epsilon}, using the current policy (computed by the control problem part) to select a current-best action, and for a probability of 1−{\epsilon}, choosing actions randomly.

MC learning is an instance of supervised learning: each example has state as input and observed return as output, so it's simple and unbiased. However, the biggest disadvantage for MC learning is that we need to wait till the end of episode to get one trail and then learn can be done. So let's introduce another method, which can learn after each step -- temporal difference learning.

Temporal Difference Learning

Temporal difference (TD) learning exploits more of the Bellman equation constraints than Monte Carlo learning. For a transition from state s to s′, TD learning does:

U^{\pi}(s) \leftarrow U^{\pi}(s)+ \alpha (R(s)+{\gamma}U^{\pi}(s^′)−U^{\pi}(s))

where {\alpha} is the learning rate and {\gamma} is the discounted factor.

In the formula, we can see that it will increase U^{\pi}(s) if R(s)+{\gamma}U^{\pi}(s^′) is larger than U^{\pi}(s) and decrease it otherwise. So we call R(s)+{\gamma} U^{\pi} (s^′) the TD target and R(s)+{\gamma}U^{\pi}(s^′)−U^{\pi}(s) the TD error. The update formula will converge if {\alpha} decreases appropriately with the number of times the state has been visited, e.g. {\alpha}(n)=O(1/n).

The TD learning algorithm can be illustrated as following:

image

As you can see, TD can learn online after every step, instead, MC needs complete episode before learning. However, TD target depends only on one measured reward, while MC target depends on the sum of many rewards, so TD target has lower variance but is biased. Usually TD can converge faster than MC in practice.

To make it more accurate, we can also set more than one steps in TD target, which is called n-step TD: Let R_t+{\gamma}R_{t+2}...+{\gamma}^{n−1} R_{t+n−1}+{\gamma}^n \hat{U}(S_{t+n}) be the target for TD update. TD sets n equals to 1 while MC sets n to \infty. Usually, an intermediate value of n tends to work better.

Based on the n-step TD that using one value of n, a new method averages over different values of n is called TD(\lambda). It sets the following as the target for update:

G_t^{\lambda}=(1−{\lambda}) \sum_{n=1}^{\infty}{{\lambda}^(n−1) G_{t:t+n}}

It calculates the weighted sum of n-step returns, where the n-th step is weighted by {\lambda}^{n−1}, and this can be computed efficiently using a method called eligibility traces. Actually, it converges to TD as {\lambda} approaches 0, and to MC as {\lambda} approaches 1.

Now, we have seen the basic rule for prediction in TD method, now we turn the aim from utility function to Q-function, there are two different approaches to update the Q-function. They are called On-Policy methods and Off-Policy methods respectively and two examples for them are SARSA (State-Action-Reward-State-Action) algorithm and Q-learning algorithm.

  • SARSA Algorithm:

Q(s,a) \leftarrow Q(s,a)+{\alpha}(R(s)+{\gamma}Q(s^′,a^′)−Q(s,a))

where a′ is the action taken at s′ based on the {\epsilon}-greedy action selection with the current policy.

  • Q-learning Algorithm:

Q(s,a) \leftarrow Q(s,a)+{\alpha}(R(s)+{\gamma} max_{a^′}⁡{Q(s^′,a^′)}−Q(s,a))

where a′ represents all available actions at state s′.

The biggest difference between these two methods is the max operator.

SARSA is an on-policy method, which means that it only tries to improve the same policy that is used to generate the training data. For example, in the learning, for the current policy {\pi}_{now}, at the state s, the agent selects the action a (based on {\pi}_{now} and the action-selection policy, e.g. {\epsilon}-greedy, same thing below), then the environment transits to state s′ and returns reward r, finally, on the new state s′, the agent again selects an action a′, for now, we have get a tuple (s,a,r,s^′,a^′), using the formula above, we can do one-step update of the Q-function. In this method, selecting a′ and selecting a is based on the same policy, so we generate data from the policy and update the same policy.

Q-learning is an off-policy method, which means that the updated policy can be different from the policies that are used to generate data. This is because of the max operation, just as the formula shows, to update the Q-function for a (s,a) pair, we need the max_{a^′}⁡Q(s^′,a^′) part. Here, we choose the maximum value among all possible actions, the new action a′ in not limited to the current policy, we can use the data collected by other policies. You may have many differernt policies that generates and collects data in the environment, which are called behaivor policy, they can all be used to update a target policy. Here is also a pseudo-code of the Q-learning algorithm:

image
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,001评论 6 498
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,210评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 161,874评论 0 351
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,001评论 1 291
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,022评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,005评论 1 295
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,929评论 3 416
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,742评论 0 271
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,193评论 1 309
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,427评论 2 331
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,583评论 1 346
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,305评论 5 342
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,911评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,564评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,731评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,581评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,478评论 2 352

推荐阅读更多精彩内容