Introduction to TensorFlow (1) correction of some small mistakes

Posted by deveed on Fri, 03 Apr 2020 22:09:01 +0200

Recently, in-depth learning, I borrowed the TensorFlow practical Google in-depth learning framework from the library and looked at it curtly. Recently, Bi set needs to find a PDF version on the Internet and chew it carefully. I found that there are some errors in the code of this book, which need to be slightly modified. Otherwise, it will fail to run. Maybe it is because of the iteration of TensorFlow version that some previous function methods have changed.
In Chapter 5, the classic in-depth learning entry routine: MNIST number recognition, in the TensorFlow training neural network complete routine given in 5.2.1:

Code for all initial variables in the initial session

    with tf.Session() as sess:
        tf.initialize_all_variables().run()

Need to change to

    with tf.Session() as sess:
        tf.global_variables_initializer().run()

In addition, in def train(mnist): cross

def train(mnist):
...
cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(y, tf.argmax(y_,1))
...

Should be amended to

cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=tf.argmax(y_, 1), logits=y)

In the book, the positions of the two parameters are reversed, resulting in errors

line 1875, in sparse_softmax_cross_entropy_with_logits
    (labels_static_shape.ndims, logits.get_shape().ndims))
ValueError: Rank mismatch: Rank of labels (received 2) should equal rank of logits minus 1 (received 1).

There are also some obvious spelling and typography mistakes... I did not make complaints about 2333.

Last

Attach my revised full code

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import argparse
import sys
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

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

FLAGS = None


Input_Node = 784
Output_Node = 10

Layer1_Node = 500

Batch_size = 100

Learning_rate_base = 0.8
Learning_rate_decay = 0.99
Regularization_rate = 0.0001
Training_steps = 30000
Moving_average_decay = 0.99

def inference(input_tensor, avg_class, weights1, biases1, weights2, biases2):
    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, shape=[None, Input_Node], name='x-input')
    y_ = tf.placeholder(tf.float32, shape=[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)

    variable_averages = tf.train.ExponentialMovingAverage(Moving_average_decay, global_step)

    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(labels=tf.argmax(y_, 1), logits=y)
    #cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=y, logits=tf.argmax(y_, 1))
    cross_entropy_mean = tf.reduce_mean(cross_entropy)

    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_rate_base, global_step, mnist.train.num_examples/Batch_size, Learning_rate_decay)

    train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_step)

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

    with tf.Session() as sess:
        tf.global_variables_initializer().run()

        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 step(s), validation accuracy "
                      "using average model is %g" % (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 step(s), test accuracy using average"
                  "model is %g" % (Training_steps, test_acc))

'''
def main(_):
    mnist = input_data.read_data_sets("/data", one_hot=True)
    train(mnist)

if __name__ == '__main__':
  parser = argparse.ArgumentParser()
  parser.add_argument(
      '--data_dir',
      type=str,
      default='/tmp/tensorflow/mnist/input_data',
      help='Directory for storing input data')
  FLAGS, unparsed = parser.parse_known_args()
  tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)
'''

def main(argv=None):
    mnist = input_data.read_data_sets("/data", one_hot=True)
    train(mnist)

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

In addition, because my computer does not have a separate GPU, I can only use the CPU to calculate and add code

import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

The following warnings can be avoided

2018-03-23 22:43:44.983594: I C:\tf_jenkins\home\workspace\rel-win\M\windows\PY\35\tensorflow\core\platform\cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2

It's not the perfect solution, it's just an invisible solution.

Topics: Session Google network Windows