神经网络实现mnist_详细注释版(附数据集下载地址)

import input_data
import tensorflow as tf
sess  = tf.InteractiveSession()
x = tf.placeholder("float", shape=[None, 784])
y_ = tf.placeholder("float", shape=[None, 10])
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
#定义两个函数进行初始化
def weight_variable(shape):
    initial=tf.truncated_normal(shape,stddev=0.1)#这个函数产生正态分布
    return tf.Variable(initial)

def bias_variable(shape):
    initial=tf.constant(0.1,shape=shape)
    return tf.Variable(initial)
#卷积使用1步长(stride size) 0边距(padding zize)
def conv2d(x,W):
    return tf.nn.conv2d(x,W,strides=[1,1,1,1],padding='SAME')
#池化用简单的2x2大小的模板做max pooling
def max_pool_2x2(x):
    return tf.nn.max_pool(x,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')



def variable_summaries(var):
    with tf.name_scope('summaries'):
        mean=tf.reduce_mean(var)
        tf.summary.scalar('mean',mean)
        with tf.name_scope('stddev'):
            stddev=tf.sqrt(tf.reduce_mean(tf.square(var-mean)))
        tf.summary.scalar('stddev',stddev)
        tf.summary.scalar('max',tf.reduce_max(var))
        tf.summary.scalar('min',tf.reduce_min(var))
        tf.summary.histogram('histogram',var)
#第一层卷积 一个卷积接一个max pooling 完成,卷积额权重张量形状是[5,5,1,32] 前两个维度是patch大小 接着是·通道数目 最后是输出的通道数目
W_conv1 = weight_variable([5,5,1,32])
#每一个输出通道都有一个对应的偏置量
b_conv1 = bias_variable([32])
#把x变成一个4d向量 其第2、第3维 对应图片的宽、高,最后一维代表图片的颜色通道数(因为是灰度图 所以这里通道数为1,如果是rgb彩色图,则为3)
x_image = tf.reshape(x,[-1,28,28,1])
#把x_image和权值向量进行卷积 加上偏置项 然后应用relu激活函数,最后进行max pooling
h_conv1 = tf.nn.relu(conv2d(x_image,W_conv1)+b_conv1)
h_pool1 = max_pool_2x2(h_conv1)

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)
h_pool2 = max_pool_2x2(h_conv2)
#密集连接层  图片尺寸减小到7x7 我们加入一个有1024个神经元的全连接层 用来用于处理整个图片 我们把池化层输出的张量reshape 成一些向量,乘上权重矩阵 加上偏执 并对其使用RELU函数
W_fcl  =  weight_variable([7*7*64,1024])
b_fcl  =  bias_variable([1024])

h_pool2_flat = tf.reshape(h_pool2,[-1,7*7*64])
h_fcl  = tf.nn .relu(tf.matmul(h_pool2_flat,W_fcl)+b_fcl)

#在训练中启动placeholder 启动dropout来减少过拟合
keep_prob =tf.placeholder("float")
h_fcl_drop =tf.nn.dropout(h_fcl,keep_prob)

#输出层 softmax
W_fc2 = weight_variable([1024,10])
b_fc2 = bias_variable([10])
y_conv= tf.nn.softmax(tf.matmul(h_fcl_drop,W_fc2) + b_fc2)
#评估模型
#我们用最速下降法让交叉熵下降 步长为0.01
cross_entropy = -tf.reduce_sum(y_*tf.log(y_conv))
#在运行时会使用梯度下降来更新函数 因此 整个训练模型可以通过train_step来进行
trian_step=tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
# 来检测我们的预测是否真实标签匹配(索引位置一样表示匹配)。
correct_prediction = tf.equal(tf.arg_max(y_conv,1),tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction,"float")) # tf.cast 转化数据
sess.run(tf.initialize_all_variables())
for i in range(2000):
    #每一次训练 加载50个训练样本
    batch = mnist.train.next_batch(50)
    if i% 100 ==0:
        #计算没100步的准确率
        train_accurancy=accuracy.eval(feed_dict={
            x:batch[0],
            y_:batch[1],
            keep_prob:1.0
        })
        print("step %d ,trianing accuracy %g"%(i,train_accurancy))
        #通过feed_dict将x、y_张量占位符用训练数据来替代,注意 在计算图中 可以用 feed_dict来替代任何张量
    trian_step.run(feed_dict={x:batch[0],y_:batch[1],keep_prob:0.5})
print("test accuaracy %g"%accuracy.eval(feed_dict={
    x:mnist.test.images,
    y_:mnist.test.labels,
    keep_prob:1.0
}))

链接:https://pan.baidu.com/s/1BGl1zgQtDR1zWvex3v9OBg 密码:wdn0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值