DL: 初试 tensorflow

一、TensorFlow安装

win 7 下安装 TensorFlow并不困难,cmd下 pip install TensorFlow即可

二、对于TensorFlow初学者,MNIST数据集试验就像是初学编程的“Hello,world”,第一次尝试并不困难,网上教程挺多。

参考博客: http://wiki.jikexueyuan.com/project/tensorflow-zh/tutorials/mnist_beginners.html

                   http://blog.csdn.net/sysstc/article/details/73468532

                  http://blog.csdn.net/sysstc/article/details/74910918

                  http://wiki.jikexueyuan.com/project/tensorflow-zh/tutorials/mnist_pros.html

#coding:utf-8
from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
import gzip
import os
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
mnist = input_data.read_data_sets("MNIST_data/",one_hot=True)
x = tf.placeholder(tf.float32,[None,784])
y_ = tf.placeholder(tf.float32,[None,10])
W = tf.Variable(tf.zeros([784,10]))
b=tf.Variable(tf.zeros([10]))
y = tf.nn.softmax(tf.matmul(x,W)+b)
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(labels=y_,logits=tf.log(y)))
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
init = tf.global_variables_initializer()
sess = tf.InteractiveSession()
sess.run(init)
for i in range(1000):
    batch_x,batch_y = mnist.train.next_batch(100)
    sess.run(train_step,feed_dict={x:batch_x,y_:batch_y})
correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction,"float"))
print(sess.run(accuracy,feed_dict={x:mnist.test.images,y_:mnist.test.labels}))
sess.close()

三、报错调试

可能会出现如下图所示错误

出现如图所示错误其实不要紧,因为程序已经正确运行,答案已经出来了,但是对于强迫症来说还是很难受,尤其是看到红色的bug,解决方法很简单,在头部加两句代码:

import os
os.environ['TF_CPP_MIN_LOG_LEVEL']='2' 
既然无论如何时间都会过去,为什么不选择做些有意义的事情呢
原文地址:https://www.cnblogs.com/xiaodongsuibi/p/8583950.html