前馈神经网络多分类任务

pytorch深度学习的套路都差不多,多看多想多写多测试,自然就会了。主要的技术还是在于背后的数学思想和数学逻辑。

废话不多说,上代码自己看。

python 复制代码
import torch
import numpy as np
import torch.nn as nn
import torchvision
import torchvision.transforms as transforms

class Network(nn.Module):
    def __init__(self ,input_dim ,hidden_dim ,out_dim):
        super().__init__()
        self.layer1 = nn.Sequential(  # 全连接层     [1, 28, 28]
            nn.Linear(784, 400),       # 输入维度,输出维度
            nn.BatchNorm1d(400),  # 批标准化,加快收敛,可不需要
            nn.ReLU()  				 # 激活函数
        )

        self.layer2 = nn.Sequential(
            nn.Linear(400, 200),
            nn.BatchNorm1d(200),
            nn.ReLU()
        )

        self.layer3 = nn.Sequential(   # 全连接层
            nn.Linear(200, 100),
            nn.BatchNorm1d(100),
            nn.ReLU()
        )

        self.layer4 = nn.Sequential(   # 最后一层为实际输出,不需要激活函数,因为有 10 个数字,所以输出维度为 10,表示10 类
            nn.Linear(100, 10),
        )

    def forward(self ,x):
        x = self.layer1(x)
        x = self.layer2(x)
        x = self.layer3(x)
        output = self.layer4(x)
        return output
def get_num_correct(preds, labels):
    return (preds.argmax(dim=1) == labels).sum().item()

def dropout(x, keep_prob = 0.5):
    '''
    np.random.binomial 当输入二维数组时,按行按列(每个维度)都是按照给定概率生成1的个数,
比如 输入 10 * 6的矩阵,按照0.5的概率生成1 那么每列都大概会有5个1,每行大概会有3个1,
其实就不用考虑按行drop或者按列drop,相当于每行生成的mask都是不一样的,那么矩阵中每行的元素(代表一层中的神经元)都是按照不同的mask失活的
当矩阵形状改变行列代表的意义不一样时,由于每行每列(各个维度)的1的个数都是按照prob留存的,因此对结果没有影响。
    '''
    mask = torch.from_numpy(np.random.binomial(1,keep_prob,x.shape))
    return x * mask / keep_prob
    

if __name__ == "__main__":
    train_set = torchvision.datasets.MNIST(
        root='./data'
        , train=True
        , download=False
        , transform=transforms.Compose([
            transforms.ToTensor()
        ])
    )
    test_set = torchvision.datasets.MNIST(
        root='./data',
        train=False,
        download=False,
        transform=transforms.Compose([
            transforms.ToTensor()])
    )

    train_loader = torch.utils.data.DataLoader(train_set
                                               , batch_size=512
                                               , shuffle=True
                                               )
    test_loader = torch.utils.data.DataLoader(test_set
                                              , batch_size=512
                                              , shuffle=True)

    net = Network(28 * 28, 256, 10)
    optimizer = torch.optim.SGD(net.parameters(), lr=0.01)
    criterion = nn.CrossEntropyLoss()
    epoch = 10

    for i in range(epoch):
        train_accur = 0.0
        train_loss = 0.0
        for batch in train_loader:
            images, labels = batch
            #images, labels = images.to(device), labels.to(device)
            images = images.squeeze(1).reshape(images.shape[0], -1)
            preds = net(images)
            optimizer.zero_grad()
            loss = criterion(preds, labels)
            loss.backward()
            optimizer.step()
            train_loss += loss.item()
            train_accur += get_num_correct(preds, labels)
        print("loss :" + str(train_loss) + "train accur:" + str(train_accur * 1.0 / 60000))

    global correct
    with torch.no_grad():
        correct = 0
        for batch in test_loader:
            images, labels = batch
            #images, labels = images.to(device), labels.to(device)
            images = images.squeeze(1).reshape(-1, 784)
            preds = net(images)

            preds = preds.argmax(dim=1)
            correct += (preds == labels).sum()
            print(correct)
    print(correct.item() * 1.0 / len(test_set))
相关推荐
BullSmall17 小时前
汽车HIL测试:电子开发的关键验证环节
人工智能·机器学习·自动驾驶
woshihonghonga17 小时前
停止Conda开机自动运行方法
linux·人工智能·conda
海洲探索-Hydrovo19 小时前
TTP Aether X 天通透传模块丨国产自主可控大数据双向通讯定位模组
网络·人工智能·科技·算法·信息与通信
触想工业平板电脑一体机19 小时前
【触想智能】工业安卓一体机在人工智能领域上的市场应用分析
android·人工智能·智能电视
墨染天姬20 小时前
【AI】数学基础之矩阵
人工智能·线性代数·矩阵
2401_841495641 天前
【计算机视觉】基于复杂环境下的车牌识别
人工智能·python·算法·计算机视觉·去噪·车牌识别·字符识别
倔强青铜三1 天前
苦练Python第66天:文件操作终极武器!shutil模块完全指南
人工智能·python·面试
倔强青铜三1 天前
苦练Python第65天:CPU密集型任务救星!多进程multiprocessing模块实战解析,攻破GIL限制!
人工智能·python·面试
强哥之神1 天前
浅谈目前主流的LLM软件技术栈:Kubernetes + Ray + PyTorch + vLLM 的协同架构
人工智能·语言模型·自然语言处理·transformer·openai·ray
zskj_qcxjqr1 天前
七彩喜艾灸机器人:当千年中医智慧遇上现代科技
大数据·人工智能·科技·机器人