## TensorFlow搭建神经网络最佳实践

| |
[ 所属分类 开发（python） | 发布者 店小二04 | 时间 | 作者 红领巾 ] 0人收藏点击收藏

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu May 25 08:56:30 2017

@author: marsjhao
"""

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data

INPUT_NODE = 784 # 输入节点数
OUTPUT_NODE = 10 # 输出节点数
LAYER1_NODE = 500 # 隐含层节点数
BATCH_SIZE = 100
LEARNING_RETE_BASE = 0.8 # 基学习率
LEARNING_RETE_DECAY = 0.99 # 学习率的衰减率
REGULARIZATION_RATE = 0.0001 # 正则化项的权重系数
TRAINING_STEPS = 10000 # 迭代训练次数
MOVING_AVERAGE_DECAY = 0.99 # 滑动平均的衰减系数

# 传入神经网络的权重和偏置，计算神经网络前向传播的结果
def inference(input_tensor, avg_class, weights1, biases1, weights2, biases2):
# 判断是否传入ExponentialMovingAverage类对象
if avg_class == None:
layer1 = tf.nn.relu(tf.matmul(input_tensor, weights1) + biases1)
return tf.matmul(layer1, weights2) + biases2
else:
layer1 = tf.nn.relu(tf.matmul(input_tensor, avg_class.average(weights1)) + avg_class.average(biases1))
return tf.matmul(layer1, avg_class.average(weights2))\ + avg_class.average(biases2)

# 神经网络模型的训练过程
def train(mnist):
x = tf.placeholder(tf.float32, [None,INPUT_NODE], name='x-input')
y_ = tf.placeholder(tf.float32, [None, OUTPUT_NODE], name='y-input')

# 定义神经网络结构的参数
weights1 = tf.Variable(tf.truncated_normal([INPUT_NODE, LAYER1_NODE], stddev=0.1))
biases1 = tf.Variable(tf.constant(0.1, shape=[LAYER1_NODE]))
weights2 = tf.Variable(tf.truncated_normal([LAYER1_NODE, OUTPUT_NODE], stddev=0.1))
biases2 = tf.Variable(tf.constant(0.1, shape=[OUTPUT_NODE]))

# 计算非滑动平均模型下的参数的前向传播的结果
y = inference(x, None, weights1, biases1, weights2, biases2)

global_step = tf.Variable(0, trainable=False) # 定义存储当前迭代训练轮数的变量

# 定义ExponentialMovingAverage类对象
variable_averages = tf.train.ExponentialMovingAverage( MOVING_AVERAGE_DECAY, global_step) # 传入当前迭代轮数参数
# 定义对所有可训练变量trainable_variables进行更新滑动平均值的操作op
variables_averages_op = variable_averages.apply(tf.trainable_variables())

# 计算滑动模型下的参数的前向传播的结果
average_y = inference(x, variable_averages, weights1, biases1, weights2, biases2)

# 定义交叉熵损失值
cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(
logits=y, labels=tf.argmax(y_, 1))
cross_entropy_mean = tf.reduce_mean(cross_entropy)
# 定义L2正则化器并对weights1和weights2正则化
regularizer = tf.contrib.layers.l2_regularizer(REGULARIZATION_RATE)
regularization = regularizer(weights1) + regularizer(weights2)
loss = cross_entropy_mean + regularization # 总损失值

# 定义指数衰减学习率
learning_rate = tf.train.exponential_decay(LEARNING_RETE_BASE, global_step,
mnist.train.num_examples / BATCH_SIZE, LEARNING_RETE_DECAY)
# 定义梯度下降操作op，global_step参数可实现自加1运算
# 组合两个操作op
train_op = tf.group(train_step, variables_averages_op)
'''''
# 与tf.group()等价的语句
with tf.control_dependencies([train_step, variables_averages_op]):
train_op = tf.no_op(name='train')
'''
# 定义准确率
# 在最终预测的时候，神经网络的输出采用的是经过滑动平均的前向传播计算结果
correct_prediction = tf.equal(tf.argmax(average_y, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

# 初始化回话sess并开始迭代训练
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
# 验证集待喂入数据
validate_feed = {x: mnist.validation.images, y_: mnist.validation.labels}
# 测试集待喂入数据
test_feed = {x: mnist.test.images, y_: mnist.test.labels}
for i in range(TRAINING_STEPS):
if i % 1000 == 0:
validate_acc = sess.run(accuracy, feed_dict=validate_feed)
print('After %d training steps, validation accuracy'
' using average model is %f' % (i, validate_acc))
xs, ys = mnist.train.next_batch(BATCH_SIZE)
sess.run(train_op, feed_dict={x: xs, y_:ys})

test_acc = sess.run(accuracy, feed_dict=test_feed)
print('After %d training steps, test accuracy'
' using average model is %f' % (TRAINING_STEPS, test_acc))

# 主函数
def main(argv=None):
train(mnist)

# 当前的python文件是shell文件执行的入口文件，而非当做import的python module。
if __name__ == '__main__': # 在模块内部执行
tf.app.run() # 调用main函数并传入所需的参数list

1. 程序分析改进

2. 改进后程序设计

mnist_inference.py

mnist_train.py

mnist_eval.py

mnist_inference.py

import tensorflow as tf

INPUT_NODE = 784
OUTPUT_NODE = 10
LAYER1_NODE = 500

def get_weight_variable(shape, regularizer):
weights = tf.get_variable("weights", shape,
initializer=tf.truncated_normal_initializer(stddev=0.1))
if regularizer != None:
# 将权重参数的正则化项加入至损失集合
return weights

def inference(input_tensor, regularizer):
with tf.variable_scope('layer1'):
weights = get_weight_variable([INPUT_NODE, LAYER1_NODE], regularizer)
biases = tf.get_variable("biases", [LAYER1_NODE], initializer=tf.constant_initializer(0.0))
layer1 = tf.nn.relu(tf.matmul(input_tensor, weights) + biases)

with tf.variable_scope('layer2'):
weights = get_weight_variable([LAYER1_NODE, OUTPUT_NODE], regularizer)
biases = tf.get_variable("biases", [OUTPUT_NODE], initializer=tf.constant_initializer(0.0))
layer2 = tf.matmul(layer1, weights) + biases

return layer2

mnist_train.py

import os
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import mnist_inference

BATCH_SIZE = 100
LEARNING_RATE_BASE = 0.8
LEARNING_RATE_DECAY = 0.99
REGULARIZATION_RATE = 0.0001
TRAINING_STEPS = 10000
MOVING_AVERAGE_DECAY = 0.99

MODEL_SAVE_PATH = "Model_Folder/"
MODEL_NAME = "model.ckpt"

def train(mnist):
# 定义输入placeholder
x = tf.placeholder(tf.float32, [None, mnist_inference.INPUT_NODE], name='x-input')
y_ = tf.placeholder(tf.float32, [None, mnist_inference.OUTPUT_NODE], name='y-input')
# 定义正则化器及计算前向过程输出
regularizer = tf.contrib.layers.l2_regularizer(REGULARIZATION_RATE)
y = mnist_inference.inference(x, regularizer)
# 定义当前训练轮数及滑动平均模型
global_step = tf.Variable(0, trainable=False)
variable_averages = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY, global_step)
variables_averages_op = variable_averages.apply(tf.trainable_variables())
# 定义损失函数
cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=y, labels=tf.argmax(y_, 1))
cross_entropy_mean = tf.reduce_mean(cross_entropy)
# 定义指数衰减学习率
learning_rate = tf.train.exponential_decay(LEARNING_RATE_BASE, global_step,
mnist.train.num_examples / BATCH_SIZE, LEARNING_RATE_DECAY)
# 定义训练操作，包括模型训练及滑动模型操作
.minimize(loss, global_step=global_step)
train_op = tf.group(train_step, variables_averages_op)
# 定义Saver类对象，保存模型，TensorFlow持久化类
saver = tf.train.Saver()

# 定义会话，启动训练过程
with tf.Session() as sess:
tf.global_variables_initializer().run()

for i in range(TRAINING_STEPS):
xs, ys = mnist.train.next_batch(BATCH_SIZE)
_, loss_value, step = sess.run([train_op, loss, global_step], feed_dict={x: xs, y_: ys})
if i % 1000 == 0:
print("After %d training step(s), loss on training batch is %g."\ % (step, loss_value))
# save方法的global_step参数可以让每个被保存的模型的文件名末尾加上当前训练轮数
saver.save(sess, os.path.join(MODEL_SAVE_PATH, MODEL_NAME), global_step=global_step)

def main(argv=None):
train(mnist)

if __name__ == '__main__':
tf.app.run()
mnist_eval.py

import time
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import mnist_inference
import mnist_train

EVAL_INTERVAL_SECS = 10

def evaluate(mnist):
with tf.Graph().as_default() as g:
# 定义输入placeholder
x = tf.placeholder(tf.float32, [None, mnist_inference.INPUT_NODE], name='x-input')
y_ = tf.placeholder(tf.float32, [None, mnist_inference.OUTPUT_NODE], name='y-input')
# 定义feed字典
validate_feed = {x: mnist.validation.images, y_: mnist.validation.labels}
# 测试时不加参数正则化损失
y = mnist_inference.inference(x, None)
# 计算正确率
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
# 加载滑动平均模型下的参数值
variable_averages = tf.train.ExponentialMovingAverage( mnist_train.MOVING_AVERAGE_DECAY)
saver = tf.train.Saver(variable_averages.variables_to_restore())

# 每隔EVAL_INTERVAL_SECS秒启动一次会话
while True:
with tf.Session() as sess:
ckpt = tf.train.get_checkpoint_state(mnist_train.MODEL_SAVE_PATH)
if ckpt and ckpt.model_checkpoint_path:
saver.restore(sess, ckpt.model_checkpoint_path)
# 取checkpoint文件中的当前迭代轮数global_step
global_step = ckpt.model_checkpoint_path\ .split('/')[-1].split('-')[-1]
accuracy_score = sess.run(accuracy, feed_dict=validate_feed)
print("After %s training step(s), validation accuracy = %g"\ % (global_step, accuracy_score))

else:
print('No checkpoint file found')
return
time.sleep(EVAL_INTERVAL_SECS)

def main(argv=None):
evaluate(mnist)

if __name__ == '__main__':
tf.app.run()

tags: tf,mnist,train,step,NODE,global,inference,input,variable,op,regularizer,None,定义,神经,averages

1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责；
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性，不作出任何保证或承若；
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。