MNIST的数据文件路径设置问题:
from __future__ import absolute_import #from 语句必须写在程序开头,这三句在下面程序里可有可无 from __future__ import division from __future__ import print_function import os os.environ['TF_CPP_MIN_LOG_LEVEL']='2' from tensorflow.examples.tutorials.mnist import input_data import tensorflow as tf #加载数据, FLAGS = None mnist = input_data.read_data_sets('FlAGS.data_dir', one_hot=True)
"FlAGS.data_dir" 是MNIST的所在路径,但在windows10下的PythonCharm里却报红叉,经过查找,将它改为
“MNIST_data/“相对路径就可以了,程序会自动寻找该数据文件的所在路径。下面是正常可运行的代码
import os os.environ['TF_CPP_MIN_LOG_LEVEL']='2' from tensorflow.examples.tutorials.mnist import input_data import tensorflow as tf #加载数据, FLAGS = None mnist = input_data.read_data_sets('MNIST_data/', one_hot=True) #定义回归模型 x = tf.placeholder(tf.float32, [None, 784]) w = tf.Variable(tf.zeros([784, 10])) b = tf.Variable(tf.zeros([10])) y = tf.matmul(x, w) + b #定义损失函数和优化器 y_= tf.placeholder(tf.float32, [None, 10]) cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=y, labels=y_)) train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy) #训练模型 sess = tf.InteractiveSession() tf.global_variables_initializer().run() for _ in range(1000): #循环训练1000次 batch_xs, batch_ys = mnist.train.next_batch(100) sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys}) correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))
后台运行结果
C:\Python\Python35\python.exe D:/PythonCharm/MINIST
Extracting MNIST_data/train-images-idx3-ubyte.gz
Extracting MNIST_data/train-labels-idx1-ubyte.gz
Extracting MNIST_data/t10k-images-idx3-ubyte.gz
Extracting MNIST_data/t10k-labels-idx1-ubyte.gz
Extracting MNIST_data/train-images-idx3-ubyte.gz
Extracting MNIST_data/train-labels-idx1-ubyte.gz
Extracting MNIST_data/t10k-images-idx3-ubyte.gz
Extracting MNIST_data/t10k-labels-idx1-ubyte.gz
0.9191
Process finished with exit code 0
在window10 系统下用tensorflow框架处理MNIST的分类问题尝试出现了以下警告(
warning)
WARNING:tensorflow:From D:/PythonCharm/MINIST:17: softmax_cross_entropy_with_logits (from tensorflow.python.ops.nn_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Instructions for updating:
Future major versions of TensorFlow will allow gradients to flow
into the labels input on backprop by default.
into the labels input on backprop by default.
See tf.nn.softmax_cross_entropy_with_logits_v2.
中文意思是 提示你
tensorflow.python.ops.nn_ops 中的softmax_cross_entropy_with_logits方法已弃用,并将在以后的版本中删除
更新说明:TensorFlow未来的主要版本将在默认情况下允许梯度值流入到后向传播标签输入。具体请参阅官方的 tf.nn.softmax_cross_entropy_with_logits_v2。所以就是说当前这个方法可行但以后更新框架版本时,该方法会取消掉。并不影响当前框架下的运用。