from mxnet import nd
from mxnet.gluon import nn
Create your neural network’s first layer
创建一个dense layer with 2 output units.
layer = nn.Dense(2)
layer
输出:
Dense(None -> 2, linear)
使用默认方式初始化层的权重, 即从 [−0.7,0.7] 均匀采样
layer.initialize()
用随机生成的数据演示前向传播
x = nd.random.uniform(-1,1,(3,4))
layer(x)
输出:
[[ 0.0009278 -0.00674768]
[-0.02683341 0.00671751]
[ 0.00798804 0.02131375]]
<NDArray 3x2 @cpu(0)>
获取层的权重参数
layer.weight.data()
输出:
[[-0.01631819 -0.00312688 0.0408415 0.04370362]
[ 0.00404529 -0.0028032 0.00952624 -0.01501013]]
<NDArray 2x4 @cpu(0)>
Chain layers into a neural network
利用 nn.Sequential实现LeNet的方法
net = nn.Sequential()
# Add a sequence of layers.
net.add(# Similar to Dense, it is not necessary to specify the input channels
# by the argument `in_channels`, which will be automatically inferred
# in the first forward pass. Also, we apply a relu activation on the
# output. In addition, we can use a tuple to specify a non-square
# kernel size, such as `kernel_size=(2,4)`
nn.Conv2D(channels=6, kernel_size=5, activation='relu'),
# One can also use a tuple to specify non-symmetric pool and stride sizes
nn.MaxPool2D(pool_size=2, strides=2),
nn.Conv2D(channels=16, kernel_size=3, activation='relu'),
nn.MaxPool2D(pool_size=2, strides=2),
# The dense layer will automatically reshape the 4-D output of last
# max pooling layer into the 2-D shape: (x.shape[0], x.size/x.shape[0])
nn.Dense(120, activation="relu"),
nn.Dense(84, activation="relu"),
nn.Dense(10))
net
输出:
Sequential(
(0): Conv2D(None -> 6, kernel_size=(5, 5), stride=(1, 1), Activation(relu))
(1): MaxPool2D(size=(2, 2), stride=(2, 2), padding=(0, 0), ceil_mode=False, global_pool=False, pool_type=max, layout=NCHW)
(2): Conv2D(None -> 16, kernel_size=(3, 3), stride=(1, 1), Activation(relu))
(3): MaxPool2D(size=(2, 2), stride=(2, 2), padding=(0, 0), ceil_mode=False, global_pool=False, pool_type=max, layout=NCHW)
(4): Dense(None -> 120, Activation(relu))
(5): Dense(None -> 84, Activation(relu))
(6): Dense(None -> 10, linear)
)
初始化网络权重,之后进行前向传播
net.initialize()
# Input shape is (batch_size, color_channels, height, width)
x = nd.random.uniform(shape=(4,1,28,28))
y = net(x)
y.shape
可以用 []来index指定层
下面给出1st layer’s weight 和 6th layer’s bias.的shape获取方法
(net[0].weight.data().shape, net[5].bias.data().shape)
输出:
((6, 1, 5, 5), (84,))
Create a neural network flexibly
灵活的创建神经网络,上一节是给出了一种创建序列结构的神经网络的方法,但是很多方法并不是序列结构的,下面给出一种自由的方法。
构建一种flexible forward function
下面
init create the layers
forward define the forward function.
class MixMLP(nn.Block):
def __init__(self, **kwargs):
# Run `nn.Block`'s init method
super(MixMLP, self).__init__(**kwargs)
self.blk = nn.Sequential()
self.blk.add(nn.Dense(3, activation='relu'),
nn.Dense(4, activation='relu'))
self.dense = nn.Dense(5)
def forward(self, x):
y = nd.relu(self.blk(x))
print(y)
return self.dense(y)
net = MixMLP()
net
输出:
MixMLP(
(blk): Sequential(
(0): Dense(None -> 3, Activation(relu))
(1): Dense(None -> 4, Activation(relu))
)
(dense): Dense(None -> 5, linear)
)
初始化,进行前向传播示例:
net.initialize()
x = nd.random.uniform(shape=(2,2))
net(x)
获取网络特定层权值示例:
net.blk[1].weight.data()