当前位置:   article > 正文

TensorFlow实现基础CNN,两层卷积+2层全连接网络_全连接层加卷积层代码

全连接层加卷积层代码
  1. import tensorflow as tf
  2. from tensorflow.examples.tutorials.mnist import input_data
  3. #加载数据集
  4. mnist=input_data.read_data_sets('MNIST_data',one_hot=True)
  5. #每个批次的大小
  6. batch_size=50
  7. #共有xx个批次
  8. n_batch=mnist.train.num_examples//batch_size
  9. #初始化权重
  10. def weight_variable(shape):
  11. #生成shape结构的变量,方差=0.1
  12. initial=tf.truncated_normal(shape,stddev=0.1)
  13. return tf.Variable(initial)
  14. #初始化偏置
  15. def bias_variable(shape):
  16. initial=tf.constant(0.1,shape=shape)
  17. return tf.Variable(initial)
  18. #卷积层
  19. def conv2d(x,W):
  20. return tf.nn.conv2d(x,W,strides=[1,1,1,1],padding='SAME')
  21. #池化层
  22. def max_pool_2x2(x):
  23. return tf.nn.max_pool(x,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')
  24. #定义变量等
  25. x=tf.placeholder(tf.float32,[None,784])
  26. y=tf.placeholder(tf.float32,[None,10])
  27. #将一维的图片数据转化为2/
  28. x_image=tf.reshape(x,[-1,28,28,1])
  29. #开始写我们的第一层卷积层
  30. #初始化第一层的权重,第一层的权重应该为5*5*32的,在一通道的图像上采集
  31. #初始化第一层的偏置,第一层的偏置应与第一层卷积核数一致
  32. #得到第一层的卷积输出,=32个卷积核分别对其循环相乘在相加
  33. #进行第一层的池化,池化采用2*2的卷积核,采用samepadding的方式进行池化
  34. W_conv1=weight_variable([5,5,1,32])
  35. B_conv1=bias_variable([32])
  36. h_conv1=tf.nn.relu(conv2d(x_image,W_conv1)+B_conv1)
  37. h_pool1=max_pool_2x2(h_conv1)
  38. #第2层的卷积
  39. W_conv2=weight_variable([5,5,32,64])
  40. B_conv2=bias_variable([64])
  41. h_conv2=tf.nn.relu(conv2d(h_pool1,W_conv2)+B_conv2)
  42. h_pool2=max_pool_2x2(h_conv2)
  43. #使用全连接神经网络进行训练
  44. #经过第一层卷积池化之后得到14*14*32
  45. #经过第二层卷积池化之后得到7*7*64,通道数为64
  46. #定义神经网络,输入为7*7*64,第一层隐藏层采用1024个神经元
  47. #初始化第一层的权重
  48. W_nn1=weight_variable([7*7*64,1024])
  49. B_nn1=bias_variable([1024])
  50. #此时将我们的输出展开为1维,以方便神经网络进行计算
  51. h_x_flat=tf.reshape(h_pool2,[-1,7*7*64])
  52. #进行神经网络计算,h_nn1为第一层隐藏层的输出
  53. h_nn1=tf.nn.relu(tf.matmul(h_x_flat,W_nn1)+B_nn1)
  54. #进行dropout处理
  55. keep_props=tf.placeholder(tf.float32)
  56. h_nn1=tf.nn.dropout(h_nn1,keep_props)
  57. #第二层神经层
  58. W_nn2=weight_variable([1024,10])
  59. B_nn2=bias_variable([10])
  60. h_nn2=tf.nn.relu(tf.matmul(h_nn1,W_nn2)+B_nn2)
  61. #得到神经网络的预测结果h_nn2
  62. #定义神经网络代价函数
  63. cross_entropy=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y,logits=h_nn2))
  64. #定义优化器
  65. train_step=tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
  66. #预测结果和正确结果的比对
  67. correct_prediction=tf.equal(tf.argmax(h_nn2,1),tf.argmax(y,1))
  68. #求准确率
  69. accuracy=tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
  70. #进行循环反向传递
  71. with tf.Session() as sess:
  72. sess.run(tf.global_variables_initializer())
  73. for i in range(21):
  74. for batch in range(n_batch):
  75. x_batch,y_batch=mnist.train.next_batch(batch_size)
  76. sess.run(train_step,feed_dict={x:x_batch,y:y_batch,keep_props:0.7})
  77. acc=sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels,keep_props:1})
  78. print('iter {0}, acc={1}'.format(i,acc))

输出结果:

 


原文:https://blog.csdn.net/Fitz_p/article/details/80836626 

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号