Advertisement
Guest User

mnist

a guest
Nov 20th, 2017
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.04 KB | None | 0 0
  1. # Copyright 2015 The TensorFlow Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. #     http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ==============================================================================
  15.  
  16. # Norbert Batfai, 27 Nov 2016
  17. # Some modifications and additions to the original code:
  18. # https://github.com/tensorflow/tensorflow/blob/r0.11/tensorflow/examples/tutorials/mnist/mnist_softmax.py
  19. # See also http://progpater.blog.hu/2016/11/13/hello_samu_a_tensorflow-bol
  20. # ==============================================================================
  21.  
  22. """A very simple MNIST classifier.
  23.  
  24. See extensive documentation at
  25. http://tensorflow.org/tutorials/mnist/beginners/index.md
  26. """
  27. from __future__ import absolute_import
  28. from __future__ import division
  29. from __future__ import print_function
  30.  
  31. import argparse
  32.  
  33. # Import data
  34. from tensorflow.examples.tutorials.mnist import input_data
  35.  
  36. import tensorflow as tf
  37.  
  38. import matplotlib.pyplot
  39.  
  40.  
  41. FLAGS = None
  42.  
  43.  
  44. def readimg():
  45.     file = tf.read_file("sajat8a.png")
  46.     img = tf.image.decode_png(file)
  47.     return img
  48.  
  49. def main(_):
  50.   mnist = input_data.read_data_sets(FLAGS.data_dir, one_hot=True)
  51.  
  52.   # Create the model
  53.   x = tf.placeholder(tf.float32, [None, 784])
  54.   W = tf.Variable(tf.zeros([784, 10]))
  55.   b = tf.Variable(tf.zeros([10]))
  56.   y = tf.matmul(x, W) + b
  57.  
  58.   # Define loss and optimizer
  59.   y_ = tf.placeholder(tf.float32, [None, 10])
  60.  
  61.   # The raw formulation of cross-entropy,
  62.   #
  63.   #   tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(tf.nn.softmax(y)),
  64.   #                                 reduction_indices=[1]))
  65.   #
  66.   # can be numerically unstable.
  67.   #
  68.   # So here we use tf.nn.softmax_cross_entropy_with_logits on the raw
  69.   # outputs of 'y', and then average across the batch.
  70.   cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(y, y_))
  71.   train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
  72.  
  73.   sess = tf.InteractiveSession()
  74.   # Train
  75.   tf.initialize_all_variables().run()
  76.   print("-- A halozat tanitasa")  
  77.   for i in range(1000):
  78.     batch_xs, batch_ys = mnist.train.next_batch(100)
  79.     sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})
  80.     if i % 100 == 0:
  81.       print(i/10, "%")
  82.   print("----------------------------------------------------------")
  83.  
  84.   # Test trained model
  85.   print("-- A halozat tesztelese")  
  86.   correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
  87.   accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))  
  88.   print("-- Pontossag: ", sess.run(accuracy, feed_dict={x: mnist.test.images,
  89.                                       y_: mnist.test.labels}))
  90.   print("----------------------------------------------------------")
  91.  
  92.   writer = tf.train.SummaryWriter("/tmp/mnist_softmax_UDPROG61", sess.graph)
  93.  
  94.   img = W[:,0]
  95.   image = img.eval()
  96.   print("-- W_0[14,14] =", image[14*28+14])
  97.   print("-- Egy vedesi reszfeladat az elso heten: a W_0 sulyok abrazolasa, mutatom, a tovabblepeshez csukd be az ablakat")
  98.   #matplotlib.pyplot.imshow(image.reshape(28, 28))#, cmap=matplotlib.pyplot.cm.binary)
  99.   matplotlib.pyplot.imshow(image.reshape(28, 28), cmap="hot")
  100.   matplotlib.pyplot.savefig("w0.png")  
  101.   matplotlib.pyplot.show()
  102.   print("----------------------------------------------------------")
  103.  
  104.   print("-- A MNIST 42. tesztkepenek felismerese, mutatom a szamot, a tovabblepeshez csukd be az ablakat")
  105.  
  106.   img = mnist.test.images[42]
  107.   image = img
  108.  
  109.   matplotlib.pyplot.imshow(image.reshape(28, 28), cmap=matplotlib.pyplot.cm.binary)
  110.   matplotlib.pyplot.savefig("4.png")  
  111.   matplotlib.pyplot.show()
  112.  
  113.   classification = sess.run(tf.argmax(y, 1), feed_dict={x: [image]})
  114.  
  115.   print("-- Ezt a halozat ennek ismeri fel: ", classification[0])
  116.   print("----------------------------------------------------------")
  117.  
  118.   print("-- A sajat kezi 8-asom felismerese, mutatom a szamot, a tovabblepeshez csukd be az ablakat")
  119.  
  120.   img = readimg()
  121.   image = img.eval()
  122.   image = image.reshape(28*28)
  123.  
  124.   matplotlib.pyplot.imshow(image.reshape(28, 28), cmap=matplotlib.pyplot.cm.binary)
  125.   matplotlib.pyplot.savefig("8.png")  
  126.   matplotlib.pyplot.show()
  127.  
  128.   classification = sess.run(tf.argmax(y, 1), feed_dict={x: [image]})
  129.  
  130.   print("-- Ezt a halozat ennek ismeri fel: ", classification[0])
  131.   print("----------------------------------------------------------")
  132.  
  133. if __name__ == '__main__':
  134.   parser = argparse.ArgumentParser()
  135.   parser.add_argument('--data_dir', type=str, default='/tmp/tensorflow/mnist/input_data',
  136.                       help='Directory for storing input data')
  137.   FLAGS = parser.parse_args()
  138.   tf.app.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement