AI学习笔记之——强化学习(Reinforcement Learning, RL)

in ai •  6 years ago  (edited)

诚如在之前文章提到的,机器学习按照从那里学的角度可以分为:监督学习,无监督学习和强化学习三大类。之前的文章大多数都是介绍的前两类,而第三类强化学习(RL)确是最接近我们所想象的人工智能。

强化学习简介

强化学习(RL)就是在环境中与环境的互动不停地学习的,非常像人类学习走路,学习骑车,学习游泳等等。

想象一下自己刚刚开始学习骑自行车的样子,东扭西歪的,还摔过不少跤,最后才逐渐熟练掌握的。这种东扭西歪的动作恰恰就是对周围环境的不停反馈,如果动作不当摔倒了,就是个负反馈,下次再做这个动作就要小心了。用力蹬踏板的时候车子就向前走,这就是个正反馈,要继续保持这个动作前进。

与学习监督学习和非监督学习不一样,强化学习涉及到环境(Environment),反馈(Reward),智能体(Agent),Action(行动)并不是写几段Python代码,找个数据库就能开始学习的。还好,现在有有如OpenAI这样的开源平台,让初学者能够很快通过游戏的形式上手强化学习的项目。

在这里我简单介绍一个OpenAI 里面的小游戏,抛砖引玉让大家理解一下什么是强化学习。

OpenAI

OpenAI 的gym里面有一个叫(cart_pos的小游戏)[gym.openai.com],就是水平方向上有一个可以左右移动的平台,平台上有一个木棍,在重力的作用下会左右摇摆,你要做的事情就是左右移动平台使其保持平衡不要掉下来。

ezgif-5-0c58489cd8.gif

游戏的环境参数实际上就是如下图的四个变量:
[Horizontal Position,Horizontal Velocity, Angle of Pole, Angular Velocity]

首先以最简单的方式造一个机器人。

# Gotta import gym!
import gym

# Make the environment, replace this string with any
# from the docs. (Some environments have dependencies)
env = gym.make('CartPole-v0')

# Reset the environment to default beginning
env.reset()

# Using _ as temp placeholder variable
for _ in range(1000):
    # Render the env
    env.render()

    # 这里是关键
    env.step(env.action_space.sample()) # take a random action

这段代码不多解释,可以参考官方网文档, 其中最关键的是这一句

    env.step(env.action_space.sample())

我们实际上是让平台随机移动,可以想象结果是这样的。

ezgif-5-29b8b53110.gif

代码控制Agent

一般学习的对象我们叫做机器人或者Agent, 我们怎么简单地让这个Agent 智能一下呢?

最简单的方法就是当棍子向左偏的时候(Angle >0)就向右移动(Action = 1),向右偏(Angle<=0)就向左移动(Action = 0)。代码实现如下:

注意,四个环境变量是需要在obseervation中提取出来的,角度参数就是pole_ang

import gym
env = gym.make('CartPole-v0')
# print(env.action_space.)
# #> Discrete(2)
# print(env.observation_space)
# #> Box(4,)
observation = env.reset()

for t in range(1000):

    env.render()

    cart_pos , cart_vel , pole_ang , ang_vel = observation

    # Move Cart Right if Pole is Falling to the Right

    # Angle is measured off straight vertical line
    if pole_ang > 0:
        # Move Right
        action = 1
    else:
        # Move Left
        action = 0

    # Perform Action
    observation , reward, done, info = env.step(action)
    print(observation)

效果如下,虽然还是不稳定,但是比让平台随机移动还是有了点效果。
ezgif-5-20dcc80169.gif

总结

注意上面通过if语句实现对Agent的控制,是完全没有用到机器学习的,因为逻辑是写死的,机器根本就没有“学习”的过程。

这里用这个例子只是简单让读者理解强化学习是什么,就是向人一样,通过行动和环境的反馈不停地修正进行学习的。后续的文章会引入神经网路,让机器学起来。


相关文章
[AI学习笔记之——如何理解机器学习(https://steemit.com/cn/@hongtao/ai-machine-learning)
人工智能学习笔记之——人工智能基本概念和词汇
人工智能学习笔记二 —— 定义问题

Thanks for reading my posts and welcome to comment. If you like my post , please upvote , resteem and follow me @hongtao
感谢您的阅读,欢迎留言,如果您喜欢我的帖子,请帮忙点赞、推送及关注我 @hongtao

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Congratulations @hongtao! You have completed the following achievement on Steemit and have been rewarded with new badge(s) :

Award for the number of posts published

Click on the badge to view your Board of Honor.
If you no longer want to receive notifications, reply to this comment with the word STOP

To support your work, I also upvoted your post!

Do you like SteemitBoard's project? Then Vote for its witness and get one more award!

Hi ~ I'm a robot of red2018.I just upvoted your post!
Please come visit me here: https://steemit.com/@red2018
Thanks so much~!!

学习了

谢谢,互粉了