10卷积神经网络(基础篇)

导入库

import torch
import numpy as np
from torch.utils.data import DataLoader
from torchvision import datasets
from torchvision import transforms
import matplotlib.pyplot as plt

例子1

# example 1
in_channels, out_channels = 5, 10
width, height = 100, 100
kernel_size = 3
batch_size = 1

input = torch.randn(batch_size, in_channels, width, height)

# 卷积层
Conv_Layer = torch.nn.Conv2d(in_channels, out_channels, kernel_size=kernel_size)  # out_channels个形状为in_channels*kernel_size*kernel_size的卷积核

output = Conv_Layer(input)

print(input.shape)
print(output.shape)
print(Conv_Layer.weight.shape)

例子2

# example 2
input = [3, 4, 6, 5, 7,
         2, 4, 6, 8, 2,
         1, 6, 7, 8, 4,
         9, 7, 4, 6, 2,
         3, 7, 5, 4, 1]
input = torch.tensor(input, dtype=torch.float32).view(1, 1, 5, 5)  # (batch_size, num_channels, height, width)

Conv_layer = torch.nn.Conv2d(1, 1, kernel_size=3, padding=1, bias=False)
kernel = torch.tensor([1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=torch.float32).view(1, 1, 3, 3)    # 自定义卷积核的权重  (out_channels, in_channels, kernel_height, kernel_width)
Conv_layer.weight.data = kernel.data   # 给卷积层赋值我们的权重

output = Conv_layer(input)
print(output)

例子3(MaxPooling)

input = [3, 4, 5, 6,
         2, 4, 6, 8,
         1, 6, 7, 8,
         9, 7, 4, 6]
input = torch.tensor(input, dtype=torch.float32).view(1, 1, 4, 4)

maxpooling_layer = torch.nn.MaxPool2d(kernel_size=2) # 默认stride也是2

output = maxpooling_layer(input)

print(output)

实现简单CNN

# 数据加载和读取
transform = transforms.Compose([transforms.ToTensor(),
                               transforms.Normalize((0.1307, ), (0.3081, ))])

tra_data = datasets.MNIST(root="./datasets/mnist", train=True, download=False, transform=transform)
test_data = datasets.MNIST(root="./datasets/mnist", train=False, download=False, transform=transform)

tra_loader = DataLoader(dataset=tra_data, batch_size=64, shuffle=True)
test_loader = DataLoader(dataset=test_data, batch_size=64, shuffle=False)

# 创建Classifier
class Net(torch.nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = torch.nn.Conv2d(1, 10, kernel_size=5)
        self.pooling = torch.nn.MaxPool2d(2)
        self.conv2 = torch.nn.Conv2d(10, 20, kernel_size=5)
        self.linear = torch.nn.Linear(320, 10)
        self.relu = torch.nn.ReLU()

    def forward(self, x):
        batch_size = x.size(0)   # 求batch_size,方便后面将矩阵展开成一维
        x = self.pooling(self.relu(self.conv1(x)))
        x = self.pooling(self.relu(self.conv2(x)))
        x = x.view(batch_size, -1)    # (batch, 20, 4, 4) -> (batch, 320)
        x = self.linear(x)
        return x

model = Net()
# 使用gpu
device = torch.device("cuda:0" if torch.cuda.is_available() else 'cpu')
model = model.to(device)

# loss和optimizer
criterion = torch.nn.CrossEntropyLoss()
criterion = criterion.to(device)
optimizer = torch.optim.SGD(model.parameters(), lr=0.01, momentum=0.5)

def train(epoch):
    running_loss = 0.0
    for i, (data) in enumerate(tra_loader):
        inputs, targets = data
        inputs, targets = inputs.to(device), targets.to(device)  # 使用gpu
        optimizer.zero_grad()

        # forward
        y_pred = model(inputs)
        l = criterion(y_pred, targets)
        # backward
        l.backward()
        # update
        optimizer.step()

        running_loss += l.item()

        # 每300 iteration 打印一次平均loss
        if i % 300 == 299:
            print("[%d %5d]\tloss: %3f" % (epoch+1, i+1, running_loss / 300))
            running_loss = 0.0

def test():
    total = 0
    correct = 0
    accuracy = 0.0
    with torch.no_grad():
        for data in test_loader:
            x, labels = data
            x, labels = x.to(device), labels.to(device)  # 使用gpu

            outputs = model(x)  # 预测

            total += labels.size(0)   # 总的样本数量
            _, predicted = torch.max(outputs.data, dim=1)  # 取每个样本的分类最大值的下标(即样本被预测为哪个类)
            correct += (predicted == labels).sum().item()   # 被预测正确的样本数量

        print("Accuracy on Test is: %3f %% [%d %d]" % (100 * correct / total, correct, total))
        accuracy = 100 * correct / total
        return accuracy


if __name__ == '__main__':
    acc_list = []
    for epoch in range(10):
        train(epoch)
        accuracy = test()
        acc_list.append(accuracy)

    # 画图
    acc_list = np.array(acc_list)
    plt.plot(range(10), acc_list)
    plt.xlabel('epoch')
    plt.ylabel('Accuracy')
    plt.show()
    plt.close()

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值