from future import absolute_import
from future import division
from future import print_function
import math
import tensorflow.python.platform
import tensorflow as tf
NUM_CLASSES = 10
IMAGE_SIZE = 28
IMAGE_PIXELS = IMAGE_SIZE * IMAGE_SIZE
def inference(images, hidden1_units, hidden2_units):
“”“Build the MNIST model up to where it may be used for inference.
Args:
images: Images placeholder, from inputs().
hidden1_units: Size of the first hidden layer.
hidden2_units: Size of the second hidden layer.
Returns:
softmax_linear: Output tensor with the computed logits.
“””
# Hidden 1
# 表示该命令下的函数具有相同的前缀 名字 hidden1 ,用自带的函数构建权重
# 每一层所使用的权重和偏差都在tf.Variable实例中生成,并且包含了各自期望的shape,相比于,mnist_.deep.py
# 中 def 定义,复杂了一点,这里用with,主要是为了多一个命名空间,类似于局部变量,并且把权重偏差和输出,放一起
# 通过tf.truncated_normal函数初始化权重变量,tf.truncated_normal初始函数将根据所得到的均值和标准差,
# 生成一个随机分布
# 最后函数def 下的with都执行完,程序会返回包含了输出结果的logitsTensor
# 这也是网络模型,前向通道的计算单元。注意softmax,没有写在这里。之后的函数就是对这个模型的优化步骤了,
# 先定义一个损失函数loss,,然后就可以训练train,由损失函数的误差,作为输入的一部分,进行梯度的改变
# 正好,每一轮训练都是 经过这三个函数的步骤,前向计算 损失函数计算, 再进行反向传播参数的改变,
# 直到损失函数误差很小,达到了想要的精度
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
def loss(logits, labels):
“”“Calculates the loss from the logits and the labels.
Args:
logits: Logits tensor, float - [batch_size, NUM_CLASSES].
labels: Labels tensor, int32 - [batch_size].
Returns:
loss: Loss tensor of type float.
“””
# Convert from sparse integer labels in the range [0, NUM_CLASSES)
# to 1-hot dense float vectors (that is we will have batch_size vectors,
# each with NUM_CLASSES values, all of which are 0.0 except there will
# be a 1.0 in the entry corresponding to the label).
batch_size = tf.size(labels)
labels = tf.expand_dims(labels, 1)
indices = tf.expand_dims(tf.range(0, batch_size), 1)
concated = tf.concat(1, [indices, labels])
onehot_labels = tf.sparse_to_dense(
concated, tf.pack([batch_size, NUM_CLASSES]), 1.0, 0.0)
cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits,
onehot_labels,
name=’xentropy’)
loss = tf.reduce_mean(cross_entropy, name=’xentropy_mean’)
return loss
def training(loss, learning_rate):
“”“Sets up the training Ops.
Creates a summarizer to track the loss over time in TensorBoard.
Creates an optimizer and applies the gradients to all trainable variables.
The Op returned by this function is what must be passed to the
sess.run()
call to cause the model to train.
Args:
loss: Loss tensor, from loss().
learning_rate: The learning rate to use for gradient descent.
Returns:
train_op: The Op for training.
“””
# Add a scalar summary for the snapshot loss.
tf.scalar_summary(loss.op.name, 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
def evaluation(logits, labels):
“”“Evaluate the quality of the logits at predicting the label.
Args:
logits: Logits tensor, float - [batch_size, NUM_CLASSES].
labels: Labels tensor, int32 - [batch_size], with values in the
range [0, NUM_CLASSES).
Returns:
A scalar int32 tensor with the number of examples (out of batch_size)
that were predicted correctly.
“””
# For a classifier model, we can use the in_top_k Op.
# It returns a bool tensor with shape [batch_size] that is true for
# the examples where the label’s is was in the top k (here k=1)
# of all logits for that example.
correct = tf.nn.in_top_k(logits, labels, 1)
# Return the number of true entries.
return tf.reduce_sum(tf.cast(correct, tf.int32))