tensorflow逻辑回归测试

来源:互联网 发布:js dom style属性 编辑:程序博客网 时间:2024/06/05 14:28
from __future__ import print_functionimport tensorflow as tffrom tensorflow.examples.tutorials.mnist import input_datamnist = input_data.read_data_sets("MNIST_data/", one_hot=True) #下载文件放到MNIST_data目录下print("train_img:",mnist.train.images.shape) #55000张训练图print("train_lbl:",mnist.train.labels.shape)print("test_img:",mnist.test.images.shape)  #10000张测试图print("test_lbl:",mnist.test.labels.shape)learning_rate = 0.01training_epochs = 50  batch_size = 100display_step = 1x = tf.placeholder(tf.float32, [None, 784]) # mnist中的训练图是28*28=784的图片y = tf.placeholder(tf.float32, [None, 10])  # 0-9 的数字 => 10 classesW = tf.Variable(tf.zeros([784, 10]))b = tf.Variable(tf.zeros([10]))predict = tf.nn.softmax(tf.matmul(x, W) + b) # Softmax逻辑回归模型cost = tf.reduce_mean(-tf.reduce_sum(y*tf.log(predict), reduction_indices=1))optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)init = tf.global_variables_initializer()with tf.Session() as sess:    sess.run(init)    for epoch in range(training_epochs):        avg_cost = 0.        total_batch = int(mnist.train.num_examples/batch_size)        for i in range(total_batch):            batch_xs, batch_ys = mnist.train.next_batch(batch_size)             _, c = sess.run([optimizer, cost], feed_dict = {x: batch_xs, y: batch_ys})            #上面表达式等价于下面方式:            # sess.run(optimizer, {x: batch_xs, y: batch_ys})            # c = sess.run(cost, {x: batch_xs, y: batch_ys})            avg_cost += c / total_batch                    if (epoch+1) % display_step == 0:            print("Epoch:", '%04d' % (epoch+1), "cost=", "{:.9f}".format(avg_cost))    print("Optimization Finished!")    correct_prediction = tf.equal(tf.argmax(predict, 1), tf.argmax(y, 1))    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))    print("Accuracy:", accuracy.eval({x: mnist.test.images, y: mnist.test.labels}))

结果:

Extracting MNIST_data/train-images-idx3-ubyte.gzExtracting MNIST_data/train-labels-idx1-ubyte.gzExtracting MNIST_data/t10k-images-idx3-ubyte.gzExtracting MNIST_data/t10k-labels-idx1-ubyte.gztrain_img: (55000, 784)train_lbl: (55000, 10)test_img: (10000, 784)test_lbl: (10000, 10)Epoch: 0001 cost= 1.176489531Epoch: 0002 cost= 0.662428102Epoch: 0003 cost= 0.550669022Epoch: 0004 cost= 0.496786994Epoch: 0005 cost= 0.463793346Epoch: 0006 cost= 0.440911430Epoch: 0007 cost= 0.423949281Epoch: 0008 cost= 0.410678503Epoch: 0009 cost= 0.399907770Epoch: 0010 cost= 0.390889884Epoch: 0011 cost= 0.383404499Epoch: 0012 cost= 0.376811127Epoch: 0013 cost= 0.371049813Epoch: 0014 cost= 0.365936547Epoch: 0015 cost= 0.361368383Epoch: 0016 cost= 0.357302430Epoch: 0017 cost= 0.353572087Epoch: 0018 cost= 0.350146192Epoch: 0019 cost= 0.347043392Epoch: 0020 cost= 0.344097957Epoch: 0021 cost= 0.341507307Epoch: 0022 cost= 0.339022189Epoch: 0023 cost= 0.336690402Epoch: 0024 cost= 0.334497478Epoch: 0025 cost= 0.332458832Epoch: 0026 cost= 0.330518939Epoch: 0027 cost= 0.328740775Epoch: 0028 cost= 0.326980677Epoch: 0029 cost= 0.325429160Epoch: 0030 cost= 0.323856879Epoch: 0031 cost= 0.322389204Epoch: 0032 cost= 0.321006620Epoch: 0033 cost= 0.319638948Epoch: 0034 cost= 0.318378163Epoch: 0035 cost= 0.317121298Epoch: 0036 cost= 0.315938809Epoch: 0037 cost= 0.314795836Epoch: 0038 cost= 0.313735532Epoch: 0039 cost= 0.312705021Epoch: 0040 cost= 0.311682237Epoch: 0041 cost= 0.310774488Epoch: 0042 cost= 0.309808256Epoch: 0043 cost= 0.308927144Epoch: 0044 cost= 0.308017197Epoch: 0045 cost= 0.307202091Epoch: 0046 cost= 0.306348794Epoch: 0047 cost= 0.305591306Epoch: 0048 cost= 0.304780430Epoch: 0049 cost= 0.304073534Epoch: 0050 cost= 0.303377406Optimization Finished!Accuracy: 0.9192


原创粉丝点击