欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Pytorch 学习(四):Pytorch 实现 Softmax 回归

程序员文章站 2022-05-26 20:42:20
...

Pytorch 实现 Softmax 回归

本方法参考自《动手学深度学习》(Pytorch版)github项目

一般步骤

  • 构建数据集和以 batch 为单位的数据读取
  • 构建模型及其初始化、损失函数、下降方法
  • 训练网络及评估

方法一:造*法

从零搭建 Softmax 回归有三个关键点

  1. Softmax 方法实现
  2. 交叉熵函数实现
  3. 精度评估实现

Softmax 方法实现

def softmax(x):
  x_exp = x.exp()  # m * n
  partition = x_exp.sum(dim=1, keepdim=True)  # 按列累加, m * 1
  return x_exp / partition  # 广播机制, [m * n] / [m * 1] = [m * n]

交叉熵实现

  • y_hat 为网络输出,size 是 batch * c,其中 c 为类别数,每一行表示一个图片的 softmax 分类概率,一共 batch_size 行。
  • y 为真值,size 是 batch * 1,1 为该图片所属类。
  • gather 函数将 y_hat 每一行(dim=1)的 y 值处概率取出来
def cross_entropy(y_hat, y):
  return - torch.log(y_hat.gather(1, y.view(-1, 1)))

精度评估

def accuracy(y_hat, y):
  return (y_hat.argmax(dim=1) == y).float().mean().item()

完整代码

import torch
import torchvision
import numpy as np
import d2lzh_pytorch as d2l

batch_size = 256
# 数据获取
print('load fashion-mnist start...')
train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size)

# 模型参数初始化
num_inputs = 28 * 28  # 784
num_outputs = 10

w = torch.tensor(np.random.normal(0, 0.01, (num_inputs, num_outputs)), dtype=torch.float32)
b = torch.zeros(num_outputs, dtype=torch.float32)

w.requires_grad_(True)
b.requires_grad_(True)

# 定义模型
def softmax(X):
    X_exp = X.exp()
    partition = X_exp.sum(dim=1, keepdim=True)
    return X_exp / partition

def net(X):
    return softmax(torch.mm(X.view(-1, num_inputs), w) + b)

# 定义损失函数
def cross_entropy(y_hat, y):
    return - torch.log(y_hat.gather(1, y.view(-1, 1)))

# 定义精度评估函数
def accuracy(y_hat, y):
    return (y_hat.argmax(dim=1) == y).float().mean().item()

def evaluate_accuracy(data_iter, net):
    acc_sum, n = 0.0, 0
    for X, y in data_iter:
        y_hat = net(X)
        acc = (y_hat.argmax(dim=1) == y).float().sum().item()
        acc_sum += acc
        n += y.shape[0]
    return acc_sum / n

# 训练模型
num_epochs = 5
lr = 0.1

def train_ch3(net, train_iter, test_iter, loss, num_epochs, batch_size, params=None, lr=None, optimizer=None):
    for epo in range(1, num_epochs + 1):
        train_l_sum, train_acc_sum, n = 0.0, 0.0, 0
        for X, y in train_iter:
            y_hat = net(X)
            l = loss(y_hat, y).sum()

            # 梯度清零
            if optimizer is not None:
                optimizer.zero_grad()
            elif params is not None and params[0].grad is not None:
                for param in params:
                    param.grad.data.zero_()

            l.backward()
            # 梯度下降
            if optimizer is not None:
                optimizer.step()
            else:
                d2l.sgd(params, lr, batch_size)

            train_l_sum += l.item()
            train_acc_sum += (y_hat.argmax(dim=1) == y).float().sum().item()
            n += y.shape[0]
        test_acc = evaluate_accuracy(test_iter, net)
        print('epoch %d, loss %.4f, train acc %.3f, test acc %.3f'
              % (epo, train_l_sum / n, train_acc_sum / n, test_acc))

train_ch3(net, train_iter, test_iter, cross_entropy, num_epochs, batch_size, [w, b], lr)

方法二:我是调包侠

import torch
import torch.nn as nn
import torch.nn.init as init
import d2lzh_pytorch as d2l
from collections import OrderedDict
import torch.optim as optim

# 构建数据
batch_size = 256
train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size)

# 构建网络
class FlattenLayer(nn.Module):
    def __init__(self):
        super(FlattenLayer, self).__init__()

    def forward(self, x):  # transform (batch, c, h, w) to (batch, c * h * w)
        return x.view(x.shape[0], -1)

class LinearNet(nn.Module):
    def __init__(self, n_input, n_output):
        super(LinearNet, self).__init__()
        self.flatten = FlattenLayer()
        self.linear = nn.Linear(n_input, n_output)

    def forward(self, x):
        return self.linear(self.flatten(x))

num_features = 28 * 28  # 784
num_labels = 10
net = LinearNet(num_features, num_labels)
# 等价于用 Sequential 创建网络
net2 = nn.Sequential(
    OrderedDict([
        ('flatten', FlattenLayer()),
        ('linear', nn.Linear(num_features, num_labels))
    ])
)
print(net)
print(net2)

# 参数初始化
init.normal_(net.linear.weight, mean=0, std=0.01)
init.constant_(net.linear.bias, val=0)

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

# 下降器
optimizer = optim.SGD(net.parameters(), lr=0.1)

# 模型训练
num_epochs = 5
d2l.train_ch3(net, train_iter, test_iter, loss, num_epochs, batch_size, optimizer=optimizer)