CNN之LeNet

LeNet实现(TensorFlow&PyTorch)

TensorFlow

import tensorflow as tf 

def LeNet(input_tensor,train,regularizer):
 
    #第一层:卷积层,卷积核5×5,输入1,输出6,不使用全0补充,步长为1。
    #尺寸变化:32×32×1->28×28×6
    with tf.variable_scope('layer1-conv1'):
        conv1_weights = tf.get_variable('weight',[5,5,1,6],initializer=tf.truncated_normal_initializer(stddev=0.1))
        conv1_biases = tf.get_variable('bias',[6],initializer=tf.constant_initializer(0.0))
        conv1 = tf.nn.conv2d(input_tensor,conv1_weights,strides=[1,1,1,1],padding='VALID')
        relu1 = tf.nn.relu(tf.nn.bias_add(conv1,conv1_biases))
 
    #第二层:池化层,过滤器的尺寸为2×2,使用全0补充,步长为2。
    #尺寸变化:28×28×6->14×14×6
    with tf.name_scope('layer2-pool1'):
        pool1 = tf.nn.max_pool(relu1,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')
 
    #第三层:卷积层,过滤器的尺寸为5×5,深度为16,不使用全0补充,步长为1
    #尺寸变化:14×14×6->10×10×16
    with tf.variable_scope('layer3-conv2'):
        conv2_weights = tf.get_variable('weight',[5,5,6,16],initializer=tf.truncated_normal_initializer(stddev=0.1))
        conv2_biases = tf.get_variable('bias',[16],initializer=tf.constant_initializer(0.0))
        conv2 = tf.nn.conv2d(pool1,conv2_weights,strides=[1,1,1,1],padding='VALID')
        relu2 = tf.nn.relu(tf.nn.bias_add(conv2,conv2_biases))
 
    #第四层:池化层,过滤器的尺寸为2×2,使用全0补充,步长为2。
    #尺寸变化:10×10×6->5×5×16
    with tf.variable_scope('layer4-pool2'):
        pool2 = tf.nn.max_pool(relu2,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')
 
    #将第四层池化层的输出转化为第五层全连接层的输入格式。

    pool_shape = pool2.get_shape().as_list()
    nodes = pool_shape[1]*pool_shape[2]*pool_shape[3]
    reshaped = tf.reshape(pool2,[-1,nodes])
 
    #第五层:全连接层,nodes=5×5×16=400,400->120的全连接
    #尺寸变化:比如一组训练样本为64,那么尺寸变化为64×400->64×120

    with tf.variable_scope('layer5-fc1'):
        fc1_weights = tf.get_variable('weight',[nodes,120],initializer=tf.truncated_normal_initializer(stddev=0.1))
        if regularizer != None:
            tf.add_to_collection('losses',regularizer(fc1_weights))
        fc1_biases = tf.get_variable('bias',[120],initializer=tf.constant_initializer(0.1))
        fc1 = tf.nn.relu(tf.matmul(reshaped,fc1_weights) + fc1_biases)
        if train:
            fc1 = tf.nn.dropout(fc1,0.5)
 
    #第六层:全连接层,120->84的全连接
    #尺寸变化:比如一组训练样本为64,那么尺寸变化为64×120->64×84
    with tf.variable_scope('layer6-fc2'):
        fc2_weights = tf.get_variable('weight',[120,84],initializer=tf.truncated_normal_initializer(stddev=0.1))
        if regularizer != None:
            tf.add_to_collection('losses',regularizer(fc2_weights))
        fc2_biases = tf.get_variable('bias',[84],initializer=tf.truncated_normal_initializer(stddev=0.1))
        fc2 = tf.nn.relu(tf.matmul(fc1,fc2_weights) + fc2_biases)
        if train:
            fc2 = tf.nn.dropout(fc2,0.5)
 
    #第七层:全连接层(近似表示),84->10的全连接
    #尺寸变化:比如一组训练样本为64,那么尺寸变化为64×84->64×10。最后,64×10的矩阵经过softmax之后就得出了64张图片分类于每种数字的概率,
    #即得到最后的分类结果。
    with tf.variable_scope('layer7-fc3'):
        fc3_weights = tf.get_variable('weight',[84,4],initializer=tf.truncated_normal_initializer(stddev=0.1))
        if regularizer != None:
            tf.add_to_collection('losses',regularizer(fc3_weights))
        fc3_biases = tf.get_variable('bias',[4],initializer=tf.truncated_normal_initializer(stddev=0.1))
        logit = tf.matmul(fc2,fc3_weights) + fc3_biases

    return logit

PyTorch

import torch


class LeNet(torch.nn.Module):
	#构造函数,定义网络结构
	def __init__(self):
		super(LeNet,self).__init__()
		#卷基层1,1个输入通道,6个输出通道,卷积核5*5
		self.conv1=torch.nn.Conv2d(1,6,kernel_size=5,padding=2)
		#卷基层2,6个输入通道,16个输出通道,卷积核5*5
		self.conv2=torch.nn.Conv2d(6,16,5)
		#全连接层
		self.fc1=torch.nn.Linear(16*5*5,120)
		self.fc2=torch.nn.Linear(120,84)
		self.fc3=torch.nn.Linear(84,10)

	#前向传播函数
	def forward(self,x):
		#卷积->激活->最大池化
		x=torch.nn.MaxPool2d(torch.nn.ReLU(self.conv1(x)),(2,2))
		#卷积->最大池化
		x=torch.nn.MaxPool2d(self.conv2(2),(2,2))
		x=x.view(x.size(0),-1)
		x=torch.nn.ReLU(self.fc1(x))
		x=torch.nn.ReLU(self.fc2(x))
		x=self.fc3(x)

		retun x 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值