类似LeNet-5的简单神经网络与mnist数据集的训练

本文介绍了一个基于TensorFlow实现的简化版LeNet-5卷积神经网络,并通过MNIST手写数字数据集进行训练和验证。文章详细展示了网络结构设计、权重初始化、卷积与池化操作等关键步骤。

使用tensorflow实现的一个类似LeNet-5的简单CNN

  • 最重要的厘清tensor的维度传递和net的结构,大脑一定要清晰
  • tf.reshape()—重点掌握
  • 基本还是都用现成的库来做底层的设计,代码只是从宏观的角度来调用和设计网络结构
import tensorflow as tf
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
#下载数据集
mnist = input_data.read_data_sets('MNIST_data',one_hot=True)

#计算准确度
#输入:测试集图片和标注
#输出:当前训练的神经网络对测试机图片的预测结果的准确度
def compute_accuracy(v_xs,v_ys):
	#全局化predict网
    global prediction
    #使用网络对测试集图片进行预测
    y_pre = sess.run(prediction,feed_dict={xs:v_xs})
    #对比计算准确度
    correct_prediction = tf.equal(tf.arg_max(y_pre,1),tf.arg_max(v_ys,1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
    result = sess.run(accuracy,feed_dict={xs:v_xs,ys:v_ys})
    return result

#输入形状,产生weight变量
def weight_variable(shape):
    initial = tf.truncated_normal(shape,stddev=0.1)
    return tf.Variable(initial)
#输入形状,产生bias变量
def bias_variable(shape):
    initial = tf.constant(0.1,shape=shape)
    return tf.Variable(initial)
#卷积层
#输入:x:值;W:weight
def conv2d(x,W):
    #stride[1,x_move,y_move,1]
    return tf.nn.conv2d(x,W,strides=[1,1,1,1],padding='SAME')
#池化层
def max_pool_2x2(x):
    return tf.nn.max_pool(x,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')

#define placeholder for inputs to network
xs = tf.placeholder(tf.float32,[None,784])
ys = tf.placeholder(tf.float32,[None,10])
#keep_prob = tf.placeholder(tf.float32)
#处理一下输入
x_image = tf.reshape(xs,[-1,28,28,1])

# conv1 layer 
# fliter size=5*5 insize=1 outsize=32
W_conv1 = weight_variable([5,5,1,32])
b_conv1 = bias_variable([32])
h_conv1 = tf.nn.relu(conv2d(x_image,W_conv1)+b_conv1)#outsize = 28*28
h_pool1 = max_pool_2x2(h_conv1) #outsize = 14*14*32
# conv2 layer 
# fliter size=5*5 insize=32 outsize=64
W_conv2 = weight_variable([5,5,32,64])
b_conv2 = bias_variable([64])
h_conv2 = tf.nn.relu(conv2d(h_pool1,W_conv2)+b_conv2)#outsize = 14*14*64
h_pool2 = max_pool_2x2(h_conv2) #outsize = 7*7*64
# full connected layer1
W_fc1 = weight_variable([7*7*64,1024])
b_fc1 = bias_variable([1024])
### reshape-重点
h_pool2_flat = tf.reshape(h_pool2,[-1,7*7*64])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat,W_fc1)+b_fc1)
#h_fc1_drop = tf.nn.dropout(h_fc1,keep_prob)
## full connected layer2
W_fc2 = weight_variable([1024,10])
b_fc2 = bias_variable([10])
prediction = tf.nn.softmax(tf.matmul(h_fc1,W_fc2)+b_fc2)

# the error between predicton and real data
cross_entropy = tf.reduce_mean(-tf.reduce_sum(ys*tf.log(prediction),
                                             reduction_indices=[1]))
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)

sess = tf.Session()
sess.run(tf.initialize_all_variables())

for i in range(1000):
    #分批加载训练数据
    batch_xs,batch_ys = mnist.train.next_batch(100)
    #训练
    sess.run(train_step,feed_dict={xs:batch_xs,ys:batch_ys})
    #打印准确度
    if i%50 == 0:
        print(compute_accuracy(mnist.test.images,mnist.test.labels))
        

reference:

https://www.bilibili.com/video/av16001891/?p=29

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值