aihot  2017-12-02 08:57:09  机器学习 |   查看评论   

 逻辑回归

  1.  import tensorflow as tf
  2.  # Import MINST data
  3.  from tensorflow.examples.tutorials.mnist import input_data
  4.  mnist = input_data.read_data_sets("/tmp/data/", one_hot=True)

  5.  # Parameters
  6.  learning_rate = 0.01
  7.  training_epochs = 25
  8.  batch_size = 100
  9.  display_step = 1

  10.  # tf Graph Input
  11.  x = tf.placeholder(tf.float32, [None, 784]) # mnist data image of shape 28*28=784
  12.  y = tf.placeholder(tf.float32, [None, 10]) # 0-9 digits recognition => 10 classes

  13.  # Set model weights
  14.  W = tf.Variable(tf.zeros([784, 10]))
  15.  b = tf.Variable(tf.zeros([10]))

  16.  # Construct model
  17.  pred = tf.nn.softmax(tf.matmul(x, W) + b) # Softmax

  18.  # Minimize error using cross entropy
  19.  cost = tf.reduce_mean(-tf.reduce_sum(y*tf.log(pred), reduction_indices=1))
  20.  # Gradient Descent
  21.  optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)

  22.  # Initializing the variables
  23.  init = tf.initialize_all_variables()

  24.  # Launch the graph
  25.  with tf.Session() as sess:
  26.      sess.run(init)

  27.      # Training cycle
  28.      for epoch in range(training_epochs):
  29.          avg_cost = 0.
  30.          total_batch = int(mnist.train.num_examples/batch_size)
  31.          # Loop over all batches
  32.          for i in range(total_batch):
  33.              batch_xs, batch_ys = mnist.train.next_batch(batch_size)
  34.              # Run optimization op (backprop) and cost op (to get loss value)
  35.              _, c = sess.run([optimizer, cost], feed_dict={x: batch_xs,
  36.                                                            y: batch_ys})
  37.              # Compute average loss
  38.              avg_cost += c / total_batch
  39.          # Display logs per epoch step
  40.          if (epoch+1) % display_step == 0:
  41.              print "Epoch:", '%04d' % (epoch+1), "cost=", "{:.9f}".format(avg_cost)

  42.      print "Optimization Finished!"

  43.      # Test model
  44.      correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))
  45.      # Calculate accuracy
  46.      accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
  47.      print "Accuracy:", accuracy.eval({x: mnist.test.images, y: mnist.test.labels})

  48.      # result :
  49.      Epoch: 0001 cost= 29.860467369
  50.      Epoch: 0002 cost= 22.001451784
  51.      Epoch: 0003 cost= 21.019925554
  52.      Epoch: 0004 cost= 20.561320320
  53.      Epoch: 0005 cost= 20.109135756
  54.      Epoch: 0006 cost= 19.927862290
  55.      Epoch: 0007 cost= 19.548687116
  56.      Epoch: 0008 cost= 19.429119071
  57.      Epoch: 0009 cost= 19.397068211
  58.      Epoch: 0010 cost= 19.180813479
  59.      Epoch: 0011 cost= 19.026808132
  60.      Epoch: 0012 cost= 19.057875510
  61.      Epoch: 0013 cost= 19.009575057
  62.      Epoch: 0014 cost= 18.873240641
  63.      Epoch: 0015 cost= 18.718575359
  64.      Epoch: 0016 cost= 18.718761925
  65.      Epoch: 0017 cost= 18.673640560
  66.      Epoch: 0018 cost= 18.562128253
  67.      Epoch: 0019 cost= 18.458205289
  68.      Epoch: 0020 cost= 18.538211225
  69.      Epoch: 0021 cost= 18.443384213
  70.      Epoch: 0022 cost= 18.428727668
  71.      Epoch: 0023 cost= 18.304270616
  72.      Epoch: 0024 cost= 18.323529782
  73.      Epoch: 0025 cost= 18.247192113
  74.      Optimization Finished!
  75.      (10000, 784)
  76.      Accuracy 0.9206

 

  
 

除特别注明外,本站所有文章均为 人工智能学习网 原创,转载请注明出处来自机器学习进阶笔记之一 | TensorFlow安装与入门

留言与评论(共有 0 条评论)
   
验证码: