转载请注明出处。
一、简介:
1、相比于第一个例程,在程序上做了优化,将特定功能以函数进行封装,独立可能修改的变量,使程序架构更清晰。加入了可视化、保存数据的功能。
2、模型
3、程序流程
程序流程.png4、可视化
封装视图方法:'with tf.name_scope('name'):'
收集数据:'tf.summary.scalar('name',name)','tf.summary.merge_all()'
在终端运行:tensorboard --logdir=/direction/
其中direction是log保存位置,对于fully_connected_feed.py来说,完整命令应是
tensorboard --logdir=/tmp/tensorflow/mnist/logs/fully_connected_feed/
完成后在浏览器输入http://127.0.1.1:6006,即可查看网络结构图,以及学习数据。
tensorboard.png
二、示例代码
源:/tensorflow/tensorflow/examples/tutorials/mnist/mnist.py
1.导入模块
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import math
import tensorflow as tf
2.系统参数,让程序更直观,方便修改
# The MNIST dataset has 10 classes, representing the digits 0 through 9.
NUM_CLASSES = 10
# The MNIST images are always 28x28 pixels.
IMAGE_SIZE = 28
IMAGE_PIXELS = IMAGE_SIZE * IMAGE_SIZE
3.神经网络图
输入:输入图片,隐藏层1神经元个数,隐藏层2神经元个数
输出:神经网络输出
def inference(images, hidden1_units, hidden2_units):
# Hidden 1
with tf.name_scope('hidden1'):
weights = tf.Variable(
tf.truncated_normal([IMAGE_PIXELS, hidden1_units],
stddev=1.0 / math.sqrt(float(IMAGE_PIXELS))),
name='weights')
biases = tf.Variable(tf.zeros([hidden1_units]),
name='biases')
hidden1 = tf.nn.relu(tf.matmul(images, weights) + biases)
# Hidden 2
with tf.name_scope('hidden2'):
weights = tf.Variable(
tf.truncated_normal([hidden1_units, hidden2_units],
stddev=1.0 / math.sqrt(float(hidden1_units))),
name='weights')
biases = tf.Variable(tf.zeros([hidden2_units]),
name='biases')
hidden2 = tf.nn.relu(tf.matmul(hidden1, weights) + biases)
# Linear
with tf.name_scope('softmax_linear'):
weights = tf.Variable(
tf.truncated_normal([hidden2_units, NUM_CLASSES],
stddev=1.0 / math.sqrt(float(hidden2_units))),
name='weights')
biases = tf.Variable(tf.zeros([NUM_CLASSES]),
name='biases')
logits = tf.matmul(hidden2, weights) + biases
return logits
4.输出损失计算方法
输入参数 logits:网络输出,为float类型,[batch_size,NUM_CLASSES]
labels:目标标签,为int32类型,[batch_size]
输出:损失,float类型
def loss(logits, labels):
labels = tf.to_int64(labels)
cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(
labels=labels, logits=logits, name='xentropy')
return tf.reduce_mean(cross_entropy, name='xentropy_mean')
5.训练方法
输入:损失,学习速率
输出:训练op
训练方法为梯度下降。
def training(loss, learning_rate):
# Add a scalar summary for the snapshot loss.
tf.summary.scalar('loss', loss)
# Create the gradient descent optimizer with the given learning rate.
optimizer = tf.train.GradientDescentOptimizer(learning_rate)
# Create a variable to track the global step.
global_step = tf.Variable(0, name='global_step', trainable=False)
# Use the optimizer to apply the gradients that minimize the loss
# (and also increment the global step counter) as a single training step.
train_op = optimizer.minimize(loss, global_step=global_step)
return train_op
6.评估训练效果
输入:logits:网络输出,float32,[batch_size, NUM_CLASSES]
labels:标签,int32,[batch_size]
输出:预测正确的数量
def evaluation(logits, labels):
correct = tf.nn.in_top_k(logits, labels, 1)
return tf.reduce_sum(tf.cast(correct, tf.int32))
源:/tensorflow/tensorflow/examples/tutorials/mnist/fully_connected_feed.py
1.导入模块
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
# pylint: disable=missing-docstring
import argparse
import os.path
import sys
import time
from six.moves import xrange # pylint: disable=redefined-builtin
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
from tensorflow.examples.tutorials.mnist import mnist
# Basic model parameters as external flags.
FLAGS = None
2.占位符
目的:产生图片及标签的占位符
输入:batch_size
输出:Images placehodler(float32),Labels placeholder(int32)
def placeholder_inputs(batch_size):
images_placeholder = tf.placeholder(tf.float32, shape=(batch_size,
mnist.IMAGE_PIXELS))
labels_placeholder = tf.placeholder(tf.int32, shape=(batch_size))
return images_placeholder, labels_placeholder
3.填充喂养字典
目的:在训练时对应次数自动填充字典
输入:数据源data_set,来自input_data.read_data_sets
图片holder,images_pl,来自placeholder_inputs()
标签holder,labels_pl,来自placeholder_inputs()
输出:喂养字典feed_dict.
def fill_feed_dict(data_set, images_pl, labels_pl)
images_feed, labels_feed = data_set.next_batch(FLAGS.batch_size,
FLAGS.fake_data)
feed_dict = {
images_pl: images_feed,
labels_pl: labels_feed,
}
return feed_dict
4.评估
目的:每循环1000次或结束进行一次评估。
输入:sess: 模型训练所使用的Session
eval_correct: 预测正确的样本数量
images_placeholder: images placeholder.
labels_placeholder: labels placeholder.
data_set: 图片和标签数据,来自input_data.read_data_sets().
输出:打印测试结果。
def do_eval(sess,
eval_correct,
images_placeholder,
labels_placeholder,
data_set):
true_count = 0 # 记录预测正确的数目。
steps_per_epoch = data_set.num_examples // FLAGS.batch_size
num_examples = steps_per_epoch * FLAGS.batch_size
for step in xrange(steps_per_epoch):
feed_dict = fill_feed_dict(data_set,
images_placeholder,
labels_placeholder)
true_count += sess.run(eval_correct, feed_dict=feed_dict)
precision = float(true_count) / num_examples
print(' Num examples: %d Num correct: %d Precision @ 1: %0.04f' %
(num_examples, true_count, precision))
5.训练过程
def run_training():
# 获取数据
data_sets = input_data.read_data_sets(FLAGS.input_data_dir, FLAGS.fake_data)
# 在默认Graph下运行.
with tf.Graph().as_default():
# 配置graph
images_placeholder, labels_placeholder = placeholder_inputs(
FLAGS.batch_size)
logits = mnist.inference(images_placeholder,
FLAGS.hidden1,
FLAGS.hidden2)
loss = mnist.loss(logits, labels_placeholder)
train_op = mnist.training(loss, FLAGS.learning_rate)
eval_correct = mnist.evaluation(logits, labels_placeholder)
# 汇聚tensor
summary = tf.summary.merge_all()
# 建立初始化机制
init = tf.global_variables_initializer()
# 建立保存机制
saver = tf.train.Saver()
# 建立Session
sess = tf.Session()
# 建立一个SummaryWriter输出汇聚的tensor
summary_writer = tf.summary.FileWriter(FLAGS.log_dir, sess.graph)
# 开始执行
# 执行变量
sess.run(init)
# 开始训练,2000次循环
for step in xrange(FLAGS.max_steps):
start_time = time.time()
#获取当次循环的数据
feed_dict = fill_feed_dict(data_sets.train,
images_placeholder,
labels_placeholder)
# 丢弃了train数据
_, loss_value = sess.run([train_op, loss],
feed_dict=feed_dict)
duration = time.time() - start_time
# 每训练100次输出当前损失,并记录数据
if step % 100 == 0:
print('Step %d: loss = %.2f (%.3f sec)' % (step, loss_value, duration))
summary_str = sess.run(summary, feed_dict=feed_dict)
summary_writer.add_summary(summary_str, step)
summary_writer.flush()
# 每1000次测试模型
if (step + 1) % 1000 == 0 or (step + 1) == FLAGS.max_steps:
checkpoint_file = os.path.join(FLAGS.log_dir, 'model.ckpt')
saver.save(sess, checkpoint_file, global_step=step)
# Evaluate against the training set.
print('Training Data Eval:')
do_eval(sess,
eval_correct,
images_placeholder,
labels_placeholder,
data_sets.train)
# Evaluate against the validation set.
print('Validation Data Eval:')
do_eval(sess,
eval_correct,
images_placeholder,
labels_placeholder,
data_sets.validation)
# Evaluate against the test set.
print('Test Data Eval:')
do_eval(sess,
eval_correct,
images_placeholder,
labels_placeholder,
data_sets.test)
def main(_):
if tf.gfile.Exists(FLAGS.log_dir):
tf.gfile.DeleteRecursively(FLAGS.log_dir)
tf.gfile.MakeDirs(FLAGS.log_dir)
run_training()
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument(
'--learning_rate',
type=float,
default=0.01,
help='Initial learning rate.'
)
parser.add_argument(
'--max_steps',
type=int,
default=2000,
help='Number of steps to run trainer.'
)
parser.add_argument(
'--hidden1',
type=int,
default=128,
help='Number of units in hidden layer 1.'
)
parser.add_argument(
'--hidden2',
type=int,
default=32,
help='Number of units in hidden layer 2.'
)
parser.add_argument(
'--batch_size',
type=int,
default=100,
help='Batch size. Must divide evenly into the dataset sizes.'
)
parser.add_argument(
'--input_data_dir',
type=str,
default='/tmp/tensorflow/mnist/input_data',
help='Directory to put the input data.'
)
parser.add_argument(
'--log_dir',
type=str,
default='/tmp/tensorflow/mnist/logs/fully_connected_feed',
help='Directory to put the log data.'
)
parser.add_argument(
'--fake_data',
default=False,
help='If true, uses fake data for unit testing.',
action='store_true'
)
FLAGS, unparsed = parser.parse_known_args()
tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)
三、API
1、tf.truncated_normal(shape,mean=0.0,stddev=1.0,dtype=tf.float32,seed=None,name=None)
说明:1、从截断正态分布中获取随机数,截断范围为2个标准差。
2、'mean'平均值,默认为0.0。
3、'stddev'标准差,默认为1.0。
2、tf.nn.in_top_k(predictions,targets,k,name=None)
说明:1、判断'targets'是否在前'k'个预测中,如targets在一号样本中为5,predictions预测一号样本结果概率最高的为5,
那么targets就在predictions的前1个样本中,返回True。
四、小结
并没有什么好说,进一步熟悉了tensorflow的使用。
网友评论