深度学习练习:利用softmax回归实现FashionMNIST数据集分类
从零开始
导包
import torch
from torchvision import transforms
from torch. utils. data import DataLoader
import torchvision
from torch. optim import SGD
from matplotlib import pyplot as plt
from torch import nn
定义一些超参数
batch_size = 256
resize = 64
num_inputs = 784
num_outputs = 10
lr = 0.01
epoches = 10
定义加载数据集函数
def load_data_fashion_mnist ( 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= "./dataset" , train= True , transform= trans, download= True )
mnist_test = torchvision. datasets. FashionMNIST( root= "./dataset" , train= False , transform= trans, download= True )
return ( DataLoader( mnist_train, batch_size, shuffle= True ) , DataLoader( mnist_test, batch_size, shuffle= True ) )
测试加载数据集函数
train_iter, test_iter = load_data_fashion_mnist( batch_size)
for X, y in train_iter :
print ( X. shape)
print ( y. shape)
print ( X. dtype)
print ( y. dtype)