import torch
import torchvision
import torchvision.transforms as transforms
train_set=torchvision.datasets.FashionMNIST(
root='./data/FashionMNIST'
,train=True
,download=True
,transform=transforms.Compose([
transforms.ToTensor()
])
)
train_loader=torch.utils.data.DataLoader(train_set,batch_size=10)#训练加载器,使用的数据加载构造器,设置批处理大小为10,如果不设置则默认为1
import numpy as np
import matplotlib.pyplot as plt
torch.set_printoptions(linewidth=120)
len(train_set)#训练集数
train_set.train_labels#类标签
train_set.train_labels.bincount()#每个类的样本数
sample=next(iter(train_set))#将一个训练集对象传递给python内置的内部函数,返回迭代器的下一个项目
len(sample)#2,因为包含数据和标签
type(sample)#tuple
image,label=sample
image.shape
plt.imshow(image.squeeze(),cmap='gray')
print('label:',label)
10个一批
batch=next(iter(train_loader))
images,labels=batch
grid=torchvision.utils.make_grid(images,nrow=10)
#创建一个网格,图像作为第一个参数,nrow指定每行的图像数量
plt.figure(figsize=(15,15))
plt.imshow(np.transpose(grid,(1,2,0)))
print('labels:',labels)
train_loader=torch.utils.data.DataLoader(train_set,batch_size=100)
batch=next(iter(train_loader))
images,labels=batch
grid=torchvision.utils.make_grid(images,nrow=10)
plt.figure(figsize=(15,15))
plt.imshow(np.transpose(grid,(1,2,0)))
plt.savefig('clothes')
print('labels:',labels)
class Network:
def __init__(self):
self.layer=None
def forward(self,t):
t=self.layer(t)
return t
import torch.nn as nn
class Network(nn.Module):
def __init__(self):
super(Network,self).__init()
self.layer=None
def forward(self,t):
t=self.layer(t)
return t
class Network(nn.Module):
def __init__(self):
super(Network,self).__init__()
self.conv1=nn.Conv2d(in_channels=1,out_channels=6,kernel_size=5)#复合层
self.conv2=nn.Conv2d(in_channels=6,out_channels=12,kernel_size=5)
self.fc1=nn.Linear(in_features=12*4*4,out_features=120)#线性/全连接层/dense
self.fc2=nn.Linear(in_features=120,out_features=60)
self.out=nn.Linear(in_features=60,out_features=10)
def forward(self,t):
#implement the forward pass
return t
network=Network()
network
kernel_size 卷积核 卷积滤波器
out_channels 输出通道(与滤波器数量一致)
out_features 输出特征