写一个简单的卷积神经网络代码,有输入输出图片的
时间: 2024-06-10 08:06:05 浏览: 13
代码如下:
```
import torch
import torch.nn as nn
class SimpleCNN(nn.Module):
def __init__(self):
super(SimpleCNN, self).__init__()
# Input channels = 3, output channels = 18
self.conv1 = nn.Conv2d(3, 18, kernel_size=3, stride=1, padding=1)
self.relu1 = nn.ReLU()
# Input channels = 18, output channels = 36
self.conv2 = nn.Conv2d(18, 36, kernel_size=3, stride=1, padding=1)
self.relu2 = nn.ReLU()
# Input channels = 36, output channels = 72
self.conv3 = nn.Conv2d(36, 72, kernel_size=3, stride=1, padding=1)
self.relu3 = nn.ReLU()
# Input dim = 72*8*8, output dim = 10
self.fc = nn.Linear(72*8*8, 10)
def forward(self, x):
# Pass through convolutional layers
x = self.conv1(x)
x = self.relu1(x)
x = self.conv2(x)
x = self.relu2(x)
x = self.conv3(x)
x = self.relu3(x)
# Flatten output from convolutional layers and pass through fully connected layer
x = x.view(x.size(0), -1)
x = self.fc(x)
return x
```
这个简单的卷积神经网络包括三个卷积层和一个全连接层。输入是一个包含3个通道的图像,并输出一个10维的向量。
相关推荐
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)