必威体育Betway必威体育官网
当前位置:首页 > IT技术

卷积神经网络&&Mnist

时间:2019-10-15 21:43:30来源:IT技术作者:seo实验室小编阅读:51次「手机版」
 

欲上青天揽明月

事实证明,这个世界的本质是张量的流动。

(一)构造双层卷积层和池化层

from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
mnist=input_data.read_data_sets("MNIST_data",one_hot=True)
sess=tf.Interactivesession()
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)
def conv2d(x,w):
    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')
x=tf.placeholder(tf.float32,[None,784])
y_=tf.placeholder(tf.float32,[None,10])
x_image=tf.reshape(x,[-1,28,28,1])

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)
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)

(二)构造全连接层

W_fc1=weight_variable([7*7*64,1024])
b_fc1=bias_variable([1024])
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)

keep_prob=tf.placeholder(tf.float32)
h_fc1_drop=tf.nn.dropout(h_fc1,keep_prob)

W_fc2=weight_variable([1024,10])
b_fc2=bias_variable([10])

(三)卷积神经网络的生成及验证

y_conv=tf.nn.softmax(tf.matmul(h_fc1_drop,W_fc2)+b_fc2)
cross_entropy=-tf.reduce_sum(y_*tf.log(y_conv))
#cross_entropy=tf.reduce_mean(-tf.reduce_sum(y_*tf.log(y_conv),reduction_indices=[1]))
#cross_entropy = -tf.reduce_sum(y_ * tf.log(tf.clip_by_value(y_conv, 1e-10, 1.0)))
trainstep = tf.train.AdamOptimizer(1e-4).Minimize(cross_entropy)
correct_prediction=tf.equal(tf.argmax(y_conv,1),tf.argmax(y_,1))
accuracy=tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
tf.global_variables_initializer().run()
for i in range(5000):
    BATch=mnist.train.next_batch(50)
    trainstep.run(feed_dict={x:batch[0],y_:batch[1],keep_prob:0.5})                         
    if i%100==0:
        train_accuracy=accuracy.eval(feed_dict={x:batch[0],y_:batch[1],keep_prob:1.0})
        print("step %d,trainning accuracy %g" %(i,train_accuracy))

(四)训练过程输出与最终的正确率

extracting MNIST_data\train-images-idx3-ubyte.gz
Extracting MNIST_data\train-labels-idx1-ubyte.gz
Extracting MNIST_data\t10k-images-idx3-ubyte.gz
Extracting MNIST_data\t10k-labels-idx1-ubyte.gz
step 0,trainning accuracy 0.02
step 100,trainning accuracy 0.86
step 200,trainning accuracy 0.88
step 300,trainning accuracy 0.9
step 400,trainning accuracy 0.92
step 500,trainning accuracy 1
step 600,trainning accuracy 1
step 700,trainning accuracy 0.96
step 800,trainning accuracy 0.9
step 900,trainning accuracy 0.98
step 1000,trainning accuracy 0.98
step 1100,trainning accuracy 0.96
step 1200,trainning accuracy 1
step 1300,trainning accuracy 1
step 1400,trainning accuracy 0.98
step 1500,trainning accuracy 0.94
step 1600,trainning accuracy 0.96
step 1700,trainning accuracy 0.98
step 1800,trainning accuracy 1
step 1900,trainning accuracy 1
step 2000,trainning accuracy 0.96
step 2100,trainning accuracy 0.98
step 2200,trainning accuracy 0.98
step 2300,trainning accuracy 1
step 2400,trainning accuracy 1
step 2500,trainning accuracy 1
step 2600,trainning accuracy 0.98
step 2700,trainning accuracy 1
step 2800,trainning accuracy 0.98
step 2900,trainning accuracy 1
step 3000,trainning accuracy 0.96
step 3100,trainning accuracy 1
step 3200,trainning accuracy 0.98
step 3300,trainning accuracy 1
step 3400,trainning accuracy 0.96
step 3500,trainning accuracy 0.98
step 3600,trainning accuracy 0.98
step 3700,trainning accuracy 1
step 3800,trainning accuracy 0.98
step 3900,trainning accuracy 0.98
step 4000,trainning accuracy 1
step 4100,trainning accuracy 0.98
step 4200,trainning accuracy 1
step 4300,trainning accuracy 1
step 4400,trainning accuracy 1
step 4500,trainning accuracy 1
step 4600,trainning accuracy 0.98
step 4700,trainning accuracy 0.98
step 4800,trainning accuracy 1
step 4900,trainning accuracy 1

print("test accuracy %g"%accuracy.eval(feed_dict={x:mnist.test.images,y_:mnist.test.labels,keep_prob:1.0}))
test accuracy 0.9865

相关阅读

CNN中的卷积核及TensorFlow中卷积的各种实现

转载: https://www.cnblogs.com/Yu-FeiFei/p/6800519.html https://www.cnblogs.com/hans209/p/7103168.html https://blog.csdn.n

卷积公式的理解

卷积公式的由来 卷积公式与Laplace变换的联系Laplace变换公式:F(s)=∫0∞f(t)e−stdtG(s)=∫0∞g(t)e−stdtF(s) = \int_0^\in

卷积神经网络CNN(一)基本概念、卷积

卷积神经网络(Convolutional Neural Network)是一个专门针对图像识别问题设计的神经网络。它模仿人类识别图像的多层过程:瞳孔摄入像

华裔女性钱璐璐:用 DNA 开发人工智能神经网络,识别手写

“既然要学人脑的思维方式,为什么不去研究人脑?”霍金斯在《论智能》中说道。 如今,不少生物学研究者正朝着这个方向努力。 不过,

优达学城——浅析神经网络

神经网络推理过程PerceptronWeightsActivation functionBiasPerceptron Data, like test scores and grades, are fed into a net

分享到:

栏目导航

推荐阅读

热门阅读