1.从零实现多层感知机:
import d2lzh as d2l
from mxnet import nd
from mxnet.gluon import loss as gloss
# 读取数据集,并设置批量大小为256
batch_size = 256
train_iter,test_iter = d2l.load_data_fashion_mnist(batch_size)
# 定义模型参数
num_inputs , num_outputs , num_hiddens = 784 , 10 , 256
W1 = nd.random.normal(scale=0.01,shape=(num_inputs,num_hiddens))
b1 = nd.zeros(num_hiddens)
W2 = nd.random.normal(scale=0.01,shape=(num_hiddens,num_outputs))
b2 = nd.zeros(num_outputs)
params = [W1,b1,W2,b2]
for param in params:
param.attach_grad()
# 定义激活函数RELU
def relu(X):
return nd.maximum(X,0)
# 定义模型
def net(X):
X = X.reshape((-1,num_inputs))
H = relu(nd.dot(X,W1) + b1)
return nd.dot(H,W2) + b2
# 定义损失函数
loss = gloss.SoftmaxCrossEntropyLoss()
# 训练模型
num_epochs , lr = 10,0.5
d2l.train_ch3(net,train_iter,test_iter,loss,num_epochs,batch_size,params,lr)
迭代10轮得到如下结果:
epoch 1, loss 0.7740, train acc 0.710, test acc 0.808
epoch 2, loss 0.4880, train acc 0.820, test acc 0.853
epoch 3, loss 0.4289, train acc 0.840, test acc 0.860
epoch 4, loss 0.3964, train acc 0.854, test acc 0.870
epoch 5, loss 0.3702, train acc 0.864, test acc 0.874
epoch 6, loss 0.3539, train acc 0.868, test acc 0.871
epoch 7, loss 0.3373, train acc 0.876, test acc 0.881
epoch 8, loss 0.3283, train acc 0.878, test acc 0.879
epoch 9, loss 0.3108, train acc 0.885, test acc 0.880
epoch 10, loss 0.3062, train acc 0.887, test acc 0.885
2.多层感知机的简洁实现:
import d2lzh as d2l
from mxnet import gluon,init
from mxnet.gluon import loss as gloss,nn
# 定义模型
net = nn.Sequential()
net.add(nn.Dense(256,activation='relu'),nn.Dense(10))
net.initialize(init.Normal(sigma=0.01))
# 训练模型
batch_size = 256
train_iter,test_iter = d2l.load_data_fashion_mnist(batch_size)
loss = gloss.SoftmaxCrossEntropyLoss()
trainer = gluon.Trainer(net.collect_params(),'sgd',{'learning_rate':0.5})
num_eppochs = 10
d2l.train_ch3(net,train_iter,test_iter,loss,num_epochs,batch_size,None,None,trainer)
迭代10轮得到如下结果:
epoch 1, loss 0.8447, train acc 0.689, test acc 0.815
epoch 2, loss 0.4975, train acc 0.815, test acc 0.849
epoch 3, loss 0.4345, train acc 0.839, test acc 0.844
epoch 4, loss 0.4003, train acc 0.852, test acc 0.852
epoch 5, loss 0.3772, train acc 0.861, test acc 0.870
epoch 6, loss 0.3586, train acc 0.867, test acc 0.867
epoch 7, loss 0.3470, train acc 0.872, test acc 0.877
epoch 8, loss 0.3331, train acc 0.876, test acc 0.875
epoch 9, loss 0.3236, train acc 0.880, test acc 0.876
epoch 10, loss 0.3172, train acc 0.883, test acc 0.868
Reference:
《动手学深度学习》