参数初始化也很重要
学习了zip,enumerate用法
pytorch的广播机制
import torch
import torchvision
from torch.utils import data
from torchvision import transforms
from utils import *
W = torch.normal(0, 1, (784, 10), requires_grad=True)
b = torch.zeros(10, requires_grad=True)
batch_size=256
def load_data(batch_size, resize=None):
trans = [transforms.ToTensor()]
if resize:
trans.insert(0, transforms.Resize(resize))
trans = transforms.Compose(trans)
mnist_train = torchvision.datasets.FashionMNIST(
root="../data", train=True, transform=trans, download=False)
mnist_test = torchvision.datasets.FashionMNIST(
root="../data", train=False, transform=trans, download=False)
return data.DataLoader(mnist_train, batch_size,shuffle=True), data.DataLoader(mnist_test, batch_size,shuffle=False)
train_iter,test_iter = load_data(batch_size)
def net(x=torch.tensor(0)):
'''
:param x:
:return y_hat:
'''
y_hat = torch.exp(torch.matmul(x.reshape((-1,W.shape[0])), W) + b) # flatten X
y_hat = y_hat / y_hat.sum(1, keepdim=True) # use broadcast
return y_hat
def loss(y_hat=torch.tensor(0), y=torch.tensor(0)):
'''
:param y_hat:prediction
:param y: reality
:return: the difference between prediction and reality
why is it a vector?
'''
return -torch.log(y_hat[range(len(y_hat)), y])
def accuracy(y_hat=torch.tensor(0), y=torch.tensor(0)):
'''
:param y_hat:
:param y:
:return: accurate number
'''
y_hat = torch.argmax(y_hat, 1) # 得出y_hat所对应的类
t = y_hat.type(y.dtype) == y
return float(t.type(y.dtype).sum())
def evaluate_accuracy(net,data_iter):
'''
:param data_iter:
:return:precision
'''
metric = Accumulator(2)
for X,y in data_iter:
metric.add(accuracy(net(X),y),y.numel())
return (metric[0])/(metric[1])
def train_epoch(net,train_iter,loss,updater):
metric = Accumulator(3)
for X,y in train_iter:
y_hat=net(X)
l = loss(y_hat,y)
l.sum().backward()
updater(X.shape[0],[W,b])
metric.add(float(l.sum()), accuracy(y_hat, y), y.numel())
return metric[0] / metric[2], metric[1] / metric[2]
def train_ch3(net, train_iter, test_iter, loss, num_epochs, updater): #@save
animator = Animator(xlabel='epoch', xlim=[1, num_epochs], ylim=[0.3, 0.9],
legend=['train loss', 'train acc', 'test acc'])
for epoch in range(num_epochs):
train_metrics = train_epoch(net, train_iter, loss, updater)
test_acc = evaluate_accuracy(net, test_iter)
animator.add(epoch + 1, train_metrics + (test_acc,))
train_loss, train_acc = train_metrics
assert train_loss < 0.5, train_loss
assert train_acc <= 1 and train_acc > 0.7, train_acc
assert test_acc <= 1 and test_acc > 0.7, test_acc
num_epochs = 10
train_ch3(net, train_iter, test_iter, loss, num_epochs, updater)
import torch
from IPython import display
from d2l import torch as d2l
class Accumulator:
'''
'''
def __init__(self,n):
self.data = [0]*n
def add(self,*args):
self.data=[a+float(b) for a,b in zip(self.data,args)]
def __getitem__(self, item):
return self.data[item]
def sgd(params,lr,batch_size):
with torch.no_grad():
for param in params:
param-=lr*param.grad/batch_size
param.grad.zero_()
def updater(batch_size,params):
return sgd(params,0.1,batch_size)
class Animator: #@save
"""在动画中绘制数据。"""
def __init__(self, xlabel=None, ylabel=None, legend=None, xlim=None,
ylim=None, xscale='linear', yscale='linear',
fmts=('-', 'm--', 'g-.', 'r:'), nrows=1, ncols=1,
figsize=(3.5, 2.5)):
# 增量地绘制多条线
if legend is None:
legend = []
d2l.use_svg_display()
self.fig, self.axes = d2l.plt.subplots(nrows, ncols, figsize=figsize)
if nrows * ncols == 1:
self.axes = [self.axes, ]
# 使用lambda函数捕获参数
self.config_axes = lambda: d2l.set_axes(
self.axes[0], xlabel, ylabel, xlim, ylim, xscale, yscale, legend)
self.X, self.Y, self.fmts = None, None, fmts
def add(self, x, y):
# 向图表中添加多个数据点
if not hasattr(y, "__len__"):
y = [y]
n = len(y)
if not hasattr(x, "__len__"):
x = [x] * n
if not self.X:
self.X = [[] for _ in range(n)]
if not self.Y:
self.Y = [[] for _ in range(n)]
for i, (a, b) in enumerate(zip(x, y)):
if a is not None and b is not None:
self.X[i].append(a)
self.Y[i].append(b)
self.axes[0].cla()
for x, y, fmt in zip(self.X, self.Y, self.fmts):
self.axes[0].plot(x, y, fmt)
self.config_axes()
display.display(self.fig)
display.clear_output(wait=True)```