Commit b0a323a2 authored by hhy's avatar hhy
Browse files

封装softmax的训练函数,初步创建自定义'ml'包,便于后续调用

parent d047dcff
softmax.py
\ No newline at end of file
File added
File added
def aa():
print(1)
\ No newline at end of file
import torch
import torchvision # 计算机视觉库
from torch.utils import data
from torchvision import transforms
from torch.utils import data
from IPython import display
import matplotlib.pyplot as plt
import torchvision # 计算机视觉库
import numpy as np
import torch
import time
......@@ -48,6 +49,12 @@ class Timer:
return np.array(self.times).cumsum().tolist()
class Animator:
"""动画机:在动画中绘制数据"""
def get_dataloader_workers():
"""使用4个进程来读取数据"""
return 4
......@@ -60,6 +67,16 @@ def get_fashion_mnist_labels(labels):
return [text_labels[int(i)] for i in labels]
def set_axes(axes, xlabel, ylabel, xlim, ylim, xscale, yscale, legend):
"""set the axes for matplotlib"""
axes.set_xlabel(xlabel), axes.set_ylabel(ylabel)
axes.set_xlim(xlim), axes.set_ylim(ylim)
axes.set_xscale(xscale), axes.set_yscale(yscale)
if legend: # 添加图例
axes.legend(legend)
axes.grid() # 设置图片背景为网格形式
def show_images(imgs, num_rows, num_cols, titles=None, scale=1.5):
figsize = (num_cols * scale, num_rows * scale)
# 这里的 _ 表示忽略不使用的变量、即fig
......@@ -129,6 +146,38 @@ def evaluate_accuracy(net, data_iter):
return metric[0] / metric[1]
def train_epoch_ch3(net, train_iter, loss, updater, lr):
"""训练模型的一个迭代周期"""
if isinstance(net, torch.nn.Module):
net.train() # 将模型更新为训练模式
metric = Accumulator(3) # 累加器
for X, y in train_iter:
y_hat = net(X)
l = loss(y_hat, y)
if isinstance(updater, torch.optim.Optimizer):
# 使用pytorch内置的优化器和损失函数
updater.zero_grad()
l.mean().backward()
updater.step()
else:
l.sum().backward()
updater(X.shape[0], lr) # 传入batch_size
metric.add(float(l.sum()), accuracy(y_hat, y), y.numel())
# 返回训练损失和训练精度
return metric[0] / metric[2], metric[1] / metric[2]
def sgd(params, batch_size, lr):
with torch.no_grad():
for param in params:
param -= lr * param.grad / batch_size
param.grad.zero_()
def updater(batch_size, lr):
return sgd([W, b], batch_size, lr)
# trans = transforms.ToTensor() # 转化数据格式(PLT->32float)
# mnist_train = torchvision.datasets.FashionMNIST(
# root="../data", train=True, transform=trans, download=True
......@@ -196,24 +245,28 @@ b = torch.zeros(num_outputs, requires_grad=True)
print(evaluate_accuracy(net, test_iter))
# let's go!!!!
# # let's go!!!!
num_epochs = 20
lr = 0.1
for i in range(num_epochs):
if i > 9:
lr /= 10
for X, y in train_iter:
# print(X.shape, y.shape)
# show_images(X.reshape(256, 28, 28), 16, 16, titles=get_fashion_mnist_labels(y))
# plt.show()
l = cross_entropy(net(X), y)
l.sum().backward()
for j in [W, b]:
with torch.no_grad():
j -= j.grad * lr / batch_size
j.grad.zero_()
print(f"epoch{i+1}: loss = {l.mean():f}")
# for i in range(num_epochs):
# if i > 9:
# lr /= 10
# for X, y in train_iter:
# # print(X.shape, y.shape)
# # show_images(X.reshape(256, 28, 28), 16, 16, titles=get_fashion_mnist_labels(y))
# # plt.show()
# l = cross_entropy(net(X), y)
# l.sum().backward()
# for j in [W, b]:
# with torch.no_grad():
# j -= j.grad * lr / batch_size
# j.grad.zero_()
# print(f"epoch{i+1}: loss = {l.mean():f}")
for epoch in range(num_epochs):
loss, acc = train_epoch_ch3(net, train_iter, cross_entropy, updater, lr)
print(f"epoch{epoch+1}: loss={loss:.2f}, acc={acc:.2f}")
print(evaluate_accuracy(net, test_iter))
......@@ -226,4 +279,5 @@ for i in range(num_epochs):
plt.subplots_adjust(hspace=1)
plt.show()
# 综上,依然使用小批量随机梯度下降来优化算法sgd来优化模型,在学习率lr=0.1时收敛最快,准确度最高可以到0.83左右
# 综上,依然使用小批量随机梯度下降来优化算法sgd来优化模型,在学习率lr=0.1时收敛最快,准确度最高可以到0.85左右
from ml import torch as ml
print(ml.aa())
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment