TensorFlow实现LeNet5模型
# -*- coding: utf-8 -*-
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
# 获取mnist数据
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
# 注册默认session 后面操作无需指定session 不同sesson之间的数据是独立的
sess = tf.InteractiveSession()
# 构造参数W函数 给一些偏差0.1防止死亡节点
def weight_variable(shape):
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(initial)
# 构造偏差b函数
def bias_variable(shape):
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial)
# x是输入,W为卷积参数 如[5,5,1,30] 前两个表示卷积核的尺寸
# 第三个表示通道channel 第四个表示提取多少类特征
# strides 表示卷积模板移动的步长都是 1代表不遗漏的划过图片每一个点
# padding 表示边界处理方式这里的SAME代表给边界加上padding让输出和输入保持相同尺寸
def conv2d(x, W):
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
# ksize 使用2x2最大池化即将一个2x2像素块变为1x1 最大池化保持像素最高的点
# stride也横竖两个方向为2歩长,如果步长为1 得到尺寸不变的图片
def max_pool_2x2(x):
return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
# 定义张量流输入格式
# reshape变换张量shape 2维张量变4维 [None, 784] to [-1,28,28,1]
# -1表示样本数量不固定 28 28为尺寸 1为通道
x = tf.placeholder(tf.float32, [None, 784])
y_ = tf.placeholder(tf.float32, [None, 10])
x_image = tf.reshape(x, [-1, 28, 28, 1])
# 第一次卷积池化 卷积层用ReLU激活函数
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)
# 第二次卷积池化 卷积层用ReLU激活函数
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)
# 全连接层使用ReLU激活函数 reshape改变张量结构 变成一维
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)
# 为了减轻过拟合使用一个Dropout层
keep_prob = tf.placeholder(tf.float32)
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
# Dropout层 softmax连接输出层
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)
# loss函数
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y_conv), reduction_indices=[1]))
# 优化算法Adam函数
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
# accuracy函数
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()
# 训练20000次 每次大小为50的mini-batch 每100次训练查看训练结果 用以实时监测模型性能
for i in range(20000):
batch = mnist.train.next_batch(50)
if i % 100 == 0:
train_accuracy = accuracy.eval(feed_dict={x: batch[0], y_: batch[1], keep_prob: 1.0})
print("step %d, train_accuracy %g" % (i, train_accuracy))
train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})
print("test accuracy %g" % accuracy.eval(feed_dict={
x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0
}))
TensorFlow实现LeNet5模型的更多相关文章
- tensorflow实现LeNet-5模型
网络结构如下: INPUT: [28x28x1] weights: 0 CONV5-32: [28x28x32] weights: (5*5*1+1)*32 POOL2: [14x14x32] wei ...
- 81、Tensorflow实现LeNet-5模型,多层卷积层,识别mnist数据集
''' Created on 2017年4月22日 @author: weizhen ''' import os import tensorflow as tf import numpy as np ...
- 吴裕雄 python 神经网络——TensorFlow 实现LeNet-5模型处理MNIST手写数据集
import os import numpy as np import tensorflow as tf from tensorflow.examples.tutorials.mnist import ...
- 吴裕雄 python 神经网络TensorFlow实现LeNet模型处理手写数字识别MNIST数据集
import tensorflow as tf tf.reset_default_graph() # 配置神经网络的参数 INPUT_NODE = 784 OUTPUT_NODE = 10 IMAGE ...
- FaceRank-人脸打分基于 TensorFlow 的 CNN 模型
FaceRank-人脸打分基于 TensorFlow 的 CNN 模型 隐私 因为隐私问题,训练图片集并不提供,稍微可能会放一些卡通图片. 数据集 130张 128*128 张网络图片,图片名: 1- ...
- Tensorflow滑动平均模型tf.train.ExponentialMovingAverage解析
觉得有用的话,欢迎一起讨论相互学习~Follow Me 移动平均法相关知识 移动平均法又称滑动平均法.滑动平均模型法(Moving average,MA) 什么是移动平均法 移动平均法是用一组最近的实 ...
- tensorflow初次接触记录,我用python写的tensorflow第一个模型
tensorflow初次接触记录,我用python写的tensorflow第一个模型 刚用python写的tensorflow机器学习代码,训练60000张手写文字图片,多层神经网络学习拟合17000 ...
- tensorflow笔记:模型的保存与训练过程可视化
tensorflow笔记系列: (一) tensorflow笔记:流程,概念和简单代码注释 (二) tensorflow笔记:多层CNN代码分析 (三) tensorflow笔记:多层LSTM代码分析 ...
- 139、TensorFlow Serving 实现模型的部署(二) TextCnn文本分类模型
昨晚终于实现了Tensorflow模型的部署 使用TensorFlow Serving 1.使用Docker 获取Tensorflow Serving的镜像,Docker在国内的需要将镜像的Repos ...
随机推荐
- [Cypress] Test React’s Controlled Input with Cypress Selector Playground
React based applications often use controlled inputs, meaning the input event leads to the applicati ...
- 一见钟情Sublime
打开 preferences -> Setting-User,加入下面代码 { "font_size": 14, "ignored_packages": ...
- oracle删除日志文件
oracle删除日志文件 删除日志文件的语法例如以下: alter database drop logfile member logfile_name; 删除日志文件须要注意例如以下几点: 1.该日志 ...
- Maximal Rectangle [leetcode] 的三种思路
第一种方法是利用DP.时间复杂度是 O(m * m * n) dp(i,j):矩阵中同一行以(i,j)结尾的所有为1的最长子串长度 代码例如以下: int maximalRectangle(vecto ...
- Linux环境中Apache也就是httpd服务的启动,查看版本等操作
本机是虚拟机,装的redhat Linux版本,默认是安装了httpd的 打开terminal 切换到root用户 cd到/etc/rc.d/init.d/目录,并列出该目录下的所有文件,看看是否有h ...
- andoid电阻触摸移植
这里我使用的是210的开发板 系统Android4.0.4 内核linux3.0.8 要用电阻屏一般都是使用tslib进行校准的 这里给个我在android上用的tslib 下载地址 http://d ...
- Makefile中怎样调用python和perl文件为自己提供须要的数据
Makefile中怎样调用python和perl文件为自己提供须要的数据,利用print函数对外输出数据 实例代码例如以下 perl.pl #!/usr/bin/perl print("he ...
- luogu3197 [HNOI2008] 越狱
题目大意 已知序列$P$满足$|P|=N$,(以下所有$i,i\in[1,N]$)$\forall i, P_i\in [1,M]$.求$|\{P|\exists i, P_i =P_{i+1}\}| ...
- python 读取二进制文件 转为16进制输出
示例: #!/usr/bin/env python #encoding: utf-8 import binascii fh = open(r'C:\Temp\img\2012517165556.png ...
- BZOJ 4756 线段树合并(线段树)
思路: 1.最裸的线段树合并 2. 我们可以观察到子树求一个东西 那我们直接DFS序好了 入队的时候统计一下有多少比他大的 出的时候统计一下 减一下 搞定~ 线段树合并代码: //By SiriusR ...