AlexNet_代码详细

本文详述了AlexNet的代码实现过程,包括网络结构、关键函数的解释,如tf.truncated_normal(), tf.nn.conv2d()等,并讨论了在不同硬件配置下运行时的性能差异。通过实例展示了AlexNet的前向和后向传播,并提到了batch_size在模型尺寸中的作用。" 101095197,8396981,Flutter 集成测试实战指南,"['移动开发', 'Flutter', '测试']
摘要由CSDN通过智能技术生成

问题背景:昨天了解清除了AlexNet的原理部分,今天着手看看代码。BTW,我是参考别人的文章,不算原创,不过文中会有很多我自己的见解与补充,算是做个笔记吧。
原文链接:
https://blog.csdn.net/taoyanqi8932/article/details/71081390 @yqtaowhu的


首先还是PO一下AlexNet的网络结构:
这里写图片描述


@yqtaowhu使用的是CPU运行,怕耗时太久,因此只定义网络结构,并进行前向后向的测试.

# 此函数为输出当前层的参数(层名称、输出尺寸)
def print_activations(t):   #输入为tensor
  print(t.op.name, ' ', t.get_shape().as_list())
#此函数为搭建AlexNet的函数
def inference(images):
  """Build the AlexNet model.
  Args:
    images: Images Tensor
  Returns:
    pool5: the last Tensor in the convolutional component of AlexNet.     #返回conv_5中池化后的结果
    parameters: a list of Tensors corresponding to the weights and biases of the AlexNet model.  #返回权重和偏差列表
  """

  parameters = []  
  # conv1
  with tf.name_scope('conv1') as scope:# 将scope内生成的Variable自动命名为conv1/xxx,区分不同卷积层之间的组件。
    kernel = tf.Variable(tf.truncated_normal([11, 11, 3, 96],dtype=tf.float32,stddev=1e-1), name='weights') #conv1的核是11*11,3是输入channel数(后面的函数run_benchmark()中指定了image的channel是3),96是输出channel数.stddev标准差0.1
    conv = tf.nn.conv2d(images, kernel, [1, 4, 4, 1], padding='SAME')
    biases = tf.Variable(tf.constant(0.0, shape=[96],dtype=tf.float32),trainable=True, name='biases')  #初始化biases,一维向量,96个元素,全为0
    bias = tf.nn.bias_add(conv, biases)  #把初始biases加到conv
    conv1 = tf.nn.relu(bias, name=scope)  #计算relu
    print_activations(conv1)  #输出当前层参数
    parameters += [kernel, biases]    #更新权重和偏差

  # lrn1 没有用LRN层
  # TODO(shlens, jiayq): Add a GPU version of local response normalization.
  # 考虑到LRN层效果不明显,而且会让forward和backwood的速度大大下降所以在此没有添加

 
AlexNet是一种经典的卷积神经网络结构,是在2012年ILSVRC竞赛中取得突破性成果的模型。下面我将详细描述AlexNet的原理,并给出一个简化的代码实现。 1. 网络结构: - 输入层:输入图像的尺寸为224x224x3。 - 卷积层:第一层为11x11的卷积核,步长为4,输出通道数为96;之后接着一个尺寸为3x3、步长为2的最大池化层。 - 卷积层:第三层为尺寸为5x5的卷积核,输出通道数为256;之后接着一个尺寸为3x3、步长为2的最大池化层。 - 卷积层:第五层为尺寸为3x3的卷积核,输出通道数为384。 - 卷积层:第六层为尺寸为3x3的卷积核,输出通道数为384。 - 卷积层:第七层为尺寸为3x3的卷积核,输出通道数为256;之后接着一个尺寸为3x3、步长为2的最大池化层。 - 全连接层:第八层和第九层分别为4096个神经元的全连接层。 - 输出层:第十层为1000个神经元的全连接层,对应于1000个ImageNet类别。 2. 激活函数: - 在卷积层和全连接层之后,使用ReLU激活函数进行非线性映射。 3. 正则化和随机失活: - 在第一、第二和第五卷积层之后,使用局部响应归一化(Local Response Normalization)进行正则化。 - 在全连接层之后,使用随机失活(Dropout)进行正则化。 4. 代码实现(使用Python和PyTorch): ```python import torch import torch.nn as nn class AlexNet(nn.Module): def __init__(self, num_classes=1000): super(AlexNet, self).__init__() self.features = nn.Sequential( nn.Conv2d(3, 96, kernel_size=11, stride=4), nn.ReLU(inplace=True), nn.MaxPool2d(kernel_size=3, stride=2), nn.Conv2d(96, 256, kernel_size=5, padding=2), nn.ReLU(inplace=True), nn.MaxPool2d(kernel_size=3, stride=2), nn.Conv2d(256, 384, kernel_size=3, padding=1), nn.ReLU(inplace=True), nn.Conv2d(384, 384, kernel_size=3, padding=1), nn.ReLU(inplace=True), nn.Conv2d(384, 256, kernel_size=3, padding=1), nn.ReLU(inplace=True), nn.MaxPool2d(kernel_size=3, stride=2), ) self.classifier = nn.Sequential( nn.Dropout(), nn.Linear(256 * 6 * 6, 4096), nn.ReLU(inplace=True), nn.Dropout(), nn.Linear(4096, 4096), nn.ReLU(inplace=True), nn.Linear(4096, num_classes), ) def forward(self, x): x = self.features(x) x = torch.flatten(x, 1) x = self.classifier(x) return x ``` 以上是AlexNet的简化实现代码。请注意,这只是一个简化版本,与原始的AlexNet相比可能会有一些细微的差别。完整的AlexNet实现包含更多的细节,如LRN和更复杂的数据预处理步骤等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值