簡(jiǎn)介
創(chuàng)新互聯(lián)建站,為您提供網(wǎng)站建設(shè)公司、網(wǎng)站制作公司、網(wǎng)站營(yíng)銷推廣、網(wǎng)站開發(fā)設(shè)計(jì),對(duì)服務(wù)成都玻璃鋼坐凳等多個(gè)行業(yè)擁有豐富的網(wǎng)站建設(shè)及推廣經(jīng)驗(yàn)。創(chuàng)新互聯(lián)建站網(wǎng)站建設(shè)公司成立于2013年,提供專業(yè)網(wǎng)站制作報(bào)價(jià)服務(wù),我們深知市場(chǎng)的競(jìng)爭(zhēng)激烈,認(rèn)真對(duì)待每位客戶,為客戶提供賞心悅目的作品。 與客戶共同發(fā)展進(jìn)步,是我們永遠(yuǎn)的責(zé)任!卷積神經(jīng)網(wǎng)絡(luò)(Convolutional Neural Network, CNN)是深度學(xué)習(xí)技術(shù)中極具代表的網(wǎng)絡(luò)結(jié)構(gòu)之一,在圖像處理領(lǐng)域取得了很大的成功,在國(guó)際標(biāo)準(zhǔn)的ImageNet數(shù)據(jù)集上,許多成功的模型都是基于CNN的。
卷積神經(jīng)網(wǎng)絡(luò)CNN的結(jié)構(gòu)一般包含這幾個(gè)層:
PyTorch實(shí)戰(zhàn)
本文選用上篇的數(shù)據(jù)集MNIST手寫數(shù)字識(shí)別實(shí)踐CNN。
import torch import torch.nn as nn import torch.nn.functional as F import torch.optim as optim from torchvision import datasets, transforms from torch.autograd import Variable # Training settings batch_size = 64 # MNIST Dataset train_dataset = datasets.MNIST(root='./data/', train=True, transform=transforms.ToTensor(), download=True) test_dataset = datasets.MNIST(root='./data/', train=False, transform=transforms.ToTensor()) # Data Loader (Input Pipeline) train_loader = torch.utils.data.DataLoader(dataset=train_dataset, batch_size=batch_size, shuffle=True) test_loader = torch.utils.data.DataLoader(dataset=test_dataset, batch_size=batch_size, shuffle=False) class Net(nn.Module): def __init__(self): super(Net, self).__init__() # 輸入1通道,輸出10通道,kernel 5*5 self.conv1 = nn.Conv2d(1, 10, kernel_size=5) self.conv2 = nn.Conv2d(10, 20, kernel_size=5) self.mp = nn.MaxPool2d(2) # fully connect self.fc = nn.Linear(320, 10) def forward(self, x): # in_size = 64 in_size = x.size(0) # one batch # x: 64*10*12*12 x = F.relu(self.mp(self.conv1(x))) # x: 64*20*4*4 x = F.relu(self.mp(self.conv2(x))) # x: 64*320 x = x.view(in_size, -1) # flatten the tensor # x: 64*10 x = self.fc(x) return F.log_softmax(x) model = Net() optimizer = optim.SGD(model.parameters(), lr=0.01, momentum=0.5) def train(epoch): for batch_idx, (data, target) in enumerate(train_loader): data, target = Variable(data), Variable(target) optimizer.zero_grad() output = model(data) loss = F.nll_loss(output, target) loss.backward() optimizer.step() if batch_idx % 200 == 0: print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format( epoch, batch_idx * len(data), len(train_loader.dataset), 100. * batch_idx / len(train_loader), loss.data[0])) def test(): test_loss = 0 correct = 0 for data, target in test_loader: data, target = Variable(data, volatile=True), Variable(target) output = model(data) # sum up batch loss test_loss += F.nll_loss(output, target, size_average=False).data[0] # get the index of the max log-probability pred = output.data.max(1, keepdim=True)[1] correct += pred.eq(target.data.view_as(pred)).cpu().sum() test_loss /= len(test_loader.dataset) print('\nTest set: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n'.format( test_loss, correct, len(test_loader.dataset), 100. * correct / len(test_loader.dataset))) for epoch in range(1, 10): train(epoch) test()