欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

MXNET深度学习框架-16-使用gluon实现dropout

程序员文章站 2022-03-06 10:03:38
...

        上一章从0开始实现了dropout方法,已经dropout应用在mlp中的实例,本章我们使用gluon实现带有dropout的mlp:

import mxnet.gluon as gn
import mxnet.autograd as ag
import mxnet.ndarray as nd
import mxnet.gluon as gn



'''---模型训练实例(引入dropout)---'''
# 预处理
def transform(data, label):
    return data.astype("float32") / 255, label.astype("float32")  # 样本归一化


mnist_train = gn.data.vision.FashionMNIST(train=True)
mnist_test = gn.data.vision.FashionMNIST(train=False)
data, label = mnist_train[0:9]
print(data.shape, label)  # 查看数据维度
import matplotlib.pyplot as plt


def show_image(image):  # 显示图像
    n = image.shape[0]
    _, figs = plt.subplots(1, n, figsize=(15, 15))
    for i in range(n):
        figs[i].imshow(image[i].reshape((28, 28)).asnumpy())
    plt.show()


def get_fashion_mnist_labels(labels):  # 显示图像标签
    text_labels = ['t-shirt', 'trouser', 'pullover', 'dress', 'coat',
                   'sandal', 'shirt', 'sneaker', 'bag', 'ankle boot']
    return [text_labels[int(i)] for i in labels]

#
# show_image(data)
# print(get_fashion_mnist_labels(label))

'''----数据读取----'''
batch_size = 100
transformer = gn.data.vision.transforms.ToTensor()
train_data = gn.data.DataLoader(dataset=mnist_train, batch_size=batch_size, shuffle=True)
test_data = gn.data.DataLoader(dataset=mnist_test, batch_size=batch_size, shuffle=False)

'''----初始化模型参数----'''

'''----定义模型----'''

dropout1,dropout2=0.2,0.5
net=gn.nn.Sequential()
net.add(gn.nn.Dense(256,activation="relu"),
        gn.nn.Dropout(dropout1),
        gn.nn.Dense(128, activation="relu"),
        gn.nn.Dropout(dropout2),
        gn.nn.Dense(10)
        )
net.initialize()



# softmax和交叉熵损失函数
# 由于将它们分开会导致数值不稳定(前两章博文的结果可以对比),所以直接使用gluon提供的API
cross_loss=gn.loss.SoftmaxCrossEntropyLoss()

# 定义准确率
def accuracy(output,label):
    return nd.mean(output.argmax(axis=1)==label).asscalar()

def evaluate_accuracy(data_iter,net):# 定义测试集准确率
    acc=0
    for data,label in data_iter:
        data,label=transform(data,label)
        output=net(data)  # 测试的时候dropout必须为0
        acc+=accuracy(output,label)
    return acc/len(data_iter)

# softmax和交叉熵分开的话数值可能会不稳定
cross_loss=gn.loss.SoftmaxCrossEntropyLoss()
# 优化
train_step=gn.Trainer(net.collect_params(),'sgd',{"learning_rate":0.1})

# 训练
lr=0.1
epochs=20
for epoch in range(epochs):
    train_loss=0
    train_acc=0
    for image,y in train_data:
        image,y=transform(image,y) # 类型转换,数据归一化
        with ag.record():
            output = net(image)
            loss = cross_loss(output, y)
        loss.backward()
        train_step.step(batch_size)
        train_loss += nd.mean(loss).asscalar()
        train_acc += accuracy(output, y)
    test_acc = evaluate_accuracy(test_data, net)
    print("Epoch %d, Loss:%f, Train acc:%f, Test acc:%f"
          %(epoch,train_loss/len(train_data),train_acc/len(train_data),test_acc))
'''----预测-------'''
# 训练完成后,可对样本进行预测
image_10,label_10=mnist_test[:10] #拿到前10个数据
show_image(image_10)
print("真实样本标签:",label_10)
print("真实数字标签对应的服饰名:",get_fashion_mnist_labels(label_10))

image_10,label_10=transform(image_10,label_10)
predict_label=net(image_10).argmax(axis=1)
print("预测样本标签:",predict_label.astype("int8"))
print("预测数字标签对应的服饰名:",get_fashion_mnist_labels(predict_label.asnumpy()))


训练结果:
MXNET深度学习框架-16-使用gluon实现dropout