nextowang

# 最经典的LeNet模型

MNIST手写体识别的LeNet模型几乎已经成为深度学习玩家耳熟能详，妇孺皆知的模型。所以我们在这里简单的回顾一下，整个模型层次结构如下：

• input输入层
• 卷积核为5x5的conv2d卷积层
• 2x2的Maxpooling池化层
• 卷积核为5x5的conv2d卷积层
• 2x2的Maxpooling池化层
• 1024的full-connect全连接层
• output输出层

MNIST数据集特征张量通常都是（X，28，28，1），那么由上述结构我们可以写出对应的Keras代码，如下代码所示：

``````from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D

model = Sequential()

Keras框架是一个封装好的深度学习框架，但是Tensorflow框架相对于Keras框架更为底层一些，所以搭建过程相对复杂一些，笔者就直接用Tensorflow官方为我们提供的较为具有公信力的代码mnist_with_summaries.py

``````import tensorflow as tf
import numpy as np

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

def variable_summaries(var):
"""Attach a lot of summaries to a Tensor (for TensorBoard visualization)."""
with tf.name_scope('summaries'):
mean = tf.reduce_mean(var)
tf.summary.scalar('mean', mean)
with tf.name_scope('stddev'):
stddev = tf.sqrt(tf.reduce_mean(tf.square(var - mean)))
tf.summary.scalar('stddev', stddev)
tf.summary.scalar('max', tf.reduce_max(var))
tf.summary.scalar('min', tf.reduce_min(var))
tf.summary.histogram('histogram', var)

def add_layer(input_tensor, weights_shape, biases_shape, layer_name, act = tf.nn.relu, flag = 1):
"""Reusable code for making a simple neural net layer.

It does a matrix multiply, bias add, and then uses relu to nonlinearize.
It also sets up name scoping so that the resultant graph is easy to read,
and adds a number of summary ops.
"""
with tf.name_scope(layer_name):
with tf.name_scope('weights'):
weights = weight_variable(weights_shape)
variable_summaries(weights)
with tf.name_scope('biases'):
biases = bias_variable(biases_shape)
variable_summaries(biases)
with tf.name_scope('Wx_plus_b'):
if flag == 1:
else:
tf.summary.histogram('pre_activations', preactivate)
if act == None:
outputs = preactivate
else:
outputs = act(preactivate, name = 'activation')
tf.summary.histogram('activation', outputs)
return outputs

with tf.name_scope('Input'):
x = tf.placeholder(tf.float32, [None, 28*28], name = 'input_x')
y_ = tf.placeholder(tf.float32, [None, 10], name = 'target_y')

# First Convolutional Layer
x_image = tf.reshape(x, [-1, 28, 28 ,1])
conv_1 = add_layer(x_image, [5, 5, 1, 32], [32], 'First_Convolutional_Layer', flag = 1)

# First Pooling Layer
pool_1 = max_pool_2x2(conv_1)

# Second Convolutional Layer
conv_2 = add_layer(pool_1, [5, 5, 32, 64], [64], 'Second_Convolutional_Layer', flag = 1)

# Second Pooling Layer
pool_2 = max_pool_2x2(conv_2)

# Densely Connected Layer
pool_2_flat = tf.reshape(pool_2, [-1, 7*7*64])
dc_1 = add_layer(pool_2_flat, [7*7*64, 1024], [1024], 'Densely_Connected_Layer', flag = 0)

# Dropout
keep_prob = tf.placeholder(tf.float32)
dc_1_drop = tf.nn.dropout(dc_1, keep_prob)

# Keras模型参数

10250 = 1024×10+1×10

832=（5×5×1+1）×32

# Tensorflow内存参数

``````from tensorflow.python.platform import gfile
from tensorflow.examples.tutorials.mnist import input_data

import numpy as np
import tensorflow as tf
import mnist_inference

mnist_data_path = 'MNIST_data/'
mnist = input_data.read_data_sets(mnist_data_path, one_hot = True)
batch_xs, batch_ys = mnist.test.images[:1], mnist.test.labels[:1]
print batch_ys.shape

with tf.Session() as sess:
with tf.device('/cpu:0'):

with gfile.FastGFile('LeNet_mnist.pb','rb') as f:
graph_def = tf.GraphDef()
sess.run(tf.global_variables_initializer())
tf.import_graph_def(graph_def, name='')

input_tensor = sess.graph.get_tensor_by_name('input:0')
output = sess.graph.get_tensor_by_name('layer6-fc2/output:0')

y_ = tf.placeholder(tf.float32,[None,10], name = 'target')
drop_rate = sess.graph.get_tensor_by_name('dropout:0')
correct_prediction = tf.equal(tf.argmax(output,1),tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
tf.summary.scalar('accuracy', accuracy)
merged = tf.summary.merge_all()

test_writer = tf.summary.FileWriter('pbtest', sess.graph)
run_options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE)
_, test_accuracy = sess.run([merged, accuracy], feed_dict={input_tensor: batch_xs, y_: batch_ys, drop_rate:1.0},
options=run_options,
test_writer.close()
``````

98KB=98×1024B=28×28×32×4B

49KB=49×1024B=14×14×64×4B

# 还想再聊聊

nextowang@stu.xidian.edu.cn

### 评论(0)

keras 或 tensorflow 调用GPU报错：Blas GEMM launch failed

GPU版的tensorflow在模型训练时遇到Blas GEMM launch failed错误，或者keras遇到相同错误（keras 一般将tensorflow作为backend，如果安装了GPU版本的tensorflow，那么在使用keras时会优先使用...

osc_0cqshtia
2019/09/24
6
0
Tensorflow 2.0 轻松实现迁移学习

image from unsplash by Gábor Juhász 迁移学习即利用已有的知识来学习新的知识，与人类类似，比如你学会了用笔画画，也就可以学习用笔来画画，并不用从头学习握笔的姿势。对于机器学习来说...

Hongtao洪滔
2019/08/20
0
0
tensorflow2.0第2章 Tensorflow keras实战

osc_56wm84nt
01/02
9
0

osc_xopfh3w8
2019/05/09
15
0

2018/11/19
0
0

SQL Server 并发控制 第三篇：隔离级别和行版本（2）

SQL Server 并发控制 第一篇：并发模式和事务 SQL Server 并发控制 第二篇：隔离级别和锁（1） SQL Server 并发控制 第三篇：隔离级别和行版本（2） 隔离级别定义事务处理数据读取操作的隔离...

osc_x5cptzgg
40分钟前
21
0
[Deprecation] Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimen...

osc_cxi2ewsx
41分钟前
29
0
python pip install指定国内源镜像

有时候安装一些依赖包，网不好，直接超时，或者这个包就是死都下不下来的时候，可以指定国内源镜像。 　　pip install -i 国内镜像地址 包名 　　e.g. pip install -i http://mirrors.al...

osc_ym1l2qni
42分钟前
19
0
uniapp打包发版到linux服务器步骤----H5端

osc_2sv5yx4m
43分钟前
23
0
.NET Core微服务之基于Ocelot实现API网关服务（续）

Tip: 此篇已加入.NET Core微服务基础系列文章索引 一、负载均衡与请求缓存 1.1 负载均衡 　　为了验证负载均衡，这里我们配置了两个Consul Client节点，其中ClientService分别部署于这两个节...

osc_ho8dcqsx
44分钟前
27
0