PyTorch实现Softmax回归

1 导入模块

import torch
from torch.utils.data import DataLoader
import torch.nn as nn
import torchvision.datasets as Dataset
import torchvision.transforms as transforms
import sys

2 加载数据 FashionMNIST

# 批量加载数据
def data_iter(mnist_train, mnist_test, batch_szie, num_workers=0):
    if sys.platform.startswith('win'):
        num_workers = 0
    else:
        num_workers = 4

    train_iter = DataLoader(mnist_train, batch_size=batch_szie, shuffle=True, num_workers=num_workers)
    test_iter = DataLoader(mnist_test, batch_size=batch_szie, shuffle=False, num_workers=num_workers)
    return train_iter, test_iter

# 加载数据,FashionMNIST
#训练数据
mnist_train = Dataset.FashionMNIST('./datasets/FashionMNIST', train=True, transform=transforms.ToTensor(),
                                   download=True)

# 测试数据
mnist_test = Dataset.FashionMNIST('./datasets/FashionMNIST', train=False, transform=transforms.ToTensor(),
                                  download=True)

batch_size = 256
train_iter, test_iter = data_iter(mnist_train, mnist_test, batch_size, num_workers=0)

3 定义模型

# 定义模型, 全连接层
class Net(nn.Module):
    def __init__(self, num_inputs, num_outputs):
        super(Net, self).__init__()
        self.linear = nn.Linear(num_inputs, num_outputs)

    def forward(self, x):
        return self.linear(x.view(x.shape[0], -1))


num_inputs = 28 * 28
num_outputs = 10

# 定义模型
net = Net(num_inputs, num_outputs)

4初始化模型参数

# 初始化模型参数
torch.nn.init.normal_(net.linear.weight, mean=0, std=0.01)
torch.nn.init.zeros_(net.linear.bias)

5定义损失函数和优化器

# 定义损失函数
loss = nn.CrossEntropyLoss()

# 定义优化器
optimizer = torch.optim.SGD(net.parameters(), lr=0.1)

6定义模型的评估方式

# 定义评估方式
def evaluate(data_iter, net):
    acc_sum = 0.0
    n = 0
    for X, y in data_iter:
        acc_sum += (net(X).argmax(dim=1) == y).float().sum().item()
        n += y.shape[0]
    return acc_sum / n

7训练模型的函数

# 定义训练函数
def train(net, train_iter, test_iter, loss, num_epochs, batch_size, lr, optimizer):
    train_l_sum = 0.0
    train_acc_sum = 0.0
    n = 0
    for epoch in range(1, num_epochs + 1):
        for X, y in train_iter:
            y_hat = net(X) # 预测结果
            l = loss(y_hat, y).sum() # 计算损失
            optimizer.zero_grad() # 梯度清0
            l.backward() # 反向传播
            optimizer.step() 
            train_l_sum += l.item()
            train_acc_sum += (y_hat.argmax(dim=1) == y).float().sum().item()
            n += y.shape[0]

        test_acc = evaluate(test_iter, net)
        print('epoch %d, loss= %.4f, train_acc=%.4f, test_acc=%.4f' % (
        epoch, train_l_sum / n, train_acc_sum / n, test_acc))

8训练模型

num_epochs = 10
lr = 0.1
train(net, train_iter, test_iter, loss, num_epochs, batch_size, lr, optimizer)

输出

PyTorch实现Softmax回归_第1张图片

10 完整代码

import torch
from torch.utils.data import DataLoader
import torch.nn as nn
import torchvision.datasets as Dataset
import torchvision.transforms as transforms
import sys

# 加载数据,FashionMNIST
mnist_train = Dataset.FashionMNIST('./datasets/FashionMNIST', train=True, transform=transforms.ToTensor(),
                                   download=True)

mnist_test = Dataset.FashionMNIST('./datasets/FashionMNIST', train=False, transform=transforms.ToTensor(),
                                  download=True)


def data_iter(mnist_train, mnist_test, batch_szie, num_workers=0):
    if sys.platform.startswith('win'):
        num_workers = 0
    else:
        num_workers = 4

    train_iter = DataLoader(mnist_train, batch_size=batch_szie, shuffle=True, num_workers=num_workers)
    test_iter = DataLoader(mnist_test, batch_size=batch_szie, shuffle=False, num_workers=num_workers)

    return train_iter, test_iter


batch_size = 256
train_iter, test_iter = data_iter(mnist_train, mnist_test, batch_size, num_workers=0)


class Net(nn.Module):
    def __init__(self, num_inputs, num_outputs):
        super(Net, self).__init__()
        self.linear = nn.Linear(num_inputs, num_outputs)

    def forward(self, x):
        return self.linear(x.view(x.shape[0], -1))


num_inputs = 28 * 28
num_outputs = 10

# 定义模型
net = Net(num_inputs, num_outputs)

# 初始化模型参数
torch.nn.init.normal_(net.linear.weight, mean=0, std=0.01)
torch.nn.init.zeros_(net.linear.bias)

# 定义损失函数
loss = nn.CrossEntropyLoss()

# 定义优化器
optimizer = torch.optim.SGD(net.parameters(), lr=0.1)
num_epochs = 10
lr = 0.1


# 定义评估方式
def evaluate(data_iter, net):
    acc_sum = 0.0
    n = 0
    for X, y in data_iter:
        acc_sum += (net(X).argmax(dim=1) == y).float().sum().item()
        n += y.shape[0]
    return acc_sum / n


# 定义训练函数
def train(net, train_iter, test_iter, loss, num_epochs, batch_size, lr, optimizer):
    train_l_sum = 0.0
    train_acc_sum = 0.0
    n = 0
    for epoch in range(1, num_epochs + 1):
        for X, y in train_iter:
            y_hat = net(X)
            l = loss(y_hat, y).sum()
            optimizer.zero_grad()
            l.backward()

            optimizer.step()
            train_l_sum += l.item()
            train_acc_sum += (y_hat.argmax(dim=1) == y).float().sum().item()
            n += y.shape[0]

        test_acc = evaluate(test_iter, net)
        print('epoch %d, loss= %.4f, train_acc=%.4f, test_acc=%.4f' % (
        epoch, train_l_sum / n, train_acc_sum / n, test_acc))


train(net, train_iter, test_iter, loss, num_epochs, batch_size, lr, optimizer)

你可能感兴趣的:(pytorch,回归,深度学习)