通过nn.Sequential构造LeNet ,MXNet将自动构造向前函数,按顺序执行添加的LeNet 卷积神经网络是一种特殊的多层神经网络。与几乎所有其他神经网络一样,它们都经过了反向传播算法的训练。它们的不同之处在于体系结构。
卷积神经网络是设计用来直接从像素图像中识别视觉模式的最小预处理。
它们能够识别具有极端可变性的模式(例如手写字符),并且对扭曲和简单几何变换具有鲁棒性。
from mxnet.gluon import nn
net = nn.Sequential()
#创建卷积神经网络
net.add(
#卷积层+池化层+卷积层+池化层+3个密集层,最后输出的第二维度为10
nn.Conv2D(channels=6, kernel_size=5, activation='relu'),
nn.MaxPool2D(pool_size=2, strides=2),
nn.Conv2D(channels=16, kernel_size=3, activation='relu'),
nn.MaxPool2D(pool_size=2, strides=2),
nn.Dense(120, activation="relu"),
nn.Dense(84, activation="relu"),
nn.Dense(10))
print(net)
Sequential(
(0): Conv2D(None -> 6, kernel_size=(5, 5), stride=(1, 1))
(1): MaxPool2D(size=(2, 2), stride=(2, 2), padding=(0, 0), ceil_mode=False)
(2): Conv2D(None -> 16, kernel_size=(3, 3), stride=(1, 1))
(3): MaxPool2D(size=(2, 2), stride=(2, 2), padding=(0, 0), ceil_mode=False)
(4): Dense(None -> 120, Activation(relu))
(5): Dense(None -> 84, Activation(relu))
(6): Dense(None -> 10, linear)
)
net.initialize()
#x shape:(batch_size, color_channels, height, width)
x = nd.random.uniform(shape=(4,1,28,28))
y = net(x)
print(y.shape)
print(net[0].weight.data().shape, net[5].bias.data().shape)
Sequential(
(0): Conv2D(None -> 6, kernel_size=(5, 5), stride=(1, 1))
(1): MaxPool2D(size=(2, 2), stride=(2, 2), padding=(0, 0), ceil_mode=False)
(2): Conv2D(None -> 16, kernel_size=(3, 3), stride=(1, 1))
(3): MaxPool2D(size=(2, 2), stride=(2, 2), padding=(0, 0), ceil_mode=False)
(4): Dense(None -> 120, Activation(relu))
(5): Dense(None -> 84, Activation(relu))
(6): Dense(None -> 10, linear)
)
(4, 10)
(6, 1, 5, 5) (84,)