Pytorch---CNN 落日映苍穹つ 2022-04-05 14:52 171阅读 0赞 CNN网络输入层、隐藏层(卷积层和池化层)、输出层。卷积神经网络的特殊性体现在两个方面,一方面是神经元之间的连接不是全连接,另一方面是同一层中某些神经元之间的权重值是共享的。因此学习CNN主要是学习卷积层和池化层。 1.学习CNN 神经网络首先得知道卷积的原理: <table style="width:250px;"> <tbody> <tr> <td>1</td> <td>1</td> <td>1</td> <td>1</td> </tr> <tr> <td>0</td> <td>0</td> <td>1</td> <td>1</td> </tr> <tr> <td>0</td> <td>1</td> <td>1</td> <td>0</td> </tr> <tr> <td>0</td> <td>1</td> <td>1</td> <td>0</td> </tr> </tbody> </table> 卷积 <table style="width:200px;"> <tbody> <tr> <td>1</td> <td>1</td> </tr> <tr> <td>0</td> <td>1</td> </tr> </tbody> </table> 结果为: <table style="width:250px;"> <tbody> <tr> <td>2</td> <td>3</td> <td>3</td> </tr> <tr> <td>1</td> <td>2</td> <td>2</td> </tr> <tr> <td>2</td> <td>3</td> <td>1</td> </tr> </tbody> </table> 卷积的原理就是与原来的位置一一对应,然后结果相加既可。比如上面卷积的结果就是4\*4的矩阵,结果成为了3\*3的矩阵。 此处有定则:假设上一层图大小是n\*n,卷积核大小是k\*k,则卷积后该层图的大小则是(n-k+1)\*(n-k+1). 另外需要记住的参数是padding是补0用的,stride是步长。 2.CNN层的池化层 池化层的输入一般来源于上一个卷积层,主要作用是提供很强的鲁棒性并且减少参数的量,防止过拟合的现象发生。 <table style="width:200px;"> <tbody> <tr> <td>1</td> <td>1</td> <td>2</td> <td>4</td> </tr> <tr> <td>5</td> <td>6</td> <td>7</td> <td>8</td> </tr> <tr> <td>3</td> <td>2</td> <td>1</td> <td>0</td> </tr> <tr> <td>1</td> <td>2</td> <td>3</td> <td>4</td> </tr> </tbody> </table> 经过maxpool 2\*2 filters 和stride=2,则结果为: <table style="width:150px;"> <tbody> <tr> <td>6</td> <td>8</td> </tr> <tr> <td>3</td> <td>4</td> </tr> </tbody> </table> 3. 看得视频: ![watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MjUyODA4OQ_size_16_color_FFFFFF_t_70][] 三大特性: Property1 ,Property2, Property3. 第四代码: import torch import torch.nn as nn import torchvision import torchvision.transforms as transforms # Device configuration device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu') # Hyper parameters num_epochs = 5 num_classes = 10 batch_size = 100 learning_rate = 0.001 # MNIST dataset train_dataset = torchvision.datasets.MNIST(root='../../data/', train=True, transform=transforms.ToTensor(), download=True) test_dataset = torchvision.datasets.MNIST(root='../../data/', train=False, transform=transforms.ToTensor()) # Data loader 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) # Convolutional neural network (two convolutional layers) class ConvNet(nn.Module): def __init__(self, num_classes=10): super(ConvNet, self).__init__() self.layer1 = nn.Sequential( nn.Conv2d(1, 16, kernel_size=5, stride=1, padding=2), ## 16 是自己设置得 nn.BatchNorm2d(16), nn.ReLU(), nn.MaxPool2d(kernel_size=2, stride=2)) self.layer2 = nn.Sequential( nn.Conv2d(16, 32, kernel_size=5, stride=1, padding=2), nn.BatchNorm2d(32), nn.ReLU(), nn.MaxPool2d(kernel_size=2, stride=2)) self.fc = nn.Linear(7*7*32, num_classes) def forward(self, x): out = self.layer1(x) out = self.layer2(out) out = out.reshape(out.size(0), -1) out = self.fc(out) return out model = ConvNet(num_classes).to(device) # Loss and optimizer criterion = nn.CrossEntropyLoss() optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate) # Train the model total_step = len(train_loader) for epoch in range(num_epochs): for i, (images, labels) in enumerate(train_loader): images = images.to(device) labels = labels.to(device) # Forward pass outputs = model(images) loss = criterion(outputs, labels) # Backward and optimize optimizer.zero_grad() loss.backward() optimizer.step() if (i+1) % 100 == 0: print ('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}' .format(epoch+1, num_epochs, i+1, total_step, loss.item())) # Test the model model.eval() # eval mode (batchnorm uses moving mean/variance instead of mini-batch mean/variance) with torch.no_grad(): correct = 0 total = 0 for images, labels in test_loader: images = images.to(device) labels = labels.to(device) outputs = model(images) _, predicted = torch.max(outputs.data, 1) total += labels.size(0) correct += (predicted == labels).sum().item() print('Test Accuracy of the model on the 10000 test images: {} %'.format(100 * correct / total)) # Save the model checkpoint torch.save(model.state_dict(), 'model.ckpt') © 2019 GitHub, Inc. padding 通常是补0,也不会增加噪声。 参考: [https://github.com/yunjey/pytorch-tutorial/blob/master/tutorials/02intermediate/convolutional\_neural\_network/main.py\#L35-L56][https_github.com_yunjey_pytorch-tutorial_blob_master_tutorials_02intermediate_convolutional_neural_network_main.py_L35-L56] [pytorch 中文文档][pytorch] [李宏毅-Convolutional Neural Network(CNN)-卷积神经网络][-Convolutional Neural Network_CNN_-] [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MjUyODA4OQ_size_16_color_FFFFFF_t_70]: /images/20220405/abad2ec40bba4f459a9b270423fcf1e9.png [https_github.com_yunjey_pytorch-tutorial_blob_master_tutorials_02intermediate_convolutional_neural_network_main.py_L35-L56]: https://github.com/yunjey/pytorch-tutorial/blob/master/tutorials/02-intermediate/convolutional_neural_network/main.py#L35-L56 [pytorch]: https://pytorch-cn.readthedocs.io/zh/latest/package_references/torch-nn/ [-Convolutional Neural Network_CNN_-]: https://www.bilibili.com/video/av23593949?from=search&seid=13442106649215121429
还没有评论,来说两句吧...