????????神经网络里面有很多不同的层,全连接层、卷积层、池化层等等,在MXNet框架中的Gluon自带了很多常用的层,但很多时候我们想自己创建一个层,可以在不同的地方重复被调用。
不含模型参数的自定义层
#前向计算中返回减掉均值的值
from mxnet import gluon,nd
from mxnet.gluon import nn
#不带参数的层
class CustomLayer(nn.Block):
def __init__(self,**kwargs):
super(CustomLayer,self).__init__(**kwargs)
def forward(self,x):
return x-x.mean()
layer1=CustomLayer()
X=nd.array([6,4,15,5,10])
layer1(X)
'''
[-2. -4. 7. -3. 2.]
<NDArray 5 @cpu(0)>
'''
放到多层里面看看
net=nn.Sequential()
net.add(nn.Dense(10),CustomLayer())
net.initialize()
X=nd.random.uniform(shape=(3,4))
y=net(X)
print(y)
print(y.mean())
'''
[[-0.00265966 -0.00993038 0.05119815 0.03966434 -0.00209734 0.03759544
0.04753209 0.01199018 0.06117208 -0.04921837]
[ 0.01035871 -0.0496955 -0.04076268 0.00558676 -0.01279666 -0.06531943
0.01902303 -0.01878176 0.0925374 -0.03170357]
[ 0.01041228 -0.05810181 -0.02959615 0.00663922 -0.02288266 -0.05688043
0.02015793 -0.02125452 0.0989683 -0.04115499]]
<NDArray 3x10 @cpu(0)>
[4.967054e-10]
<NDArray 1 @cpu(0)>
均值是浮点数,值很接近于0
'''
含模型参数的自定义层
模型参数的访问等可以参看:MXNet模型参数的访问、初始化、自定义初始化、共享
这里的参数就用到Block类自带的ParameterDict类
P=gluon.ParameterDict()
x=p.get('tony1',shape=(3,4))
y=p.get('tony2',shape=(3,4))
print(x,y)
'''
Parameter tony1 (shape=(3, 4), dtype=<class 'numpy.float32'>) Parameter tony2 (shape=(3, 4), dtype=<class 'numpy.float32'>)
'''
?根据上面的实现,现在定义一个层:
#带参数的层
class CustomLayer2(nn.Block):
#in_units:该层的输入个数,units:该层的输出个数
def __init__(self,in_units,units,**kwargs):
super(CustomLayer2,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)
layer=CustomLayer2(in_units=5,units=3)
print(layer.params)
layer.initialize()
print(layer(nd.random.uniform(shape=(2,5))))
'''
customlayer23_ (
Parameter customlayer23_weight (shape=(5, 3), dtype=<class 'numpy.float32'>)
Parameter customlayer23_bias (shape=(3,), dtype=<class 'numpy.float32'>)
)
[[0.07319182 0. 0. ]
[0.10009611 0. 0. ]]
<NDArray 2x3 @cpu(0)>
'''
?当然也可以跟其他层一样来构造模型:
net=nn.Sequential()
net.add(CustomLayer2(64,8),CustomLayer2(8,1))
net.initialize()
net(nd.random.uniform(shape=(2,64)))
'''
[[0.01317193]
[0.01314665]]
<NDArray 2x1 @cpu(0)>
'''
|