import torch
from IPython import display
from d2l import torch as d2l
batch_size=256
#定义训练和验证数据集
train_iter,test_iter=d2l.load_data_fashion_mnist(batch_size)
#参数初始化,把输入图片看成长度784的向量,这个数据集有十个类别,输出为10
num_inputs=784
num_outputs=10
w=torch.normal(0,0.01,size=(num_inputs,num_outputs),requires_grad=True)
b=torch.zeros(num_outputs,requires_grad=True)
#实现softmax函数
def softmax(X):
X_exp=torch.exp(X)
partition=X_exp.sum(1,keepdim=True)#保持输出维度,使它还是一个矩阵,0是按列求和,1是按行求和
#实现softmax回归模型
def net(X):
return softmax(torch.matmul(X.reshape(-1,w.shape[0]),w)+b)#matmul是矩阵乘法
#实现交叉熵损失函数
def cross_entropy(y_hat,y):#公式是-y*log(y_hat)
return -torch.log(y_hat[range(len(y_hat)),y])#log是以e为底的对数,根据前面推的公式,【】内是取出对应元素值
#将预测类别与真实类别比较,这里开始进入测试部分
def accuracy(y_hat,y):
if len(y_hat.shape)>1 and y_hat.shape[1]>1:
y_hat=y_hat.argmax(axis=1)#选出每行中最大的,也就是分类的类别
cmp=y_hat.type(y.dtype)==y
return float(cmp.type(y.dtype).sum())
#按照accuracy的思路,可以写出模型结果准确率计算函数,分子分母不断累加正确的个数和总的个数
def evaluate_accuracy(data_iter, net):
acc_sum, n = 0.0, 0
for X, y in data_iter:
acc_sum += (net(X).argmax(dim=1) == y).float().sum().item()#item将tensor类型转为数据类型
n += y.shape[0]
return acc_sum / n
#softmax训练过程
def train_ch3(net, train_iter, test_iter, loss, num_epochs,batch_size,params=None, lr=None, optimizer=None):#num_epochs训练次数,lr学习率
for epoch in range(num_epochs):
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 None:
d2l.sgd(params, lr, batch_size)
else:
optimizer.step()
train_l_sum += l.item()
train_acc_sum += (y_hat.argmax(dim=1) ==y).sum().item()
n += y.shape[0]
test_acc = evaluate_accuracy(test_iter, net)
print('epoch %d, loss %.4f, train acc %.3f, test acc %.3f'% (epoch + 1, train_l_sum / n, train_acc_sum / n,test_acc))
总结一下:写一个深度学习算法的底层就是写它的模型、损失函数和评价函数
最终输出的结果:
epoch 1, loss 0.7878, train acc 0.749, test acc 0.794
epoch 2, loss 0.5702, train acc 0.814, test acc 0.813
epoch 3, loss 0.5252, train acc 0.827, test acc 0.819
epoch 4, loss 0.5010, train acc 0.833, test acc 0.824
epoch 5, loss 0.4858, train acc 0.836, test acc 0.815