提交 49a7fba8 编写于 作者: M minqiyang

Polish code

test=develop
上级 159c4073
...@@ -141,11 +141,13 @@ class VarBase { ...@@ -141,11 +141,13 @@ class VarBase {
void RunBackward(); void RunBackward();
void TrackPreOp(OpBase* pre_op, const std::string& pre_op_out_name, void TrackPreOp(OpBase* pre_op, const std::string& pre_op_out_name,
int pre_op_out_idx, bool stop_gradient) { int pre_op_out_idx, bool pre_op_stop_gradient) {
pre_op_ = pre_op; pre_op_ = pre_op;
pre_op_out_name_ = pre_op_out_name; pre_op_out_name_ = pre_op_out_name;
pre_op_out_idx_ = pre_op_out_idx; pre_op_out_idx_ = pre_op_out_idx;
stop_gradient_ = stop_gradient; if (pre_op_stop_gradient) {
stop_gradient_ = pre_op_stop_gradient;
}
} }
void ClearGradient() { void ClearGradient() {
......
...@@ -51,9 +51,8 @@ class Layer(core.Layer): ...@@ -51,9 +51,8 @@ class Layer(core.Layer):
return params return params
def clear_gradients(self): def clear_gradients(self):
print([p.name for p in self.parameters()])
for p in self.parameters(): for p in self.parameters():
if p.name not in set(['batch_norm_0.w_2', 'batch_norm_0.w_1']): if not p._stop_gradient:
p._clear_gradient() p._clear_gradient()
def _build_once(self, inputs): def _build_once(self, inputs):
......
...@@ -168,22 +168,22 @@ class ResNet(fluid.imperative.Layer): ...@@ -168,22 +168,22 @@ class ResNet(fluid.imperative.Layer):
self.pool2d_max = Pool2D( self.pool2d_max = Pool2D(
pool_size=3, pool_stride=2, pool_padding=1, pool_type='max') pool_size=3, pool_stride=2, pool_padding=1, pool_type='max')
# self.bottleneck_block_list = [] self.bottleneck_block_list = []
# num_channels = 64 num_channels = 64
# for block in range(len(depth)): for block in range(len(depth)):
# shortcut = False shortcut = False
# for i in range(depth[block]): for i in range(depth[block]):
# bottleneck_block = BottleneckBlock( bottleneck_block = BottleneckBlock(
# num_channels=num_channels, num_channels=num_channels,
# num_filters=num_filters[block], num_filters=num_filters[block],
# stride=2 if i == 0 and block != 0 else 1, stride=2 if i == 0 and block != 0 else 1,
# shortcut=shortcut) shortcut=shortcut)
# num_channels = bottleneck_block._num_channels_out num_channels = bottleneck_block._num_channels_out
# self.bottleneck_block_list.append(bottleneck_block) self.bottleneck_block_list.append(bottleneck_block)
# shortcut = True shortcut = True
# self.pool2d_avg = Pool2D( self.pool2d_avg = Pool2D(
# pool_size=7, pool_type='avg', global_pooling=True) pool_size=7, pool_type='avg', global_pooling=True)
import math import math
stdv = 1.0 / math.sqrt(2048 * 1.0) stdv = 1.0 / math.sqrt(2048 * 1.0)
...@@ -196,9 +196,9 @@ class ResNet(fluid.imperative.Layer): ...@@ -196,9 +196,9 @@ class ResNet(fluid.imperative.Layer):
def forward(self, inputs): def forward(self, inputs):
y = self.conv(inputs) y = self.conv(inputs)
y = self.pool2d_max(y) y = self.pool2d_max(y)
# for bottleneck_block in self.bottleneck_block_list: for bottleneck_block in self.bottleneck_block_list:
# y = bottleneck_block(y) y = bottleneck_block(y)
# y = self.pool2d_avg(y) y = self.pool2d_avg(y)
y = self.out(y) y = self.out(y)
return y return y
...@@ -209,7 +209,7 @@ class TestImperativeResnet(unittest.TestCase): ...@@ -209,7 +209,7 @@ class TestImperativeResnet(unittest.TestCase):
batch_size = train_parameters["batch_size"] batch_size = train_parameters["batch_size"]
batch_num = 1 batch_num = 1
with fluid.imperative.guard(place=fluid.CPUPlace()): with fluid.imperative.guard():
fluid.default_startup_program().random_seed = seed fluid.default_startup_program().random_seed = seed
fluid.default_main_program().random_seed = seed fluid.default_main_program().random_seed = seed
...@@ -275,9 +275,8 @@ class TestImperativeResnet(unittest.TestCase): ...@@ -275,9 +275,8 @@ class TestImperativeResnet(unittest.TestCase):
fluid.default_startup_program().random_seed = seed fluid.default_startup_program().random_seed = seed
fluid.default_main_program().random_seed = seed fluid.default_main_program().random_seed = seed
exe = fluid.Executor(fluid.CPUPlace()) exe = fluid.Executor(fluid.CPUPlace(
# exe = fluid.Executor(fluid.CPUPlace( ) if not core.is_compiled_with_cuda() else fluid.CUDAPlace(0))
# ) if not core.is_compiled_with_cuda() else fluid.CUDAPlace(0))
resnet = ResNet() resnet = ResNet()
optimizer = optimizer_setting(train_parameters) optimizer = optimizer_setting(train_parameters)
...@@ -347,7 +346,6 @@ class TestImperativeResnet(unittest.TestCase): ...@@ -347,7 +346,6 @@ class TestImperativeResnet(unittest.TestCase):
static_grad_value[static_grad_name_list[ static_grad_value[static_grad_name_list[
i - grad_start_pos]] = out[i] i - grad_start_pos]] = out[i]
print(static_out, dy_out)
self.assertTrue(np.allclose(static_out, dy_out)) self.assertTrue(np.allclose(static_out, dy_out))
self.assertEqual(len(dy_param_init_value), len(static_param_init_value)) self.assertEqual(len(dy_param_init_value), len(static_param_init_value))
...@@ -358,9 +356,7 @@ class TestImperativeResnet(unittest.TestCase): ...@@ -358,9 +356,7 @@ class TestImperativeResnet(unittest.TestCase):
self.assertEqual(len(dy_grad_value), len(static_grad_value)) self.assertEqual(len(dy_grad_value), len(static_grad_value))
for key, value in six.iteritems(static_grad_value): for key, value in six.iteritems(static_grad_value):
if not np.allclose(value, dy_grad_value[key]): self.assertTrue(np.allclose(value, dy_grad_value[key]))
print(key)
#self.assertTrue(np.allclose(value, dy_grad_value[key]))
self.assertTrue(np.isfinite(value.all())) self.assertTrue(np.isfinite(value.all()))
self.assertFalse(np.isnan(value.any())) self.assertFalse(np.isnan(value.any()))
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册