Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PARL
提交
c50746b1
P
PARL
项目概览
PaddlePaddle
/
PARL
通知
67
Star
3
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
18
列表
看板
标记
里程碑
合并请求
3
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PARL
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
18
Issue
18
列表
看板
标记
里程碑
合并请求
3
合并请求
3
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
c50746b1
编写于
6月 17, 2020
作者:
T
TomorrowIsAnOtherDay
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Update quick_start.md
上级
8ebddf33
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
64 addition
and
2 deletion
+64
-2
docs/zh_CN/tutorial/quick_start.md
docs/zh_CN/tutorial/quick_start.md
+64
-2
未找到文件。
docs/zh_CN/tutorial/quick_start.md
浏览文件 @
c50746b1
# **教程:使用PARL解决Cartpole问题**
本教程会使用
[
示例
](
~/parl
/examples/QuickStart
)
中的代码来解释任何通过PARL构建智能体解决经典的Cartpole问题。
本教程会使用
[
示例
](
https://github.com/PaddlePaddle/PARL/tree/develop
/examples/QuickStart
)
中的代码来解释任何通过PARL构建智能体解决经典的Cartpole问题。
本教程的目标:
-
熟悉PARL构建智能体过程中需要用到的子模块。
...
...
@@ -103,5 +103,67 @@ class CartpoleAgent(parl.Agent):
self
.
train_program
,
feed
=
feed
,
fetch_list
=
[
self
.
cost
])[
0
]
return
cost
```
一般情况下,用户必须实现以下几个函数:
-
构造函数:
把algorithm传进来,以及相关的环境参数(用户自定义的)。需要注意的是,这里必须得要初始化父类:super(CartpoleAgent, self).__init__(algorithm)。
-
build_program: 定义paddle里面的program。通常需要定义两个program:一个用于训练,一个用于预测。
-
predict: 根据输入返回预测动作(action)。
-
sample:根据输入返回动作(action),带探索的动作。
-
learn: 输入训练数据,更新算法。
## 开始训练
首先,我们来定一个智能体。逐步定义model|algorithm|agent,然后得到一个可以和环境进行交互的智能体。
```
python
model
=
CartpoleModel
(
act_dim
=
2
)
alg
=
parl
.
algorithms
.
PolicyGradient
(
model
,
lr
=
1e-3
)
agent
=
CartpoleAgent
(
alg
,
obs_dim
=
OBS_DIM
,
act_dim
=
2
)
```
然后我们用这个agent和环境进行交互,训练模型,1000个episode之后,agent就可以很好地解决Cartpole问题,拿到满分(200)。
```
python
def
run_episode
(
env
,
agent
,
train_or_test
=
'train'
):
obs_list
,
action_list
,
reward_list
=
[],
[],
[]
obs
=
env
.
reset
()
while
True
:
obs_list
.
append
(
obs
)
if
train_or_test
==
'train'
:
action
=
agent
.
sample
(
obs
)
else
:
action
=
agent
.
predict
(
obs
)
action_list
.
append
(
action
)
obs
,
reward
,
done
,
info
=
env
.
step
(
action
)
reward_list
.
append
(
reward
)
if
done
:
break
return
obs_list
,
action_list
,
reward_list
env
=
gym
.
make
(
"CartPole-v0"
)
for
i
in
range
(
1000
):
obs_list
,
action_list
,
reward_list
=
run_episode
(
env
,
agent
)
if
i
%
10
==
0
:
logger
.
info
(
"Episode {}, Reward Sum {}."
.
format
(
i
,
sum
(
reward_list
)))
batch_obs
=
np
.
array
(
obs_list
)
batch_action
=
np
.
array
(
action_list
)
batch_reward
=
calc_discount_norm_reward
(
reward_list
,
GAMMA
)
agent
.
learn
(
batch_obs
,
batch_action
,
batch_reward
)
if
(
i
+
1
)
%
100
==
0
:
_
,
_
,
reward_list
=
run_episode
(
env
,
agent
,
train_or_test
=
'test'
)
total_reward
=
np
.
sum
(
reward_list
)
logger
.
info
(
'Test reward: {}'
.
format
(
total_reward
))
```
## 总结
<img
src=
"../../../examples/QuickStart/performance.gif"
width=
"300"
/>
<img
src=
"../../images/quickstart.png"
width=
"300"
/>
在这个教程,我们展示了如何一步步地构建强化学习智能体,用于解决经典的Cartpole问题。完整的训练代码可以在这个
[
文件夹
](
https://github.com/PaddlePaddle/PARL/tree/develop/examples/QuickStart
)
中找到。
-
构造函数
```
shell
cd
examples/QuickStart/
python train.py
```
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录