Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
31a1cd8c
P
Paddle
项目概览
PaddlePaddle
/
Paddle
1 年多 前同步成功
通知
2302
Star
20931
Fork
5422
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
31a1cd8c
编写于
1月 21, 2019
作者:
M
minqiyang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Align the first batch of gpu resnet
上级
dbd4d058
变更
9
显示空白变更内容
内联
并排
Showing
9 changed file
with
142 addition
and
44 deletion
+142
-44
paddle/fluid/imperative/layer.cc
paddle/fluid/imperative/layer.cc
+33
-0
paddle/fluid/imperative/layer.h
paddle/fluid/imperative/layer.h
+2
-0
paddle/fluid/imperative/tracer.cc
paddle/fluid/imperative/tracer.cc
+5
-1
paddle/fluid/pybind/pybind.cc
paddle/fluid/pybind/pybind.cc
+3
-7
python/paddle/fluid/framework.py
python/paddle/fluid/framework.py
+2
-2
python/paddle/fluid/imperative/nn.py
python/paddle/fluid/imperative/nn.py
+18
-9
python/paddle/fluid/layer_helper.py
python/paddle/fluid/layer_helper.py
+7
-2
python/paddle/fluid/tests/unittests/test_imperative_base.py
python/paddle/fluid/tests/unittests/test_imperative_base.py
+3
-2
python/paddle/fluid/tests/unittests/test_imperative_resnet.py
...on/paddle/fluid/tests/unittests/test_imperative_resnet.py
+69
-21
未找到文件。
paddle/fluid/imperative/layer.cc
浏览文件 @
31a1cd8c
...
@@ -167,12 +167,42 @@ class Autograd {
...
@@ -167,12 +167,42 @@ class Autograd {
}
}
};
};
framework
::
LoDTensor
*
VarBase
::
CopiedTensor
()
const
{
PADDLE_ENFORCE
(
var_
->
IsInitialized
(),
"Variable must be initialized when getting numpy tensor"
);
platform
::
Place
place
=
var_
->
Get
<
framework
::
LoDTensor
>
().
place
();
framework
::
LoDTensor
*
result
=
new
framework
::
LoDTensor
();
result
->
Resize
(
var_
->
Get
<
framework
::
LoDTensor
>
().
dims
());
result
->
set_lod
(
var_
->
Get
<
framework
::
LoDTensor
>
().
lod
());
if
(
platform
::
is_gpu_place
(
place
))
{
VLOG
(
3
)
<<
"fetch tensor "
<<
var_desc_
->
Name
()
<<
" from gpu"
;
framework
::
TensorCopy
(
var_
->
Get
<
framework
::
LoDTensor
>
(),
platform
::
CPUPlace
(),
result
);
platform
::
DeviceContext
*
dev_ctx
=
platform
::
DeviceContextPool
::
Instance
().
Get
(
place
);
dev_ctx
->
Wait
();
}
else
{
TensorCopy
(
var_
->
Get
<
framework
::
LoDTensor
>
(),
platform
::
CPUPlace
(),
result
);
}
return
result
;
}
framework
::
LoDTensor
&
VarBase
::
GradValue
()
{
framework
::
LoDTensor
&
VarBase
::
GradValue
()
{
VLOG
(
3
)
<<
"get var grad "
<<
var_desc_
->
Name
();
VLOG
(
3
)
<<
"get var grad "
<<
var_desc_
->
Name
();
return
*
(
grads_
->
var_
->
GetMutable
<
framework
::
LoDTensor
>
());
return
*
(
grads_
->
var_
->
GetMutable
<
framework
::
LoDTensor
>
());
}
}
std
::
map
<
std
::
string
,
std
::
vector
<
VarBase
*>>
OpBase
::
ApplyGrad
()
{
std
::
map
<
std
::
string
,
std
::
vector
<
VarBase
*>>
OpBase
::
ApplyGrad
()
{
VLOG
(
3
)
<<
"ApplyGrad to Op: "
<<
op_desc_
->
Type
();
for
(
auto
it
:
input_vars_
)
{
for
(
VarBase
*
var
:
it
.
second
)
{
VLOG
(
3
)
<<
"Op Input: "
<<
it
.
first
<<
" : "
<<
var
->
var_desc_
->
Name
();
}
}
if
(
!
grad_op_desc_
&&
backward_id_
<=
0
)
{
if
(
!
grad_op_desc_
&&
backward_id_
<=
0
)
{
LOG
(
WARNING
)
<<
"op with no grad: "
<<
op_desc_
->
Type
();
LOG
(
WARNING
)
<<
"op with no grad: "
<<
op_desc_
->
Type
();
return
{};
return
{};
...
@@ -222,6 +252,9 @@ std::map<std::string, std::vector<VarBase*>> OpBase::ApplyGrad() {
...
@@ -222,6 +252,9 @@ std::map<std::string, std::vector<VarBase*>> OpBase::ApplyGrad() {
for
(
size_t
i
=
0
;
i
<
outputs
.
size
();
++
i
)
{
for
(
size_t
i
=
0
;
i
<
outputs
.
size
();
++
i
)
{
framework
::
Variable
*
grad
=
outputs
[
i
];
framework
::
Variable
*
grad
=
outputs
[
i
];
framework
::
Variable
*
orig_grad
=
origin_outputs
[
i
];
framework
::
Variable
*
orig_grad
=
origin_outputs
[
i
];
LOG
(
ERROR
)
<<
"Add grad of "
<<
it
.
first
<<
" "
<<
i
<<
" "
<<
orig_grad
->
GetMutable
<
framework
::
LoDTensor
>
()
->
mutable_data
(
expected_place_
);
AddGradTo
(
grad
,
orig_grad
,
expected_place_
);
AddGradTo
(
grad
,
orig_grad
,
expected_place_
);
delete
grad
;
delete
grad
;
}
}
...
...
paddle/fluid/imperative/layer.h
浏览文件 @
31a1cd8c
...
@@ -136,6 +136,8 @@ class VarBase {
...
@@ -136,6 +136,8 @@ class VarBase {
framework
::
LoDTensor
&
GradValue
();
framework
::
LoDTensor
&
GradValue
();
framework
::
LoDTensor
*
CopiedTensor
()
const
;
inline
std
::
string
GradName
()
const
{
inline
std
::
string
GradName
()
const
{
PADDLE_ENFORCE
(
PADDLE_ENFORCE
(
var_desc_
,
var_desc_
,
...
...
paddle/fluid/imperative/tracer.cc
浏览文件 @
31a1cd8c
...
@@ -43,7 +43,7 @@ void InitVar(framework::Variable* var, framework::Variable* grad_var,
...
@@ -43,7 +43,7 @@ void InitVar(framework::Variable* var, framework::Variable* grad_var,
grad_var
->
GetMutable
<
framework
::
LoDTensor
>
()
->
mutable_data
<
float
>
(
grad_var
->
GetMutable
<
framework
::
LoDTensor
>
()
->
mutable_data
<
float
>
(
var_t
.
dims
(),
dev_ctx
->
GetPlace
());
var_t
.
dims
(),
dev_ctx
->
GetPlace
());
operators
::
math
::
set_constant
(
operators
::
math
::
set_constant
(
*
dev_ctx
,
grad_var
->
GetMutable
<
framework
::
LoDTensor
>
(),
.0
f
);
*
dev_ctx
,
grad_var
->
GetMutable
<
framework
::
LoDTensor
>
(),
0.0
);
}
}
platform
::
Place
GetExpectedPlace
(
platform
::
Place
place
,
VarBasePtrMap
inputs
)
{
platform
::
Place
GetExpectedPlace
(
platform
::
Place
place
,
VarBasePtrMap
inputs
)
{
...
@@ -162,6 +162,7 @@ void Tracer::Trace(OpBase* op, const VarBasePtrMap& inputs,
...
@@ -162,6 +162,7 @@ void Tracer::Trace(OpBase* op, const VarBasePtrMap& inputs,
}
else
{
}
else
{
VarBase
*
var
=
vars
[
var_it
->
second
];
VarBase
*
var
=
vars
[
var_it
->
second
];
if
(
!
var
->
grads_
->
var_
->
IsInitialized
())
{
if
(
!
var
->
grads_
->
var_
->
IsInitialized
())
{
LOG
(
ERROR
)
<<
"Init grad input "
<<
it
.
first
<<
" "
<<
grad_invar
;
InitVar
(
var
->
var_
,
var
->
grads_
->
var_
,
InitVar
(
var
->
var_
,
var
->
grads_
->
var_
,
prepared_op
.
GetDeviceContext
());
prepared_op
.
GetDeviceContext
());
}
}
...
@@ -183,6 +184,9 @@ void Tracer::Trace(OpBase* op, const VarBasePtrMap& inputs,
...
@@ -183,6 +184,9 @@ void Tracer::Trace(OpBase* op, const VarBasePtrMap& inputs,
VarBase
*
var
=
vars
[
var_it
->
second
];
VarBase
*
var
=
vars
[
var_it
->
second
];
if
(
!
var
->
grads_
->
var_
->
IsInitialized
())
{
if
(
!
var
->
grads_
->
var_
->
IsInitialized
())
{
InitVar
(
var
->
var_
,
var
->
grads_
->
var_
,
prepared_op
.
GetDeviceContext
());
InitVar
(
var
->
var_
,
var
->
grads_
->
var_
,
prepared_op
.
GetDeviceContext
());
LOG
(
ERROR
)
<<
"Init grad output "
<<
it
.
first
<<
" "
<<
grad_outvar
<<
var
->
grads_
->
var_
->
GetMutable
<
framework
::
LoDTensor
>
()
->
mutable_data
(
platform
::
CPUPlace
());
}
}
grad_out_vars
.
push_back
(
var
->
grads_
->
var_
);
grad_out_vars
.
push_back
(
var
->
grads_
->
var_
);
}
}
...
...
paddle/fluid/pybind/pybind.cc
浏览文件 @
31a1cd8c
...
@@ -136,15 +136,11 @@ PYBIND11_MODULE(core, m) {
...
@@ -136,15 +136,11 @@ PYBIND11_MODULE(core, m) {
.
def
(
"_grad_ivar"
,
.
def
(
"_grad_ivar"
,
[](
const
imperative
::
VarBase
&
self
)
{
return
self
.
grads_
;
},
[](
const
imperative
::
VarBase
&
self
)
{
return
self
.
grads_
;
},
py
::
return_value_policy
::
reference
)
py
::
return_value_policy
::
reference
)
.
def
(
"_cpu_tensor"
,
[](
const
imperative
::
VarBase
&
self
)
{
return
self
.
CopiedTensor
();
},
py
::
return_value_policy
::
take_ownership
)
.
def
(
"value"
,
[](
const
imperative
::
VarBase
&
self
)
{
return
self
.
var_
;
},
.
def
(
"value"
,
[](
const
imperative
::
VarBase
&
self
)
{
return
self
.
var_
;
},
py
::
return_value_policy
::
reference
)
py
::
return_value_policy
::
reference
)
.
def
(
"wait_device"
,
[](
const
imperative
::
VarBase
&
self
)
{
platform
::
DeviceContext
*
dev_ctx
=
platform
::
DeviceContextPool
::
Instance
().
Get
(
self
.
var_
->
Get
<
framework
::
LoDTensor
>
().
place
());
dev_ctx
->
Wait
();
})
.
def_property
(
.
def_property
(
"desc"
,
"desc"
,
[](
const
imperative
::
VarBase
&
self
)
{
return
self
.
var_desc_
;
},
[](
const
imperative
::
VarBase
&
self
)
{
return
self
.
var_desc_
;
},
...
...
python/paddle/fluid/framework.py
浏览文件 @
31a1cd8c
...
@@ -384,8 +384,8 @@ class Variable(object):
...
@@ -384,8 +384,8 @@ class Variable(object):
self
.
_ivar
.
stop_gradient
=
stop_gradient
self
.
_ivar
.
stop_gradient
=
stop_gradient
def
_numpy
(
self
):
def
_numpy
(
self
):
self
.
_ivar
.
wait_device
()
tensor
=
self
.
_ivar
.
_cpu_tensor
()
tensor
=
self
.
_ivar
.
value
().
get_tensor
(
)
print
(
'shapex'
,
self
.
name
,
tensor
.
shape
()
)
return
np
.
array
(
tensor
)
return
np
.
array
(
tensor
)
def
_backward
(
self
):
def
_backward
(
self
):
...
...
python/paddle/fluid/imperative/nn.py
浏览文件 @
31a1cd8c
...
@@ -55,7 +55,8 @@ class Conv2D(layers.Layer):
...
@@ -55,7 +55,8 @@ class Conv2D(layers.Layer):
param_attr
=
param_attr
,
param_attr
=
param_attr
,
bias_attr
=
bias_attr
,
bias_attr
=
bias_attr
,
dtype
=
dtype
,
dtype
=
dtype
,
name
=
name
)
name
=
name
,
act
=
act
)
self
.
_groups
=
groups
self
.
_groups
=
groups
self
.
_stride
=
utils
.
convert_to_list
(
stride
,
2
,
'stride'
)
self
.
_stride
=
utils
.
convert_to_list
(
stride
,
2
,
'stride'
)
...
@@ -141,6 +142,7 @@ class Conv2D(layers.Layer):
...
@@ -141,6 +142,7 @@ class Conv2D(layers.Layer):
outputs
=
{
'Out'
:
[
pre_act
]},
outputs
=
{
'Out'
:
[
pre_act
]},
attrs
=
{
'axis'
:
1
})
attrs
=
{
'axis'
:
1
})
# Currently, we don't support inplace in imperative mode
return
self
.
_helper
.
append_activation
(
pre_act
)
return
self
.
_helper
.
append_activation
(
pre_act
)
...
@@ -239,7 +241,6 @@ class FC(layers.Layer):
...
@@ -239,7 +241,6 @@ class FC(layers.Layer):
shape
=
param_shape
,
shape
=
param_shape
,
dtype
=
self
.
_dtype
,
dtype
=
self
.
_dtype
,
is_bias
=
False
)
is_bias
=
False
)
print
(
"create param: "
,
self
.
_w
.
name
,
self
.
_w
.
stop_gradient
)
if
self
.
_helper
.
bias_attr
:
if
self
.
_helper
.
bias_attr
:
size
=
list
([
self
.
_size
])
size
=
list
([
self
.
_size
])
...
@@ -281,6 +282,7 @@ class FC(layers.Layer):
...
@@ -281,6 +282,7 @@ class FC(layers.Layer):
attrs
=
{
'axis'
:
self
.
_num_flatten_dims
})
attrs
=
{
'axis'
:
self
.
_num_flatten_dims
})
else
:
else
:
pre_activation
=
pre_bias
pre_activation
=
pre_bias
# Currently, we don't support inplace in imperative mode
return
self
.
_helper
.
append_activation
(
pre_activation
)
return
self
.
_helper
.
append_activation
(
pre_activation
)
...
@@ -308,7 +310,11 @@ class BatchNorm(layers.Layer):
...
@@ -308,7 +310,11 @@ class BatchNorm(layers.Layer):
from
..layer_helper
import
LayerHelper
from
..layer_helper
import
LayerHelper
self
.
_helper
=
LayerHelper
(
self
.
_helper
=
LayerHelper
(
'batch_norm'
,
param_attr
=
param_attr
,
bias_attr
=
bias_attr
,
name
=
name
)
'batch_norm'
,
param_attr
=
param_attr
,
bias_attr
=
bias_attr
,
name
=
name
,
act
=
act
)
if
dtype
==
core
.
VarDesc
.
VarType
.
FP16
:
if
dtype
==
core
.
VarDesc
.
VarType
.
FP16
:
self
.
_dtype
=
core
.
VarDesc
.
VarType
.
FP32
self
.
_dtype
=
core
.
VarDesc
.
VarType
.
FP32
...
@@ -324,18 +330,20 @@ class BatchNorm(layers.Layer):
...
@@ -324,18 +330,20 @@ class BatchNorm(layers.Layer):
dtype
=
self
.
_dtype
,
dtype
=
self
.
_dtype
,
default_initializer
=
Constant
(
1.0
))
default_initializer
=
Constant
(
1.0
))
# setting stop_gradient=True to reduce computation
# TODO(minqiyang): change stop_gradient sign to trainable to align with static graph
if
use_global_stats
and
self
.
_helper
.
param_attr
.
learning_rate
==
0.
:
# # setting stop_gradient=True to reduce computation
self
.
_scale
.
stop_gradient
=
True
# if use_global_stats and self._helper.param_attr.learning_rate == 0.:
# self._scale.stop_gradient = True
self
.
_bias
=
self
.
_helper
.
create_parameter
(
self
.
_bias
=
self
.
_helper
.
create_parameter
(
attr
=
self
.
_helper
.
bias_attr
,
attr
=
self
.
_helper
.
bias_attr
,
shape
=
param_shape
,
shape
=
param_shape
,
dtype
=
self
.
_dtype
,
dtype
=
self
.
_dtype
,
is_bias
=
True
)
is_bias
=
True
)
# setting stop_gradient=True to reduce computation
# TODO(minqiyang): change stop_gradient sign to trainable to align with static graph
if
use_global_stats
and
self
.
_helper
.
bias_attr
.
learning_rate
==
0.
:
# # setting stop_gradient=True to reduce computation
self
.
_bias
.
stop_gradient
=
True
# if use_global_stats and self._helper.bias_attr.learning_rate == 0.:
# self._bias.stop_gradient = True
self
.
_mean
=
self
.
_helper
.
create_parameter
(
self
.
_mean
=
self
.
_helper
.
create_parameter
(
attr
=
ParamAttr
(
attr
=
ParamAttr
(
...
@@ -406,4 +414,5 @@ class BatchNorm(layers.Layer):
...
@@ -406,4 +414,5 @@ class BatchNorm(layers.Layer):
"use_global_stats"
:
self
.
_use_global_stats
"use_global_stats"
:
self
.
_use_global_stats
})
})
# Currently, we don't support inplace in imperative mode
return
self
.
_helper
.
append_activation
(
batch_norm_out
)
return
self
.
_helper
.
append_activation
(
batch_norm_out
)
python/paddle/fluid/layer_helper.py
浏览文件 @
31a1cd8c
...
@@ -435,7 +435,12 @@ class LayerHelper(object):
...
@@ -435,7 +435,12 @@ class LayerHelper(object):
act_type
=
act
.
pop
(
'type'
)
act_type
=
act
.
pop
(
'type'
)
tmp
=
input_var
tmp
=
input_var
# NOTE(dzhwinter): some activation support inplace compution.
# NOTE(dzhwinter): some activation support inplace compution.
if
not
core
.
IsInplace
(
act_type
):
# NOTE(minqiyang): currently, we don't support inplace in imperative mode
# if core.IsInplace(act_type) and no_inplace:
# print("inplace", act_type)
# tmp = input_var
# else:
print
(
"not inplace"
,
act_type
)
tmp
=
self
.
create_variable_for_type_inference
(
dtype
=
input_var
.
dtype
)
tmp
=
self
.
create_variable_for_type_inference
(
dtype
=
input_var
.
dtype
)
self
.
append_op
(
self
.
append_op
(
type
=
act_type
,
type
=
act_type
,
...
...
python/paddle/fluid/tests/unittests/test_imperative_base.py
浏览文件 @
31a1cd8c
...
@@ -24,7 +24,8 @@ from paddle.fluid import core
...
@@ -24,7 +24,8 @@ from paddle.fluid import core
def
new_program_scope
():
def
new_program_scope
():
prog
=
fluid
.
Program
()
prog
=
fluid
.
Program
()
startup_prog
=
fluid
.
Program
()
startup_prog
=
fluid
.
Program
()
scope
=
fluid
.
core
.
Scope
()
scope
=
core
.
Scope
()
with
fluid
.
scope_guard
(
scope
):
with
fluid
.
scope_guard
(
scope
):
with
fluid
.
program_guard
(
prog
,
startup_prog
):
with
fluid
.
program_guard
(
prog
,
startup_prog
):
with
fluid
.
unique_name
.
guard
():
yield
yield
python/paddle/fluid/tests/unittests/test_imperative_resnet.py
浏览文件 @
31a1cd8c
...
@@ -25,17 +25,18 @@ from paddle.fluid.imperative.nn import Conv2D, Pool2D, BatchNorm, FC
...
@@ -25,17 +25,18 @@ from paddle.fluid.imperative.nn import Conv2D, Pool2D, BatchNorm, FC
from
paddle.fluid.imperative.base
import
to_variable
from
paddle.fluid.imperative.base
import
to_variable
from
test_imperative_base
import
new_program_scope
from
test_imperative_base
import
new_program_scope
batch_size
=
8
train_parameters
=
{
train_parameters
=
{
"input_size"
:
[
3
,
224
,
224
],
"input_size"
:
[
3
,
224
,
224
],
"input_mean"
:
[
0.485
,
0.456
,
0.406
],
"input_mean"
:
[
0.485
,
0.456
,
0.406
],
"input_std"
:
[
0.229
,
0.224
,
0.225
],
"input_std"
:
[
0.229
,
0.224
,
0.225
],
"learning_strategy"
:
{
"learning_strategy"
:
{
"name"
:
"piecewise_decay"
,
"name"
:
"piecewise_decay"
,
"batch_size"
:
1
,
"batch_size"
:
batch_size
,
"epochs"
:
[
30
,
60
,
90
],
"epochs"
:
[
30
,
60
,
90
],
"steps"
:
[
0.1
,
0.01
,
0.001
,
0.0001
]
"steps"
:
[
0.1
,
0.01
,
0.001
,
0.0001
]
},
},
"batch_size"
:
1
,
"batch_size"
:
batch_size
,
"lr"
:
0.1
,
"lr"
:
0.1
,
"total_images"
:
1281164
,
"total_images"
:
1281164
,
}
}
...
@@ -56,6 +57,7 @@ def optimizer_setting(params):
...
@@ -56,6 +57,7 @@ def optimizer_setting(params):
lr
=
[]
lr
=
[]
lr
=
[
base_lr
*
(
0.1
**
i
)
for
i
in
range
(
len
(
bd
)
+
1
)]
lr
=
[
base_lr
*
(
0.1
**
i
)
for
i
in
range
(
len
(
bd
)
+
1
)]
optimizer
=
fluid
.
optimizer
.
SGD
(
learning_rate
=
params
[
"lr"
])
optimizer
=
fluid
.
optimizer
.
SGD
(
learning_rate
=
params
[
"lr"
])
# TODO(minqiyang): Add learning rate scheduler support to imperative mode
# optimizer = fluid.optimizer.Momentum(
# optimizer = fluid.optimizer.Momentum(
# learning_rate=params["lr"],
# learning_rate=params["lr"],
# learning_rate=fluid.layers.piecewise_decay(
# learning_rate=fluid.layers.piecewise_decay(
...
@@ -208,8 +210,12 @@ class TestImperativeResnet(unittest.TestCase):
...
@@ -208,8 +210,12 @@ class TestImperativeResnet(unittest.TestCase):
resnet
=
ResNet
()
resnet
=
ResNet
()
optimizer
=
optimizer_setting
(
train_parameters
)
optimizer
=
optimizer_setting
(
train_parameters
)
np
.
random
.
seed
(
seed
)
import
random
random
.
seed
=
seed
train_reader
=
paddle
.
batch
(
train_reader
=
paddle
.
batch
(
paddle
.
dataset
.
flowers
.
train
(),
batch_size
=
batch_size
)
paddle
.
dataset
.
flowers
.
train
(
use_xmap
=
False
),
batch_size
=
batch_size
)
dy_param_init_value
=
{}
dy_param_init_value
=
{}
for
param
in
fluid
.
default_main_program
().
global_block
(
for
param
in
fluid
.
default_main_program
().
global_block
(
...
@@ -220,18 +226,22 @@ class TestImperativeResnet(unittest.TestCase):
...
@@ -220,18 +226,22 @@ class TestImperativeResnet(unittest.TestCase):
if
batch_id
>=
1
:
if
batch_id
>=
1
:
break
break
x_data
=
np
.
array
(
dy_
x_data
=
np
.
array
(
[
x
[
0
].
reshape
(
3
,
224
,
224
)
for
x
in
data
]).
astype
(
'float32'
)
[
x
[
0
].
reshape
(
3
,
224
,
224
)
for
x
in
data
]).
astype
(
'float32'
)
print
(
'dy input shape'
,
dy_x_data
.
shape
)
y_data
=
np
.
array
([
x
[
1
]
for
x
in
data
]).
astype
(
'int64'
).
reshape
(
y_data
=
np
.
array
([
x
[
1
]
for
x
in
data
]).
astype
(
'int64'
).
reshape
(
batch_size
,
1
)
batch_size
,
1
)
img
=
to_variable
(
x_data
)
img
=
to_variable
(
dy_
x_data
)
label
=
to_variable
(
y_data
)
label
=
to_variable
(
y_data
)
label
.
_stop_gradient
=
True
label
.
_stop_gradient
=
True
out
=
resnet
(
img
)
out
=
resnet
(
img
)
loss
=
fluid
.
layers
.
cross_entropy
(
input
=
out
,
label
=
label
)
loss
=
fluid
.
layers
.
cross_entropy
(
input
=
out
,
label
=
label
)
avg_loss
=
fluid
.
layers
.
mean
(
x
=
loss
)
avg_loss
=
fluid
.
layers
.
mean
(
x
=
loss
)
print
(
'shapex '
,
avg_loss
.
shape
)
dy_out
=
avg_loss
.
_numpy
()
dy_out
=
avg_loss
.
_numpy
()
if
batch_id
==
0
:
if
batch_id
==
0
:
...
@@ -241,6 +251,15 @@ class TestImperativeResnet(unittest.TestCase):
...
@@ -241,6 +251,15 @@ class TestImperativeResnet(unittest.TestCase):
dy_param_init_value
[
param
.
name
]
=
param
.
_numpy
()
dy_param_init_value
[
param
.
name
]
=
param
.
_numpy
()
avg_loss
.
_backward
()
avg_loss
.
_backward
()
dy_grad_value
=
{}
for
param
in
fluid
.
default_main_program
().
global_block
(
).
all_parameters
():
if
not
param
.
stop_gradient
:
np_array
=
np
.
array
(
param
.
_ivar
.
_grad_ivar
().
value
()
.
get_tensor
())
dy_grad_value
[
param
.
name
+
core
.
grad_var_suffix
(
)]
=
np_array
optimizer
.
minimize
(
avg_loss
)
optimizer
.
minimize
(
avg_loss
)
dy_param_value
=
{}
dy_param_value
=
{}
...
@@ -256,8 +275,13 @@ class TestImperativeResnet(unittest.TestCase):
...
@@ -256,8 +275,13 @@ class TestImperativeResnet(unittest.TestCase):
resnet
=
ResNet
()
resnet
=
ResNet
()
optimizer
=
optimizer_setting
(
train_parameters
)
optimizer
=
optimizer_setting
(
train_parameters
)
np
.
random
.
seed
(
seed
)
import
random
random
.
seed
=
seed
train_reader
=
paddle
.
batch
(
train_reader
=
paddle
.
batch
(
paddle
.
dataset
.
flowers
.
train
(),
batch_size
=
batch_size
)
paddle
.
dataset
.
flowers
.
train
(
use_xmap
=
False
),
batch_size
=
batch_size
)
img
=
fluid
.
layers
.
data
(
img
=
fluid
.
layers
.
data
(
name
=
'pixel'
,
shape
=
[
3
,
224
,
224
],
dtype
=
'float32'
)
name
=
'pixel'
,
shape
=
[
3
,
224
,
224
],
dtype
=
'float32'
)
...
@@ -267,12 +291,21 @@ class TestImperativeResnet(unittest.TestCase):
...
@@ -267,12 +291,21 @@ class TestImperativeResnet(unittest.TestCase):
avg_loss
=
fluid
.
layers
.
mean
(
x
=
loss
)
avg_loss
=
fluid
.
layers
.
mean
(
x
=
loss
)
optimizer
.
minimize
(
avg_loss
)
optimizer
.
minimize
(
avg_loss
)
print
(
'avg_loss shape'
,
avg_loss
.
shape
)
print
(
fluid
.
default_main_program
())
# initialize params and fetch them
# initialize params and fetch them
static_param_init_value
=
{}
static_param_init_value
=
{}
static_param_name_list
=
[]
static_param_name_list
=
[]
static_grad_name_list
=
[]
for
param
in
fluid
.
default_startup_program
().
global_block
(
for
param
in
fluid
.
default_startup_program
().
global_block
(
).
all_parameters
():
).
all_parameters
():
static_param_name_list
.
append
(
param
.
name
)
static_param_name_list
.
append
(
param
.
name
)
for
param
in
fluid
.
default_main_program
().
global_block
(
).
all_parameters
():
if
not
param
.
stop_gradient
:
static_grad_name_list
.
append
(
param
.
name
+
core
.
grad_var_suffix
())
out
=
exe
.
run
(
fluid
.
default_startup_program
(),
out
=
exe
.
run
(
fluid
.
default_startup_program
(),
fetch_list
=
static_param_name_list
)
fetch_list
=
static_param_name_list
)
...
@@ -284,34 +317,49 @@ class TestImperativeResnet(unittest.TestCase):
...
@@ -284,34 +317,49 @@ class TestImperativeResnet(unittest.TestCase):
if
batch_id
>=
1
:
if
batch_id
>=
1
:
break
break
x_data
=
np
.
array
(
static_
x_data
=
np
.
array
(
[
x
[
0
].
reshape
(
3
,
224
,
224
)
for
x
in
data
]).
astype
(
'float32'
)
[
x
[
0
].
reshape
(
3
,
224
,
224
)
for
x
in
data
]).
astype
(
'float32'
)
y_data
=
np
.
array
([
x
[
1
]
for
x
in
data
]).
astype
(
'int64'
).
reshape
(
y_data
=
np
.
array
([
x
[
1
]
for
x
in
data
]).
astype
(
'int64'
).
reshape
(
[
batch_size
,
1
])
[
batch_size
,
1
])
fetch_list
=
[
loss
.
name
]
fetch_list
=
[
avg_
loss
.
name
]
fetch_list
.
extend
(
static_param_name_list
)
fetch_list
.
extend
(
static_param_name_list
)
fetch_list
.
extend
(
static_grad_name_list
)
out
=
exe
.
run
(
fluid
.
default_main_program
(),
out
=
exe
.
run
(
fluid
.
default_main_program
(),
feed
=
{
"pixel"
:
x_data
,
feed
=
{
"pixel"
:
static_
x_data
,
"label"
:
y_data
},
"label"
:
y_data
},
fetch_list
=
fetch_list
)
fetch_list
=
fetch_list
)
static_param_value
=
{}
static_param_value
=
{}
static_grad_value
=
{}
static_out
=
out
[
0
]
static_out
=
out
[
0
]
for
i
in
range
(
1
,
len
(
out
)):
param_start_pos
=
1
static_param_value
[
static_param_name_list
[
i
-
1
]]
=
out
[
i
]
grad_start_pos
=
len
(
static_param_name_list
)
+
param_start_pos
for
i
in
range
(
param_start_pos
,
len
(
static_param_name_list
)
+
param_start_pos
):
static_param_value
[
static_param_name_list
[
i
-
param_start_pos
]]
=
out
[
i
]
for
i
in
range
(
grad_start_pos
,
len
(
static_grad_name_list
)
+
grad_start_pos
):
static_grad_value
[
static_grad_name_list
[
i
-
grad_start_pos
]]
=
out
[
i
]
self
.
assertTrue
(
np
.
allclose
(
static_out
,
dy_out
))
self
.
assertEqual
(
len
(
dy_param_init_value
),
len
(
static_param_init_value
))
for
key
,
value
in
six
.
iteritems
(
static_param_init_value
):
self
.
assertTrue
(
np
.
allclose
(
value
,
dy_param_init_value
[
key
]))
self
.
assertTrue
(
np
.
allclose
(
static_out
.
all
(),
dy_out
.
all
()))
self
.
assertEqual
(
len
(
dy_grad_value
),
len
(
static_grad_value
))
# TODO(minqiyang): find a way to align the gradient
# for key, value in six.iteritems(static_grad_value):
# self.assertTrue(
# np.allclose(value, dy_grad_value[key]))
for
key
,
value
in
six
.
iteritems
(
static_param_init_value
):
self
.
assertEqual
(
len
(
dy_param_value
),
len
(
static_param_value
))
self
.
assertTrue
(
# for key, value in six.iteritems(static_param_value):
np
.
allclose
(
value
.
all
(),
dy_param_init_value
[
key
].
all
()))
# self.assertTrue(np.allclose(value, dy_param_value[key]))
for
key
,
value
in
six
.
iteritems
(
static_param_value
):
if
not
np
.
allclose
(
value
.
all
(),
dy_param_value
[
key
].
all
()):
print
(
key
)
print
(
value
,
dy_param_value
[
key
])
self
.
assertTrue
(
np
.
allclose
(
value
.
all
(),
dy_param_value
[
key
].
all
()))
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录