卷积神经网络(多分类问题 pytorch)

# 手写数字识别  神经网络处理  高级处理
import torch
import torch.nn as nn
# 数据集处理
from torchvision import transforms
from torchvision import datasets
from torch.utils.data import DataLoader
# 函数  激活函数等
import torch.nn.functional as F
# 优化器包
import torch.optim as optim
# 分批
batch_size = 64

# 1. 数据处理
transform = transforms.Compose([
    transforms.ToTensor(),
    transforms.Normalize((0.1307, ), (0.3081, ))
])

train_dataset = datasets.MNIST(root='../dataset/mnist/',
                            train=True,
                            download=True,
                            transform=transform)
test_dataset = datasets.MNIST(root='../dataset/mnist/',
                              train=False,
                              download=True,
                              transform=transform)
train_loader = DataLoader(test_dataset,
                         shuffle=True,
                         batch_size=batch_size)

test_loader = DataLoader(test_dataset,
                         shuffle=False,
                         batch_size=batch_size)

# 数据为1 * 28 * 28
# 2. 建立模型
class InceptionA(nn.Module):
    def __init__(self, in_channels):
        super(InceptionA, self).__init__()
        '''初始化'''
        """初始化"""
        # 池化分支
        self.branch_pool = nn.Conv2d(in_channels, 24, kernel_size=1)
        # 1 * 1 分支
        self.branch1x1 = nn.Conv2d(in_channels, 16, kernel_size=1)
        # 5 * 5 分支
        self.branch5x5_1 = nn.Conv2d(in_channels, 16, kernel_size=1)
        self.branch5x5_2 = nn.Conv2d(16, 24, kernel_size=5, padding=2)
        # 3 * 3分支
        self.branch3x3_1 = nn.Conv2d(in_channels, 16, kernel_size=1)
        self.branch3x3_2 = nn.Conv2d(16, 24, kernel_size=3, padding=1)
        self.branch3x3_3 = nn.Conv2d(24, 24, kernel_size=3, padding=1)


    def forward(self, x):
        branch_pool = F.avg_pool2d(x,
                                   kernel_size=3,
                                   stride=1,
                                   padding=1)
        branch_pool = self.branch_pool(branch_pool)

        branch1x1 = self.branch1x1(x)

        branch5x5 = self.branch5x5_1(x)
        branch5x5 = self.branch5x5_2(branch5x5)

        branch3x3 = self.branch3x3_1(x)
        branch3x3 = self.branch3x3_2(branch3x3)
        branch3x3 = self.branch3x3_3(branch3x3)

        outputs = [branch1x1, branch5x5, branch3x3, branch_pool]
        # dim 1纬度
        return torch.cat(outputs, dim=1)

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = torch.nn.Conv2d(1, 10, kernel_size=5)
        self.conv2 = torch.nn.Conv2d(88, 20, kernel_size=5)

        self.incep1 = InceptionA(in_channels=10)
        self.incep2 = InceptionA(in_channels=20)

        self.mp = nn.MaxPool2d(2)
        self.fc = nn.Linear(1408, 10)

    def forward(self, x):
        in_size = x.size(0)
        x = F.relu(self.mp(self.conv1(x)))
        x = self.incep1(x)
        x = F.relu(self.mp(self.conv2(x)))
        x = self.incep2(x)
        x = x.view(in_size, -1)
        x = self.fc(x)
        return x

model = Net()


# 3.损失函数和优化器  交叉熵损失
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr=0.01, momentum=0.5)

# 4.循环训练
def train(epoch):
    running_loss = 0.0
    for batch_idx, data in enumerate(train_loader):
        inputs, target = data
        optimizer.zero_grad()

        outputs = model(inputs)
        loss = criterion(outputs, target)
        loss.backward()
        optimizer.step()

        running_loss += loss.item()
        if batch_idx % 300 == 0:
            print('[%d,%d] loss: %.10f' % (epoch+1, batch_idx+1, running_loss / 300))
            running_loss = 0.0

# 测试验证
def test():
    correct = 0
    total = 0
    with torch.no_grad(): # 不会再进行梯度
        for data in  test_loader:
            images, labels = data
            outputs = model(images)
            _, predicted = torch.max(outputs.data, dim=1)
            total+=labels.size(0)
            correct+=(predicted==labels).sum().item()
    print("Accuracy on test set: %d %%" % (100 * correct / total))


# 程序入口处
if __name__ == '__main__':
    for epoch in range(10):
        train(epoch)
        test()
    print("训练结束...")

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值