SoftMax回归
![](https://i-blog.csdnimg.cn/direct/7975ef2d35504e0da0431167b1bd24bf.png)
分类的数据集
![](https://i-blog.csdnimg.cn/direct/a6816824d625447cb83937994f580a1e.png)
![](https://i-blog.csdnimg.cn/direct/d9d1a80711a34520ae3b458c53370b78.png)
![](https://i-blog.csdnimg.cn/direct/1c068c007a66405e8493f9a7cec8f466.png)
![](https://i-blog.csdnimg.cn/direct/c74bcc7d19a743069f4e45546c20685f.png)
分类和回归的区别
![](https://i-blog.csdnimg.cn/direct/a808945453a84f90ba71a8db222128b8.png)
从回归到多类分类-均方损失
![](https://i-blog.csdnimg.cn/direct/2fa8c3e3072c4e23a08de5666aefac33.png)
![](https://i-blog.csdnimg.cn/direct/401f77f94d9142979b9412c7ee4e9aba.png)
![](https://i-blog.csdnimg.cn/direct/e0e933cd67aa4c76bf3f5cfb9dbf67b4.png)
![](https://i-blog.csdnimg.cn/direct/2261c7e838d741468cadc3aa4b928b2b.png)
softmax损失函数推导
![](https://i-blog.csdnimg.cn/direct/70669129f26740f3a642a2f3953e8b50.png)
![](https://i-blog.csdnimg.cn/direct/4f498601c3444d5a8351c6e21320c994.png)
损失函数
![](https://i-blog.csdnimg.cn/direct/46548dfb54a040ae9906e654c9dc9f9d.png)
![](https://i-blog.csdnimg.cn/direct/8c28ad5333f241c38f2e67b2161faf2b.png)
![](https://i-blog.csdnimg.cn/direct/05005c504263451bad731a3844fb6387.png)
![](https://i-blog.csdnimg.cn/direct/75acda0cbd6741dca28f5a5988129e4a.png)
图片分类数据集Fashion-MNIST
import torch
import torchvision
from torch.utils import data
from torchvision import transforms
from d2l import torch as d2l
d2l.use_svg_display()
"""
通过框架中的内置函数将Fashion-MNIST数据集进行下载
通过ToTrnsor实例将图像数据从PIL类型变换成为32浮点数据格式
并除以255使得所有像素点位于0-1
"""
trans = transforms.ToTensor()
mnist_train = torchvision.datasets.FashionMNIST(root='./data', train=True, transform=trans, download=True)
miist_test = torchvision.datasets.FashionMNIST(root='./data', train=False, transform=trans, download=True)
print(len(mnist_train), len(mnist_train))
print(mnist_train[0][0].shape)
def get_fashion_mnist_labels(labels):
"""
返回Fashion-MNIST数据集的文本标签
:param labels:
:return:
"""
text_labels = ['t-shirt', 'trousers', 'pullover', 'dress', 'coat', 'sandals', 'shirt', 'sneaker', 'bag',
'ankle boot']
return [text_labels[int(i)] for i in labels]
def show_images(imgs, num_rows, num_cols, title=None, scale=1.5):
"""
Plot a list of images.
:param img:
:param num_rows:
:param num_cols:
:param title:
:param scale:
:return:
"""
figsize = (num_cols * scale, num_rows * scale)
_, axes = d2l.plt.subplots(num_rows, num_cols, figsize=figsize)
axes = axes.flatten()
for i, (ax, img) in enumerate(zip(axes, imgs)):
if torch.is_tensor(img):
# 图片张量
ax.imshow(img.numpy())
else:
ax.imshow(img)
X, y = next(iter(data.DataLoader(mnist_train, batch_size=18)))
show_images(X.reshape(18, 28, 28), 2, 9, title=get_fashion_mnist_labels(y))
d2l.plt.show()
batch_size = 256
def get_dataloader_workers():
"""使用四个进程来读取数据"""
return 4
train_iter = data.DataLoader(mnist_train, batch_size=batch_size, shuffle=True, num_workers=get_dataloader_workers())
timer = d2l.Timer()
for X, y in train_iter:
continue
print(f'{timer.stop():.2f} sec')
def load_data_fashion_mnist(batch_size, resize=None):
"""下载Fashion-MNIST数据集,然后将其加载到内存中"""
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=True)
mnist_test = torchvision.datasets.FashionMNIST(root='./data', train=False, transform=trans, download=True)
return ((data.DataLoader(mnist_train, batch_size=batch_size, shuffle=True, num_workers=get_dataloader_workers())),data.DataLoader(mnist_test, batch_size=batch_size, shuffle=True, num_workers=get_dataloader_workers()))