# 1. 通过TensorFlow-Slim定义卷机神经网络
import numpy as np
import tensorflow as tf
import tensorflow.contrib.slim as slim from tensorflow.examples.tutorials.mnist import input_data # 通过TensorFlow-Slim来定义LeNet-5的网络结构。
def lenet5(inputs):
inputs = tf.reshape(inputs, [-1, 28, 28, 1])
net = slim.conv2d(inputs, 32, [5, 5], padding='SAME', scope='layer1-conv')
net = slim.max_pool2d(net, 2, stride=2, scope='layer2-max-pool')
net = slim.conv2d(net, 64, [5, 5], padding='SAME', scope='layer3-conv')
net = slim.max_pool2d(net, 2, stride=2, scope='layer4-max-pool')
net = slim.flatten(net, scope='flatten')
net = slim.fully_connected(net, 500, scope='layer5')
net = slim.fully_connected(net, 10, scope='output')
return net # 训练模型。
def train(mnist):
x = tf.placeholder(tf.float32, [None, 784], name='x-input')
y_ = tf.placeholder(tf.float32, [None, 10], name='y-input')
y = lenet5(x) cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=y, labels=tf.argmax(y_, 1))
loss = tf.reduce_mean(cross_entropy) train_op = tf.train.GradientDescentOptimizer(0.01).minimize(loss)
with tf.Session() as sess:
tf.global_variables_initializer().run()
for i in range(3000):
xs, ys = mnist.train.next_batch(100)
_, loss_value = sess.run([train_op, loss], feed_dict={x: xs, y_: ys})
if i % 1000 == 0:
print("After %d training step(s), loss on training batch is %g." % (i, loss_value))
#  主程序
def main(argv=None):
mnist = input_data.read_data_sets("F:\\TensorFlowGoogle\\201806-github\\datasets\\MNIST_data\\", one_hot=True)
train(mnist) if __name__ == '__main__':
main()

吴裕雄--天生自然TensorFlow高层封装:使用TensorFlow-Slim处理MNIST数据集实现LeNet-5模型的更多相关文章

  1. 吴裕雄--天生自然TensorFlow高层封装:使用TFLearn处理MNIST数据集实现LeNet-5模型

    # 1. 通过TFLearn的API定义卷机神经网络. import tflearn import tflearn.datasets.mnist as mnist from tflearn.layer ...

  2. 吴裕雄--天生自然 pythonTensorFlow图形数据处理:TensorFlow图像处理函数

    import numpy as np import tensorflow as tf import matplotlib.pyplot as plt #读取图片 image_raw_data = tf ...

  3. 吴裕雄--天生自然TensorFlow高层封装:Estimator-自定义模型

    # 1. 自定义模型并训练. import numpy as np import tensorflow as tf from tensorflow.examples.tutorials.mnist i ...

  4. 吴裕雄--天生自然TensorFlow高层封装:Estimator-DNNClassifier

    # 1. 模型定义. import numpy as np import tensorflow as tf from tensorflow.examples.tutorials.mnist impor ...

  5. 吴裕雄--天生自然TensorFlow高层封装:Keras-TensorFlow API

    # 1. 模型定义. import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data mnist_ ...

  6. 吴裕雄--天生自然神经网络与深度学习实战Python+Keras+TensorFlow:TensorFlow与神经网络的实现

    import tensorflow as tf import numpy as np ''' 初始化运算图,它包含了上节提到的各个运算单元,它将为W,x,b,h构造运算部件,并将它们连接 起来 ''' ...

  7. 吴裕雄--天生自然python TensorFlow图片数据处理:No module named 'tensorflow.examples.tutorials'解决办法

    import numpy as np import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_dat ...

  8. 吴裕雄--天生自然python Google深度学习框架:Tensorflow实现迁移学习

    import glob import os.path import numpy as np import tensorflow as tf from tensorflow.python.platfor ...

  9. 吴裕雄--天生自然Numpy库学习笔记:NumPy Matplotlib

    Matplotlib 是 Python 的绘图库. 它可与 NumPy 一起使用,提供了一种有效的 MatLab 开源替代方案. 它也可以和图形工具包一起使用,如 PyQt 和 wxPython. W ...

随机推荐

  1. /etc/apt/sources.list.d

    deb http://ppa.launchpad.net/webupd8team/java/ubuntu xenial main# deb-src http://ppa.launchpad.net/w ...

  2. yarn storm spark

    单机zookeeper http://coolxing.iteye.com/blog/1871009 storm http://os.51cto.com/art/201309/411003_2.htm ...

  3. BZOJ 2749 [HAOI2012]外星人

    题解:对每一个>2的质数分解,最后统计2的个数 注意:如果一开始没有2则ans需+1,因为第一次求phi的时候并没有消耗2 WA了好几遍 #include<iostream> #in ...

  4. JS下拉框联动

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  5. bugku-杂项 convert

    打开题目文件,一大堆01码,用py转换成hex f=open("in.txt","r") print hex(int(str(f.read()),2)) f.c ...

  6. JSTL与EL表达式(为空判断)

    JSTL与EL表达式(为空判断) 一.循环遍历集合  1.在jsp中引入标准函数声明  <%@ taglib uri="http://java.sun.com/jsp/jstl/cor ...

  7. Apache Spark:来自Facebook的60 TB +生产用例

    本文主要讲Facebook扩展spark替换hive的过程中积累的经验和教训. 浪尖整理翻译https://databricks.com/blog/2016/08/31/apache-spark-sc ...

  8. javacv 设置帧率(续)

    前文地址:https://www.cnblogs.com/svenwu/p/9663038.html 前文已经对大多数正常情况可以支持了,但是我最近处理一些公司的视频流,发现一些坑爹的情况 每次给的视 ...

  9. Windows 远程桌面连接 CentOS7 (xrdp)

    Windows 远程桌面连接 CentOS7 (xrdp) 前提: CentOS安装桌面,如果无桌面,请执行: yum -y groups install "GNOME Desktop&qu ...

  10. LeetCode 687. Longest Univalue Path 最长同值路径 (C++/Java)

    题目: Given a binary tree, find the length of the longest path where each node in the path has the sam ...