从时间上来说,这篇文章写的完了,因为这个实验早就做完了;但从能力上来说,这篇文章出现的早了,因为很多地方我都还没有理解。如果不现在写,不知道什么时候会有时间是其一,另外一个原因是怕自己过段时间忘记。

 #!/usr/bin/env python
# -*- coding: utf-8 -*- # @Author : mario
# @File : mnist_faltung.py
# @Project : base
# @Time : 2018-12-19 14:11:38
# @Desc : File is ... import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data mnist = input_data.read_data_sets("data/", one_hot=True) def init_weight_variable(shape):
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(initial) def init_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]) W_conv_1 = init_weight_variable([5, 5, 1, 32])
b_conv_1 = init_bias_variable([32]) x_image = tf.reshape(x, [-1, 28, 28, 1]) h_conv_1 = tf.nn.relu(conv2d(x_image, W_conv_1) + b_conv_1)
h_pool_1 = max_pool_2x2(h_conv_1) W_conv_2 = init_weight_variable([5, 5, 32, 64])
b_conv_2 = init_bias_variable([64]) h_conv_2 = tf.nn.relu(conv2d(h_pool_1, W_conv_2) + b_conv_2)
h_pool_2 = max_pool_2x2(h_conv_2) W_fc_1 = init_weight_variable([7 * 7 * 64, 1024])
b_fc_1 = init_bias_variable([1024]) h_pool_flat = tf.reshape(h_pool_2, [-1, 7 * 7 * 64])
h_fc_1 = tf.nn.relu(tf.matmul(h_pool_flat, W_fc_1) + b_fc_1) keep_prob = tf.placeholder(tf.float32)
h_fc1_drop = tf.nn.dropout(h_fc_1, keep_prob) W_fc_2 = init_weight_variable([1024, 10])
b_fc_2 = init_bias_variable([10]) y_conv = tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc_2) + b_fc_2) cross_entropy = -tf.reduce_sum(y_ * tf.log(y_conv))
train_step = tf.train.AdagradOptimizer(1e-4).minimize(cross_entropy) sess = tf.InteractiveSession()
init = tf.global_variables_initializer()
sess.run(init) for _ in range(20000):
batch = mnist.train.next_batch(50)
train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5}) correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) print(accuracy.eval(feed_dict={x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0}))

python版本:Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 05:52:31) ;tensor flow版本:1.12.0

10、11行:导入必要模块

13行:加载本地的数据

16~18行:定义初始化权重变量函数

21~23行:定义初始化偏置变量函数

26~27行:定义一个步长为1,边距为0的2x2的卷积函数

30~31行:定义一个2x2的池化函数

34、35行:定义占位符x和y_,其中x是为了接收原始数据,y_是为了接受原始数据标签

37、38行:初始化第一层卷积权重和偏置量

40行:重塑原始数据结构,我们要把[n,784]这样的数据结构转换成卷积需要的[n,28,28,1],这里的n是指数据量,原始数据是将28x28像素的图片展开为784,卷积相当于我们先将数据还原为28x28,最后的1是指通道数

42、43行:进行卷积操作,并将卷积结果池化

45~49行:进行第二次卷积操作,同样也是将卷积结果池化

51~61行:使用ReLU,其中57行和58行是为了防止过拟合

63行:使用softmax算法确定其分类

65行:计算交叉熵

66行:利用交叉熵,调用AdagradOptimizer算法,训练模型

68~70行:启用session,初始化变量

72~74行:每次50个训练20000次

76~79行:评估模型识别率

也是遇到了很多的问题,但几乎都是因为不理解代码造成的,虽然现在代码是改对了,但是不理解的地方还是有很多,而且很多概念也是不理解,并且不知道实际上是做了什么操作,比如说卷积、池化等,倒是做了什么?感觉这个还是需要后续了解的。“路漫漫其修远兮,吾将上下而求索”用在这里再合适不过了。

最新文章

  1. JavaScript调试 - debugger语句
  2. Javascript获取select下拉框选中的的值
  3. IOS8解决获取位置坐标信息出错(Error Domain=kCLErrorDomain Code=0)(转)
  4. UIImagePickerController的知识点
  5. How Do Annotations Work in Java?--转
  6. 轻松掌握:JavaScript状态模式
  7. 1.4 基础知识——GP2.2 计划 与 GP2.8 计划跟踪
  8. CSS vertical-align 属性
  9. JavaWeb基础: 获取资源文件
  10. C#比较时间大小 1、比较时间大小的实验
  11. [BZOJ1220][POJ1091][HNOI2002]跳蚤
  12. FSM
  13. Android——文件的保存和读取
  14. LintCode "Maximum Gap"
  15. Pimp_my_Z1
  16. 命令行分析java线程CPU占用
  17. ER图与UML图
  18. 理解SQL SERVER中的分区表
  19. Java版权信息之Jautodoc
  20. 10.Java 加解密技术系列之 DH

热门文章

  1. centos 6.x 配置 mail 发送外部邮件详解和 sendmail 使用简介
  2. 爬虫之Js混淆&加密案例
  3. 4、、多变量线性回归(Linear Regression with Multiple Variables)
  4. SQL 查询 group by 的使用注意点
  5. Maven入门指南10:Maven的生命周期和插件
  6. css day1
  7. overload和override的含义和区别
  8. Oracle数据库同义词
  9. Java的GC机制及算法
  10. 【LeetCode】栈 stack(共40题)