从零开始
前面了解了多层感知机的原理,我们来实现一个多层感知机。
# -*- coding: utf-8 -*-
from mxnet import init
from mxnet import ndarray as nd
from mxnet.gluon import loss as gloss
import gb
# 定义数据源
batch_size = 256
train_iter, test_iter = gb.load_data_fashion_mnist(batch_size)
# 定义模型参数
num_inputs = 784
num_outputs = 10
num_hiddens = 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()
# 定义激活函数
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 = 5
lr = 0.5
gb.train_cpu(net, train_iter, test_iter, loss, num_epochs, batch_size,
params, lr)
添加隐层后,模型的性能大幅提升
# output
epoch 1, loss 0.5029, train acc 0.852, test acc 0.934
epoch 2, loss 0.2000, train acc 0.943, test acc 0.956
epoch 3, loss 0.1431, train acc 0.959, test acc 0.964
epoch 4, loss 0.1138, train acc 0.967, test acc 0.968
epoch 5, loss 0.0939, train acc 0.973, test acc 0.973
在定义模型参数和定义模型步骤,仍然有一些繁琐。
使用Gluon
# -*- coding: utf-8 -*-
from mxnet import init
from mxnet import ndarray as nd
from mxnet.gluon import loss as gloss
import gb
# 定义数据源
batch_size = 256
train_iter, test_iter = gb.load_data_fashion_mnist(batch_size)
# 定义模型
from mxnet.gluon import nn
net = nn.Sequential()
net.add(nn.Dense(256, activation='relu'))
net.add(nn.Dense(10))
net.add(nn.Dense(10))
net.initialize(init.Normal(sigma=0.01))
# 定义损失函数
loss = gloss.SoftmaxCrossEntropyLoss()
# 训练模型
from mxnet import gluon
trainer = gluon.Trainer(net.collect_params(), 'sgd', {'learning_rate': 0.5})
num_epochs = 5
gb.train_cpu(net, train_iter, test_iter, loss, num_epochs, batch_size,
None, None, trainer)
# output
epoch 1, loss 1.3065, train acc 0.525, test acc 0.814
epoch 2, loss 0.2480, train acc 0.928, test acc 0.950
epoch 3, loss 0.1442, train acc 0.958, test acc 0.961
epoch 4, loss 0.1060, train acc 0.969, test acc 0.971
epoch 5, loss 0.0807, train acc 0.976, test acc 0.973
原文地址:https://www.cnblogs.com/houkai/p/9520970.html
时间: 2024-11-09 09:23:49