最近在学习深度学习,之前从图书馆借了本《TensorFlow 实战Google深度学习框架》草草的看了下,最近毕设需要从网上找了个PDF版的仔细啃了起来,发现这本书的代码有些错误,需要做小修改,否则报错运行不起来。可能是因为TensorFlow版本迭代的了原因,之前的一些函数方法都产生了变化。
在第五章,经典的深度学习入门例程:MNIST数字识别问题里,5.2.1里给出的TensorFlow训练神经网络完整例程里:
初始会话里的初始所有变量的代码
with tf.Session() as sess:
tf.initialize_all_variables().run()
需要修改为
with tf.Session() as sess:
tf.global_variables_initializer().run()
另外在def train(mnist):里的cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(y, tf.argmax(y_,1))
def train(mnist):
...
cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(y, tf.argmax(y_,1))
...
应修改为
cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=tf.argmax(y_, 1), logits=y)
书中将两个参数的位置对调了,导致产生错误
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).
另外还有一些很明显的拼写和排版错误。。。我就不吐槽指出了2333
最后
附上我修改后的完整代码
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()
另外,因为我的电脑没有独立的GPU,只能使用CPU进行计算,添加代码
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
可以避免出现如下的警告
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
这并不是最完美的解决方法,只是眼不见为净的解决方法而已。