全连接神经网络:全都是线性层串起来。
卷积神经网络CNN:
3通道卷积过程:
n通道输入得到m通道输出:(每一个filter都有n个卷积核,输入通道数=卷积核数,filter数=输出通道数 )
单个卷积层示例代码:
import torch in_channel,out_channel = 5,10 kernel_size = 3 batch_size = 1 #小批量的第几个 w, h = 100, 100 input = torch.randn(batch_size,in_channel,w,h) #随机生成一个输入张量 conv_layer = torch.nn.Conv2d(in_channel, out_channel, kernel_size = kernel_size) output = conv_layer(input) print(input.shape) #torch.Size([1, 5, 100, 100]) print(output.shape) #torch.Size([1, 10, 98, 98]) print(conv_layer.weight.shape) #torch.Size([10, 5, 3, 3])
输出:
torch.Size([1, 5, 100, 100]) torch.Size([1, 10, 98, 98]) torch.Size([10, 5, 3, 3])