多层感知机

之前学习了线性模型,我们可以很容易想到,现实生活中很多的现象用线性是无法拟合的,所以研究人员就想到了在线性之后添加一些非线性的激活函数,使得整个网络获得非线性,以拟合更加复杂的情况。

常见的激活函数

数学公式自行查找

ReLU

import torch
import matplotlib.pyplot as plt
x=torch.arange(-10.0,10.0,0.1,requires_grad=True)
x

在这里插入图片描述

y=torch.relu(x)
y

在这里插入图片描述

可视化图像
plt.plot(x.detach(),y.detach())

在这里插入图片描述

可视化梯度
y.backward(torch.ones_like(x),retain_graph=True)
plt.clf()
plt.plot(x.detach(),x.grad)

在这里插入图片描述

sigmoid

y=torch.sigmoid(x)
y

在这里插入图片描述

可视化图像
plt.clf()
plt.plot(x.detach(),y.detach())

在这里插入图片描述

可视化梯度
x.grad.data.zero_()
y.backward(torch.ones_like(x),retain_graph=True)
plt.clf()
plt.plot(x.detach(),x.grad)

在这里插入图片描述

tanh

y=torch.tanh(x)
y

在这里插入图片描述

可视化图像
plt.clf()
plt.plot(x.detach(),y.detach())

在这里插入图片描述

可视化梯度
x.grad.data.zero_()
y.backward(torch.ones_like(x),retain_graph=True)
plt.clf()
plt.plot(x.detach(),x.grad)

在这里插入图片描述

读取数据

我上一篇博客写过,点击直接跳转

模型(ReLU激活+权重衰减+dropout)

上一篇博客train和test的acc中间就有1%的差距,似乎是有一点过拟合,在添加ReLU激活函数将线性模型变成多层感知机之后(整体准确率提高1%)会发现两者之间仍然有差距,所以我尝试了权重衰减和dropout。

权重衰减0.01会发现两者差距几乎没有,但是train的acc降低了1%,几乎跟线性效果差不多了,所以不可取。

dropout0.3会发现两者几乎重合,train下降很少,同时test也有所上升。

from torch import nn
net=nn.Sequential(nn.Flatten(),
                  nn.Linear(28*28,256),
                  nn.ReLU(),
                  nn.Dropout(0.3),
                  nn.Linear(256,8))
def init_weight(m):
    if type(m)==nn.Linear:
        nn.init.normal_(m.weight,std=0.01)
net.apply(init_weight)

在这里插入图片描述
权重衰退就是在优化器传参的时候对Linear()的参数进行限制。

wd=0.00 #权重衰减的值
loss_fn=nn.CrossEntropyLoss()
optimer=torch.optim.SGD([{"params": net[1].weight,"weight_decay": wd},{"params": net[1].bias},{"params": net[4].weight,"weight_decay": wd},{"params": net[4].bias}],lr=0.1)
epochs_num=10
train_len=len(train_iter.dataset)
all_acc=[]
all_loss=[]
test_all_acc=[]
for epoch in range(epochs_num):
    acc=0
    loss=0
    for x,y in train_iter:
        hat_y=net(x)
        l=loss_fn(hat_y,y)
        loss+=l
        optimer.zero_grad()
        l.backward()
        optimer.step()
        acc+=(hat_y.argmax(1)==y).sum()
    all_acc.append(acc/train_len)
    all_loss.append(loss.detach().numpy())
    test_acc=0
    test_len=len(test_iter.dataset)
    with torch.no_grad():
        for x,y in test_iter:
            hat_y=net(x)
            test_acc+=(hat_y.argmax(1)==y).sum()
    test_all_acc.append(test_acc/test_len)
    print(f'{epoch}的test的acc{test_acc/test_len}')

在这里插入图片描述

可视化

import matplotlib.pyplot as plt

损失函数可视化

plt.plot(range(1,epochs_num+1),all_loss,'.-',label='train_loss')
plt.text(epochs_num, all_loss[-1], f'{all_loss[-1]:.4f}', fontsize=12, verticalalignment='bottom')

在这里插入图片描述

准确率可视化

plt.plot(range(1,epochs_num+1),all_acc,'-',label='train_acc')
plt.text(epochs_num, all_acc[-1], f'{all_acc[-1]:.4f}', fontsize=12, verticalalignment='bottom')
plt.plot(range(1,epochs_num+1),test_all_acc,'-.',label='test_acc')
plt.legend()

在这里插入图片描述

预测结果

with torch.no_grad():
    all_num=5
    index=1
    plt.figure(figsize=(12,5))
    for i,label in zip(test_data_path,test_labels):
        if index<=all_num:
            img=cv2.imread(i)
            input_img=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
            img=cv2.cvtColor(input_img,cv2.COLOR_BGR2RGB)
            input_img=cv2.resize(input_img,size) 
            input_img=transforms.ToTensor()(input_img)
            result=net(input_img).argmax(1)
            plt.subplot(1,all_num,index)
            plt.imshow(img)
            plt.title(f'true{label},predict{result.detach().numpy()}')
            plt.axis("off")
            index+=1

在这里插入图片描述

更多推荐