import tensorflow as tfimport numpy as npimport mathimport tensorflow.examples.tutorials.mnist as mn sess = tf.InteractiveSession()mnist = mn.input_data.read_data_sets("E:\\Python35\\Lib\\site-packages\\tensorflow\\examples\\tutorials\\mnist\\MNIST_data",one_hot=True)x = tf.placeholder("float", shape=[None, 784])y_ = tf.placeholder("float", shape=[None, 10])W = tf.Variable(tf.zeros([784,10]))b = tf.Variable(tf.zeros([10])) sess.run(tf.global_variables_initializer())y = tf.nn.softmax(tf.matmul(x,W) + b)cross_entropy = -tf.reduce_sum(y_*tf.log(y))‘‘‘train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)‘‘‘ bestModel = NonebestPredict = 0.0bestIter = 0bestRate = 0.0bestSample = 0 iters = [1000,1200,1400]rates = [0.01,0.02]samples = [100,150,200]for iter in iters: for rate in rates: for sample in samples: train_step = tf.train.GradientDescentOptimizer(rate).minimize(cross_entropy) batch = mnist.train.next_batch(sample) model = train_step.run(feed_dict = {x:batch[0],y_:batch[1]}) correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float")) predict = accuracy.eval(feed_dict={x: mnist.test.images, y_: mnist.test.labels}) if predict > bestPredict: bestPredict = predict bestModel = model bestIter = iter bestRate = rate bestSample = sample print("bestRate:",bestRate,"bestIter:",bestIter,"bestSample:",bestSample) ‘‘‘for i in range(1000): batch = mnist.train.next_batch(200) train_step.run(feed_dict={x: batch[0], y_: batch[1]}) correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))print(accuracy.eval(feed_dict={x: mnist.test.images, y_: mnist.test.labels}))‘‘‘ def weight_variable(shape): initial = tf.truncated_normal(shape, stddev=0.1) return tf.Variable(initial)def bias_variable(shape): initial = tf.constant(0.1, shape=shape) return tf.Variable(initial)
时间: 2024-12-23 04:07:26