MXNET:深度学习计算-自定义层

虽然 Gluon 提供了大量常用的层,但有时候我们依然希望自定义层。本节将介绍如何使用 NDArray 来自定义一个 Gluon 的层,从而以后可以被重复调用。

不含模型参数的自定义层

我们先介绍如何定义一个不含模型参数的自定义层。事实上,这和 “模型构造” 中介绍的使用 Block 构造模型类似。

通过继承 Block 自定义了一个将输入减掉均值的层:CenteredLayer 类,并将层的计算放在 forward 函数里。

class CenteredLayer(nn.Block):
    def __init__(self, **kwargs):
        super(CenteredLayer, self).__init__(**kwargs)

    def forward(self, x):
        return x - x.mean()

layer = CenteredLayer()
layer(nd.array([1, 2, 3, 4, 5]))
# output
[-2. -1.  0.  1.  2.]
<NDArray 5 @cpu(0)>

我们也可以用它来构造更复杂的模型

net = nn.Sequential()
net.add(nn.Dense(128))
net.add(nn.Dense(10))
net.add(CenteredLayer())
net.initialize()
y = net(nd.random.uniform(shape=(4, 8)))
y.mean()

含模型参数的自定义层

在自定义层的时候我们还可以使用 Block 自带的 ParameterDict 类型的成员变量 params。顾名思义,这是一个由字符串类型的参数名字映射到 Parameter 类型的模型参数的字典。我们可以通过 get 函数从 ParameterDict 创建 Parameter。

params = gluon.ParameterDict()
params.get('param2', shape=(2, 3))
params
# ouput
(
  Parameter param2 (shape=(2, 3), dtype=<class 'numpy.float32'>)
)

现在我们看下如何实现一个含权重参数和偏差参数的全连接层。它使用 ReLU 作为激活函数。其中 in_units 和 units 分别是输入单元个数和输出单元个数。

class MyDense(nn.Block):
    def __init__(self, units, in_units, **kwargs):
        super(MyDense, self).__init__(**kwargs)
        self.weight = self.params.get('weight', shape=(in_units, units))
        self.bias = self.params.get('bias', shape=(units,))

    def forward(self, x):
        linear = nd.dot(x, self.weight.data()) + self.bias.data()
        return nd.relu(linear)

我们实例化 MyDense 类来看下它的模型参数。

# units:该层的输出个数;in_units:该层的输入个数。
dense = MyDense(units=5, in_units=10)
dense.params
# output
mydense0_ (
  Parameter mydense0_weight (shape=(10, 5), dtype=<class 'numpy.float32'>)
  Parameter mydense0_bias (shape=(5,), dtype=<class 'numpy.float32'>)
)

我们也可以使用自定义层构造模型。它用起来和 Gluon 的其他层很类似。

net = nn.Sequential()
net.add(MyDense(32, in_units=64))
net.add(MyDense(2, in_units=32))
net.initialize()
net(nd.random.uniform(shape=(2, 64)))
posted @ 2018-08-23 10:17  侯凯  阅读(1170)  评论(0编辑  收藏  举报