Python——pytorch建立CNN网络

一.数据加载器的建立

#加载数据集
import torchvision.dataset as dsets
import torchvision.transforms as transforms

#加载数据
train_dataset=dsets.MINIST(root='./data', #文件存放路径
                           train=True, #提取训练集
                           transform=transforms.ToTensor(), #将图像转化为Tensor
                            download=True)


test_dataset=dsets.MNIST(root='/data',
                        train=False,
                        transform=transforms.ToTesnsor())
                         

#数据集的加载
train_loader=torch.utils.data.DataLoader(dataset=train_dataset,
                                        batch_size=batch_size,
                                        shuttle=True)

test_loader=torch.utils.data.DataLoader(dataset=test_dataset,
                                        batch_size=batch_size,
                                        shuffle=False)

###为了区分出校验集和测试集,采用采样器和加载器配合的方式
indices=range(len(test_dataset))
indices_val=indices[:5000]
indices_test=indices[5000:]

#根据下标构造采样器
sampler_val=torch.utils.data.sampler.SubsetRandomSampler(indices_val)
sampler_test=torch.utils.data.sampler.SubsetRandomSampler(indices_test)

#根据两个采样器定义加载器
validation_loader=torch.utils.data.DataLoader(dataset=test_dataset,
                                              batch_size=batch_size,
                                              shuffle=True,
                                              sampler=sampler_val)

test_loader=torch.utils.data.DataLoader(dataset=test_dataset,
                                        batch_size=batch_size,
                                        sampler=sampler_test)

二.神经网络的实现

class ConvNet(nn.Module):
    def__init__(self):
        super(ConvNet,self).__init__()
        self.conv1=nn.Conv2d(1,4,5,padding=2)
        self.pool=nn.MaxPool2d(2,2)
        self.conv2=nn.Conv2d(4,8,5,padding=2)
        self.fc1=nn.Linear(image_size//4*image_size//4*8,512)
        self.fc2=nn.Linear(512,num_classes)

    def forward(self,x):
        x=F.relu(self.conv1(x))
        x=self.pool(x)
        x=F.relu(self.conv2(x))
        x=self.pool(x)
        x=x.view(-1,image_size//4*image_size//4*8)  #将原本是立方体的张量变成一维张量
        x=F.relu(self.fc1(x))
        x=F.dropout(x,training=self.training)
        x=self.fc2(x)
        x=F.log_softmax(x)
        return x

三.训练

#训练循环
for epoch in range(num_epochs):
    for batch_idx,(data,target) in enumerate(train_loader):
        data,target=Variable(data),Variable(target)
        
        #能够使用dropout
        net.train()
        output=net(data)

        loss=criterion(output,target)
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        if batch_idx%100==0:
            #打印和记录数据

#在测试集上运行
net.eval()
test_loss=0
correct=0
for data,target in test_loader:
    data,target=Variable(data),Variable(target)
    output=net(data)
    val=rightness(output,target)

 

你可能感兴趣的:(Pytorch)