tensorflow30《TensorFlow实战》笔记-03 TensorFlow第一步 code

来源:互联网 发布:天津大学软件下载 编辑:程序博客网 时间:2024/06/03 21:51

《TensorFlow实战》给出了很多论文和源码链接地址
InteractiveSession用法

# 《TensorFlow实战》03 TensorFlow第一步# win10 Tensorflow1.0.1 python3.5.3# CUDA v8.0 cudnn-8.0-windows10-x64-v5.1# filename:sz03.01.py # 神经网络#  https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/tutorials/mnist/mnist_softmax.py# 关于mnist的一些测试用例,可以参看 tensorflow\tensorflow\examples\tutorials\mnist 目录内容import tensorflow as tffrom tensorflow.examples.tutorials.mnist import input_datamnist = input_data.read_data_sets("MNIST_data/", one_hot=True)print(mnist.train.images.shape, mnist.train.labels.shape)print(mnist.test.images.shape, mnist.test.labels.shape)print(mnist.validation.images.shape, mnist.validation.labels.shape)x = tf.placeholder(tf.float32, [None, 784])W = tf.Variable(tf.zeros([784, 10]))b = tf.Variable(tf.zeros([10]))y = tf.nn.softmax(tf.matmul(x, W) + b)y_ = tf.placeholder(tf.float32, [None, 10])cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)sess = tf.InteractiveSession()tf.global_variables_initializer().run()for _ in range(1000):    batch_xs, batch_ys = mnist.train.next_batch(100)    #sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})    train_step.run({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}))    print(accuracy.eval({x: mnist.test.images,  y_: mnist.test.labels}))'''(55000, 784) (55000, 10)(10000, 784) (10000, 10)(5000, 784) (5000, 10)0.40750.40290.4927...0.9070.91750.91850.91880.91830.91940.9172'''
0 0