Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
models
提交
84806a5e
M
models
项目概览
PaddlePaddle
/
models
大约 1 年 前同步成功
通知
222
Star
6828
Fork
2962
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
602
列表
看板
标记
里程碑
合并请求
255
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
M
models
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
602
Issue
602
列表
看板
标记
里程碑
合并请求
255
合并请求
255
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
84806a5e
编写于
12月 28, 2018
作者:
Z
zenghsh3
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
make DeepQNetwork models support paddlepaddle>=1.0.0
上级
3b4eb996
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
136 addition
and
132 deletion
+136
-132
fluid/DeepQNetwork/DQN_agent.py
fluid/DeepQNetwork/DQN_agent.py
+40
-39
fluid/DeepQNetwork/DoubleDQN_agent.py
fluid/DeepQNetwork/DoubleDQN_agent.py
+46
-45
fluid/DeepQNetwork/DuelingDQN_agent.py
fluid/DeepQNetwork/DuelingDQN_agent.py
+48
-46
fluid/DeepQNetwork/README.md
fluid/DeepQNetwork/README.md
+1
-1
fluid/DeepQNetwork/README_cn.md
fluid/DeepQNetwork/README_cn.md
+1
-1
未找到文件。
fluid/DeepQNetwork/DQN_agent.py
浏览文件 @
84806a5e
#-*- coding: utf-8 -*-
import
math
import
numpy
as
np
import
paddle.fluid
as
fluid
from
paddle.fluid.param_attr
import
ParamAttr
import
numpy
as
np
import
math
from
tqdm
import
tqdm
from
utils
import
fluid_flatten
...
...
@@ -39,34 +39,52 @@ class DQNModel(object):
name
=
'isOver'
,
shape
=
[],
dtype
=
'bool'
)
def
_build_net
(
self
):
s
tate
,
action
,
reward
,
next_s
,
isOver
=
self
.
_get_inputs
()
self
.
pred_value
=
self
.
get_DQN_prediction
(
state
)
self
.
predict_program
=
fluid
.
default_main_program
().
clone
()
s
elf
.
predict_program
=
fluid
.
Program
()
self
.
train_program
=
fluid
.
Program
(
)
self
.
_sync_program
=
fluid
.
Program
()
reward
=
fluid
.
layers
.
clip
(
reward
,
min
=-
1.0
,
max
=
1.0
)
with
fluid
.
program_guard
(
self
.
predict_program
):
state
,
action
,
reward
,
next_s
,
isOver
=
self
.
_get_inputs
()
self
.
pred_value
=
self
.
get_DQN_prediction
(
state
)
action_onehot
=
fluid
.
layers
.
one_hot
(
action
,
self
.
action_dim
)
action_onehot
=
fluid
.
layers
.
cast
(
action_onehot
,
dtype
=
'float32'
)
with
fluid
.
program_guard
(
self
.
train_program
):
state
,
action
,
reward
,
next_s
,
isOver
=
self
.
_get_inputs
()
pred_value
=
self
.
get_DQN_prediction
(
state
)
pred_action_value
=
fluid
.
layers
.
reduce_sum
(
fluid
.
layers
.
elementwise_mul
(
action_onehot
,
self
.
pred_value
),
dim
=
1
)
reward
=
fluid
.
layers
.
clip
(
reward
,
min
=-
1.0
,
max
=
1.0
)
targetQ_predict_value
=
self
.
get_DQN_prediction
(
next_s
,
target
=
True
)
best_v
=
fluid
.
layers
.
reduce_max
(
targetQ_predict_value
,
dim
=
1
)
best_v
.
stop_gradient
=
True
action_onehot
=
fluid
.
layers
.
one_hot
(
action
,
self
.
action_dim
)
action_onehot
=
fluid
.
layers
.
cast
(
action_onehot
,
dtype
=
'float32'
)
target
=
reward
+
(
1.0
-
fluid
.
layers
.
cast
(
isOver
,
dtype
=
'float32'
))
*
self
.
gamma
*
best_v
cost
=
fluid
.
layers
.
square_error_cost
(
pred_action_value
,
target
)
cost
=
fluid
.
layers
.
reduce_mean
(
cost
)
pred_action_value
=
fluid
.
layers
.
reduce_sum
(
fluid
.
layers
.
elementwise_mul
(
action_onehot
,
pred_value
),
dim
=
1
)
self
.
_sync_program
=
self
.
_build_sync_target_network
()
targetQ_predict_value
=
self
.
get_DQN_prediction
(
next_s
,
target
=
True
)
best_v
=
fluid
.
layers
.
reduce_max
(
targetQ_predict_value
,
dim
=
1
)
best_v
.
stop_gradient
=
True
optimizer
=
fluid
.
optimizer
.
Adam
(
1e-3
*
0.5
,
epsilon
=
1e-3
)
optimizer
.
minimize
(
cost
)
target
=
reward
+
(
1.0
-
fluid
.
layers
.
cast
(
isOver
,
dtype
=
'float32'
))
*
self
.
gamma
*
best_v
cost
=
fluid
.
layers
.
square_error_cost
(
pred_action_value
,
target
)
cost
=
fluid
.
layers
.
reduce_mean
(
cost
)
# define program
self
.
train_program
=
fluid
.
default_main_program
()
optimizer
=
fluid
.
optimizer
.
Adam
(
1e-3
*
0.5
,
epsilon
=
1e-3
)
optimizer
.
minimize
(
cost
)
vars
=
list
(
self
.
train_program
.
list_vars
())
policy_vars
=
list
(
filter
(
lambda
x
:
'GRAD'
not
in
x
.
name
and
'policy'
in
x
.
name
,
vars
))
target_vars
=
list
(
filter
(
lambda
x
:
'GRAD'
not
in
x
.
name
and
'target'
in
x
.
name
,
vars
))
policy_vars
.
sort
(
key
=
lambda
x
:
x
.
name
)
target_vars
.
sort
(
key
=
lambda
x
:
x
.
name
)
self
.
_sync_program
=
fluid
.
Program
()
with
fluid
.
program_guard
(
self
.
_sync_program
):
sync_ops
=
[]
for
i
,
var
in
enumerate
(
policy_vars
):
sync_op
=
fluid
.
layers
.
assign
(
policy_vars
[
i
],
target_vars
[
i
])
sync_ops
.
append
(
sync_op
)
# fluid exe
place
=
fluid
.
CUDAPlace
(
0
)
if
self
.
use_cuda
else
fluid
.
CPUPlace
()
...
...
@@ -133,23 +151,6 @@ class DQNModel(object):
bias_attr
=
ParamAttr
(
name
=
'{}_fc1_b'
.
format
(
variable_field
)))
return
out
def
_build_sync_target_network
(
self
):
vars
=
list
(
fluid
.
default_main_program
().
list_vars
())
policy_vars
=
list
(
filter
(
lambda
x
:
'GRAD'
not
in
x
.
name
and
'policy'
in
x
.
name
,
vars
))
target_vars
=
list
(
filter
(
lambda
x
:
'GRAD'
not
in
x
.
name
and
'target'
in
x
.
name
,
vars
))
policy_vars
.
sort
(
key
=
lambda
x
:
x
.
name
)
target_vars
.
sort
(
key
=
lambda
x
:
x
.
name
)
sync_program
=
fluid
.
default_main_program
().
clone
()
with
fluid
.
program_guard
(
sync_program
):
sync_ops
=
[]
for
i
,
var
in
enumerate
(
policy_vars
):
sync_op
=
fluid
.
layers
.
assign
(
policy_vars
[
i
],
target_vars
[
i
])
sync_ops
.
append
(
sync_op
)
sync_program
=
sync_program
.
prune
(
sync_ops
)
return
sync_program
def
act
(
self
,
state
,
train_or_test
):
sample
=
np
.
random
.
random
()
...
...
fluid/DeepQNetwork/DoubleDQN_agent.py
浏览文件 @
84806a5e
#-*- coding: utf-8 -*-
import
math
import
numpy
as
np
import
paddle.fluid
as
fluid
from
paddle.fluid.param_attr
import
ParamAttr
import
numpy
as
np
from
tqdm
import
tqdm
import
math
from
utils
import
fluid_argmax
,
fluid_flatten
from
utils
import
fluid_flatten
,
fluid_argmax
class
DoubleDQNModel
(
object
):
...
...
@@ -39,41 +39,59 @@ class DoubleDQNModel(object):
name
=
'isOver'
,
shape
=
[],
dtype
=
'bool'
)
def
_build_net
(
self
):
s
tate
,
action
,
reward
,
next_s
,
isOver
=
self
.
_get_inputs
()
self
.
pred_value
=
self
.
get_DQN_prediction
(
state
)
self
.
predict_program
=
fluid
.
default_main_program
().
clone
()
s
elf
.
predict_program
=
fluid
.
Program
()
self
.
train_program
=
fluid
.
Program
(
)
self
.
_sync_program
=
fluid
.
Program
()
reward
=
fluid
.
layers
.
clip
(
reward
,
min
=-
1.0
,
max
=
1.0
)
with
fluid
.
program_guard
(
self
.
predict_program
):
state
,
action
,
reward
,
next_s
,
isOver
=
self
.
_get_inputs
()
self
.
pred_value
=
self
.
get_DQN_prediction
(
state
)
action_onehot
=
fluid
.
layers
.
one_hot
(
action
,
self
.
action_dim
)
action_onehot
=
fluid
.
layers
.
cast
(
action_onehot
,
dtype
=
'float32'
)
with
fluid
.
program_guard
(
self
.
train_program
):
state
,
action
,
reward
,
next_s
,
isOver
=
self
.
_get_inputs
()
pred_value
=
self
.
get_DQN_prediction
(
state
)
pred_action_value
=
fluid
.
layers
.
reduce_sum
(
fluid
.
layers
.
elementwise_mul
(
action_onehot
,
self
.
pred_value
),
dim
=
1
)
reward
=
fluid
.
layers
.
clip
(
reward
,
min
=-
1.0
,
max
=
1.0
)
targetQ_predict_value
=
self
.
get_DQN_prediction
(
next_s
,
target
=
True
)
action_onehot
=
fluid
.
layers
.
one_hot
(
action
,
self
.
action_dim
)
action_onehot
=
fluid
.
layers
.
cast
(
action_onehot
,
dtype
=
'float32'
)
next_s_predcit_value
=
self
.
get_DQN_prediction
(
next_s
)
greedy_action
=
fluid_argmax
(
next_s_predcit_value
)
pred_action_value
=
fluid
.
layers
.
reduce_sum
(
fluid
.
layers
.
elementwise_mul
(
action_onehot
,
pred_value
),
dim
=
1
)
predict_onehot
=
fluid
.
layers
.
one_hot
(
greedy_action
,
self
.
action_dim
)
best_v
=
fluid
.
layers
.
reduce_sum
(
fluid
.
layers
.
elementwise_mul
(
predict_onehot
,
targetQ_predict_value
),
dim
=
1
)
best_v
.
stop_gradient
=
True
targetQ_predict_value
=
self
.
get_DQN_prediction
(
next_s
,
target
=
True
)
target
=
reward
+
(
1.0
-
fluid
.
layers
.
cast
(
isOver
,
dtype
=
'float32'
))
*
self
.
gamma
*
best_v
cost
=
fluid
.
layers
.
square_error_cost
(
pred_action_value
,
target
)
cost
=
fluid
.
layers
.
reduce_mean
(
cost
)
next_s_predcit_value
=
self
.
get_DQN_prediction
(
next_s
)
greedy_action
=
fluid_argmax
(
next_s_predcit_value
)
self
.
_sync_program
=
self
.
_build_sync_target_network
()
predict_onehot
=
fluid
.
layers
.
one_hot
(
greedy_action
,
self
.
action_dim
)
best_v
=
fluid
.
layers
.
reduce_sum
(
fluid
.
layers
.
elementwise_mul
(
predict_onehot
,
targetQ_predict_value
),
dim
=
1
)
best_v
.
stop_gradient
=
True
optimizer
=
fluid
.
optimizer
.
Adam
(
1e-3
*
0.5
,
epsilon
=
1e-3
)
optimizer
.
minimize
(
cost
)
target
=
reward
+
(
1.0
-
fluid
.
layers
.
cast
(
isOver
,
dtype
=
'float32'
))
*
self
.
gamma
*
best_v
cost
=
fluid
.
layers
.
square_error_cost
(
pred_action_value
,
target
)
cost
=
fluid
.
layers
.
reduce_mean
(
cost
)
# define program
self
.
train_program
=
fluid
.
default_main_program
()
optimizer
=
fluid
.
optimizer
.
Adam
(
1e-3
*
0.5
,
epsilon
=
1e-3
)
optimizer
.
minimize
(
cost
)
vars
=
list
(
self
.
train_program
.
list_vars
())
policy_vars
=
list
(
filter
(
lambda
x
:
'GRAD'
not
in
x
.
name
and
'policy'
in
x
.
name
,
vars
))
target_vars
=
list
(
filter
(
lambda
x
:
'GRAD'
not
in
x
.
name
and
'target'
in
x
.
name
,
vars
))
policy_vars
.
sort
(
key
=
lambda
x
:
x
.
name
)
target_vars
.
sort
(
key
=
lambda
x
:
x
.
name
)
self
.
_sync_program
=
fluid
.
Program
()
with
fluid
.
program_guard
(
self
.
_sync_program
):
sync_ops
=
[]
for
i
,
var
in
enumerate
(
policy_vars
):
sync_op
=
fluid
.
layers
.
assign
(
policy_vars
[
i
],
target_vars
[
i
])
sync_ops
.
append
(
sync_op
)
# fluid exe
place
=
fluid
.
CUDAPlace
(
0
)
if
self
.
use_cuda
else
fluid
.
CPUPlace
()
...
...
@@ -140,23 +158,6 @@ class DoubleDQNModel(object):
bias_attr
=
ParamAttr
(
name
=
'{}_fc1_b'
.
format
(
variable_field
)))
return
out
def
_build_sync_target_network
(
self
):
vars
=
list
(
fluid
.
default_main_program
().
list_vars
())
policy_vars
=
list
(
filter
(
lambda
x
:
'GRAD'
not
in
x
.
name
and
'policy'
in
x
.
name
,
vars
))
target_vars
=
list
(
filter
(
lambda
x
:
'GRAD'
not
in
x
.
name
and
'target'
in
x
.
name
,
vars
))
policy_vars
.
sort
(
key
=
lambda
x
:
x
.
name
)
target_vars
.
sort
(
key
=
lambda
x
:
x
.
name
)
sync_program
=
fluid
.
default_main_program
().
clone
()
with
fluid
.
program_guard
(
sync_program
):
sync_ops
=
[]
for
i
,
var
in
enumerate
(
policy_vars
):
sync_op
=
fluid
.
layers
.
assign
(
policy_vars
[
i
],
target_vars
[
i
])
sync_ops
.
append
(
sync_op
)
sync_program
=
sync_program
.
prune
(
sync_ops
)
return
sync_program
def
act
(
self
,
state
,
train_or_test
):
sample
=
np
.
random
.
random
()
...
...
fluid/DeepQNetwork/DuelingDQN_agent.py
浏览文件 @
84806a5e
#-*- coding: utf-8 -*-
import
math
import
numpy
as
np
import
paddle.fluid
as
fluid
from
paddle.fluid.param_attr
import
ParamAttr
import
numpy
as
np
from
tqdm
import
tqdm
import
math
from
utils
import
fluid_flatten
...
...
@@ -39,34 +39,52 @@ class DuelingDQNModel(object):
name
=
'isOver'
,
shape
=
[],
dtype
=
'bool'
)
def
_build_net
(
self
):
s
tate
,
action
,
reward
,
next_s
,
isOver
=
self
.
_get_inputs
()
self
.
pred_value
=
self
.
get_DQN_prediction
(
state
)
self
.
predict_program
=
fluid
.
default_main_program
().
clone
()
s
elf
.
predict_program
=
fluid
.
Program
()
self
.
train_program
=
fluid
.
Program
(
)
self
.
_sync_program
=
fluid
.
Program
()
reward
=
fluid
.
layers
.
clip
(
reward
,
min
=-
1.0
,
max
=
1.0
)
with
fluid
.
program_guard
(
self
.
predict_program
):
state
,
action
,
reward
,
next_s
,
isOver
=
self
.
_get_inputs
()
self
.
pred_value
=
self
.
get_DQN_prediction
(
state
)
action_onehot
=
fluid
.
layers
.
one_hot
(
action
,
self
.
action_dim
)
action_onehot
=
fluid
.
layers
.
cast
(
action_onehot
,
dtype
=
'float32'
)
with
fluid
.
program_guard
(
self
.
train_program
):
state
,
action
,
reward
,
next_s
,
isOver
=
self
.
_get_inputs
()
pred_value
=
self
.
get_DQN_prediction
(
state
)
pred_action_value
=
fluid
.
layers
.
reduce_sum
(
fluid
.
layers
.
elementwise_mul
(
action_onehot
,
self
.
pred_value
),
dim
=
1
)
reward
=
fluid
.
layers
.
clip
(
reward
,
min
=-
1.0
,
max
=
1.0
)
targetQ_predict_value
=
self
.
get_DQN_prediction
(
next_s
,
target
=
True
)
best_v
=
fluid
.
layers
.
reduce_max
(
targetQ_predict_value
,
dim
=
1
)
best_v
.
stop_gradient
=
True
action_onehot
=
fluid
.
layers
.
one_hot
(
action
,
self
.
action_dim
)
action_onehot
=
fluid
.
layers
.
cast
(
action_onehot
,
dtype
=
'float32'
)
target
=
reward
+
(
1.0
-
fluid
.
layers
.
cast
(
isOver
,
dtype
=
'float32'
))
*
self
.
gamma
*
best_v
cost
=
fluid
.
layers
.
square_error_cost
(
pred_action_value
,
target
)
cost
=
fluid
.
layers
.
reduce_mean
(
cost
)
pred_action_value
=
fluid
.
layers
.
reduce_sum
(
fluid
.
layers
.
elementwise_mul
(
action_onehot
,
pred_value
),
dim
=
1
)
self
.
_sync_program
=
self
.
_build_sync_target_network
()
targetQ_predict_value
=
self
.
get_DQN_prediction
(
next_s
,
target
=
True
)
best_v
=
fluid
.
layers
.
reduce_max
(
targetQ_predict_value
,
dim
=
1
)
best_v
.
stop_gradient
=
True
optimizer
=
fluid
.
optimizer
.
Adam
(
1e-3
*
0.5
,
epsilon
=
1e-3
)
optimizer
.
minimize
(
cost
)
target
=
reward
+
(
1.0
-
fluid
.
layers
.
cast
(
isOver
,
dtype
=
'float32'
))
*
self
.
gamma
*
best_v
cost
=
fluid
.
layers
.
square_error_cost
(
pred_action_value
,
target
)
cost
=
fluid
.
layers
.
reduce_mean
(
cost
)
# define program
self
.
train_program
=
fluid
.
default_main_program
()
optimizer
=
fluid
.
optimizer
.
Adam
(
1e-3
*
0.5
,
epsilon
=
1e-3
)
optimizer
.
minimize
(
cost
)
vars
=
list
(
self
.
train_program
.
list_vars
())
policy_vars
=
list
(
filter
(
lambda
x
:
'GRAD'
not
in
x
.
name
and
'policy'
in
x
.
name
,
vars
))
target_vars
=
list
(
filter
(
lambda
x
:
'GRAD'
not
in
x
.
name
and
'target'
in
x
.
name
,
vars
))
policy_vars
.
sort
(
key
=
lambda
x
:
x
.
name
)
target_vars
.
sort
(
key
=
lambda
x
:
x
.
name
)
self
.
_sync_program
=
fluid
.
Program
()
with
fluid
.
program_guard
(
self
.
_sync_program
):
sync_ops
=
[]
for
i
,
var
in
enumerate
(
policy_vars
):
sync_op
=
fluid
.
layers
.
assign
(
policy_vars
[
i
],
target_vars
[
i
])
sync_ops
.
append
(
sync_op
)
# fluid exe
place
=
fluid
.
CUDAPlace
(
0
)
if
self
.
use_cuda
else
fluid
.
CPUPlace
()
...
...
@@ -143,24 +161,6 @@ class DuelingDQNModel(object):
advantage
,
dim
=
1
,
keep_dim
=
True
))
return
Q
def
_build_sync_target_network
(
self
):
vars
=
list
(
fluid
.
default_main_program
().
list_vars
())
policy_vars
=
list
(
filter
(
lambda
x
:
'GRAD'
not
in
x
.
name
and
'policy'
in
x
.
name
,
vars
))
target_vars
=
list
(
filter
(
lambda
x
:
'GRAD'
not
in
x
.
name
and
'target'
in
x
.
name
,
vars
))
policy_vars
.
sort
(
key
=
lambda
x
:
x
.
name
)
target_vars
.
sort
(
key
=
lambda
x
:
x
.
name
)
sync_program
=
fluid
.
default_main_program
().
clone
()
with
fluid
.
program_guard
(
sync_program
):
sync_ops
=
[]
for
i
,
var
in
enumerate
(
policy_vars
):
sync_op
=
fluid
.
layers
.
assign
(
policy_vars
[
i
],
target_vars
[
i
])
sync_ops
.
append
(
sync_op
)
# The prune API is deprecated, please don't use it any more.
sync_program
=
sync_program
.
_prune
(
sync_ops
)
return
sync_program
def
act
(
self
,
state
,
train_or_test
):
sample
=
np
.
random
.
random
()
...
...
@@ -186,12 +186,14 @@ class DuelingDQNModel(object):
self
.
global_step
+=
1
action
=
np
.
expand_dims
(
action
,
-
1
)
self
.
exe
.
run
(
self
.
train_program
,
\
feed
=
{
'state'
:
state
.
astype
(
'float32'
),
\
'action'
:
action
.
astype
(
'int32'
),
\
'reward'
:
reward
,
\
'next_s'
:
next_state
.
astype
(
'float32'
),
\
'isOver'
:
isOver
})
self
.
exe
.
run
(
self
.
train_program
,
feed
=
{
'state'
:
state
.
astype
(
'float32'
),
'action'
:
action
.
astype
(
'int32'
),
'reward'
:
reward
,
'next_s'
:
next_state
.
astype
(
'float32'
),
'isOver'
:
isOver
})
def
sync_target_network
(
self
):
self
.
exe
.
run
(
self
.
_sync_program
)
fluid/DeepQNetwork/README.md
浏览文件 @
84806a5e
...
...
@@ -29,7 +29,7 @@ The average game rewards that can be obtained for the three models as the number
+
gym
+
tqdm
+
opencv-python
+
paddlepaddle-gpu>=
0.12
.0
+
paddlepaddle-gpu>=
1.0
.0
+
ale_python_interface
### Install Dependencies:
...
...
fluid/DeepQNetwork/README_cn.md
浏览文件 @
84806a5e
...
...
@@ -28,7 +28,7 @@
+
gym
+
tqdm
+
opencv-python
+
paddlepaddle-gpu>=
0.12
.0
+
paddlepaddle-gpu>=
1.0
.0
+
ale_python_interface
### 下载依赖:
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录