提交 22956530 编写于 作者: M minqiyang

Polish PyLayers

test=develop
上级 0f6ef8ed
......@@ -24,19 +24,7 @@ __all__ = ['PyLayer']
class PyLayer(core.Layer):
def __init__(self,
dtype=core.VarDesc.VarType.FP32,
param_attr=None,
bias_attr=None,
name=None):
from ..layer_helper import LayerHelper
self._helper = LayerHelper(
type(self).__name__,
param_attr=param_attr,
bias_attr=bias_attr,
dtype=dtype,
name=name)
def __init__(self, dtype=core.VarDesc.VarType.FP32, name=None):
self._once_built = False
self._dtype = dtype
......
......@@ -46,8 +46,15 @@ class Conv2D(layers.PyLayer):
name=None,
dtype=core.VarDesc.VarType.FP32):
assert param_attr is not False, "param_attr should not be False here."
super(Conv2D, self).__init__(
param_attr=param_attr, bias_attr=bias_attr, name=name, dtype=dtype)
super(Conv2D, self).__init__(name=name, dtype=dtype)
from ..layer_helper import LayerHelper
self._helper = LayerHelper(
type(self).__name__,
param_attr=param_attr,
bias_attr=bias_attr,
dtype=dtype,
name=name)
self._groups = groups
self._stride = utils.convert_to_list(stride, 2, 'stride')
......@@ -163,6 +170,9 @@ class Pool2D(layers.PyLayer):
super(Pool2D, self).__init__(name=name, dtype=dtype)
from ..layer_helper import LayerHelper
self._helper = LayerHelper(type(self).__name__, dtype=dtype, name=name)
self._pool_type = pool_type
self._pool_size = utils.convert_to_list(pool_size, 2, 'pool_size')
self._pool_padding = utils.convert_to_list(pool_padding, 2,
......@@ -197,32 +207,22 @@ class Pool2D(layers.PyLayer):
class FC(layers.PyLayer):
def __init__(self,
size_in,
size_out,
num_flatten_dims=1,
size,
param_attr=None,
num_flatten_dims=1,
dtype=core.VarDesc.VarType.FP32):
super(FC, self).__init__(param_attr=param_attr, dtype=dtype)
self._size_in = size_in
self._size_out = size_out
super(FC, self).__init__()
self._size = size
self._num_flatten_dims = num_flatten_dims
self._dtype = dtype
if self._size_in != -1:
self._w = self._helper.create_parameter(
attr=self._helper.param_attr,
shape=[size_in, size_out],
dtype=self._dtype,
is_bias=False)
from ..layer_helper import LayerHelper
self._helper = LayerHelper('FC', param_attr=param_attr)
def _build_once(self, input):
if self._size_in != -1:
return
input_shape = input.shape
param_shape = [
reduce(lambda a, b: a * b, input_shape[self._num_flatten_dims:], 1)
] + [self._size_out]
] + [self._size]
self._w = self._helper.create_parameter(
attr=self._helper.param_attr,
shape=param_shape,
......
......@@ -9713,47 +9713,3 @@ def huber_loss(input, label, delta):
'Residual': residual},
attrs={'delta': delta})
return out
class FC(layers.PyLayer):
def __init__(self,
size,
param_attr=None,
num_flatten_dims=1,
dtype=core.VarDesc.VarType.FP32):
super(FC, self).__init__(param_attr=param_attr)
self._size = size
self._num_flatten_dims = num_flatten_dims
self._dtype = dtype
self._tmp = self._helper.create_variable_for_type_inference(self._dtype)
self._out = self._helper.create_variable_for_type_inference(self._dtype)
def _build_once(self, inputs):
input_shape = inputs.shape
param_shape = [
reduce(lambda a, b: a * b, input_shape[self._num_flatten_dims:], 1)
] + [self._size]
self._w = self._helper.create_parameter(
attr=self._helper.param_attr,
shape=param_shape,
dtype=self._dtype,
is_bias=False)
def forward(self, inputs):
self._helper.append_op(
type="mul",
inputs={"X": inputs,
"Y": self._w},
outputs={"Out": self._tmp},
attrs={
"x_num_col_dims": self._num_flatten_dims,
"y_num_col_dims": 1
})
self._helper.append_op(
type="sum",
inputs={"X": [self._tmp]},
outputs={"Out": self._out},
attrs={"use_mkldnn": False})
return self._out
......@@ -18,7 +18,7 @@ import numpy as np
import paddle.fluid as fluid
from paddle.fluid import core
from paddle.fluid.layers.nn import FC
from paddle.fluid.imperative.nn import FC
from test_imperative_base import new_program_scope
......
......@@ -74,7 +74,7 @@ class SimpleImgConvPool(fluid.imperative.PyLayer):
class MNIST(fluid.imperative.PyLayer):
def __init__(self, param_attr=None, bias_attr=None):
super(MNIST, self).__init__(param_attr=param_attr, bias_attr=bias_attr)
super(MNIST, self).__init__()
self._simple_img_conv_pool_1 = SimpleImgConvPool(
1, 20, 5, 2, 2, act="relu")
......@@ -85,8 +85,7 @@ class MNIST(fluid.imperative.PyLayer):
pool_2_shape = 50 * 8 * 8
SIZE = 10
scale = (2.0 / (pool_2_shape**2 * SIZE))**0.5
self._fc = FC(-1,
10,
self._fc = FC(10,
param_attr=fluid.param_attr.ParamAttr(
initializer=fluid.initializer.NormalInitializer(
loc=0.0, scale=scale)))
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册