# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

import mxnet as mx
from mxnet.gluon import nn
from mxnet import nd

# 使用 NDArray 实现一个 Gluon 层
from mxnet import gluon
my_param = gluon.Parameter('my parameter', shape=(3, 3))

my_param.initialize()
print(my_param.data())
my_param.grad()

# 自定义层时 通常使用 nn.Block 自带的一个 ParameterDict 类型的成员变量 params
pd = gluon.ParameterDict(prefix='block1_')
pd.get('my_parameter1', shape=(3, 3))

pd


class MyDense(nn.Block):
    def __init__(self, units, in_units, **kwargs):
        super(MyDense, self).__init__(**kwargs)
        with self.name_scope():
            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)


dense = MyDense(5, in_units=10, prefix='o_my_dense_')
dense.params

dense.initialize()
dense(nd.random.uniform(shape=(100, 10)))

net = nn.Sequential()
with net.name_scope():
    net.add(MyDense(32, in_units=64))
    net.add(MyDense(2, in_units=32))
net.initialize()
net(nd.random.uniform(shape=(2,64)))



StarcoderdataJupyterScriptsDedupFiltered