MIzadpanah

Untitled

Apr 26th, 2017
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. با سلام و احترام...
  2. در برنامه MNIST For ML Beginners به صورت زیر:
  3.  
  4.  
  5.  
  6.  
  7. from tensorflow.examples.tutorials.mnist import input_data
  8. mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
  9. import tensorflow as tf
  10. sess = tf.InteractiveSession()
  11.  
  12. x = tf.placeholder(tf.float32, shape=[None, 784])
  13. y_ = tf.placeholder(tf.float32, shape=[None, 10])
  14.  
  15. W = tf.Variable(tf.zeros([784,10]))
  16. b = tf.Variable(tf.zeros([10]))
  17.  
  18. sess.run(tf.global_variables_initializer())
  19.  
  20. y = tf.matmul(x,W) + b
  21.  
  22. cross_entropy = tf.reduce_mean(
  23. tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y))
  24.  
  25. train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
  26.  
  27. for _ in range(1000):
  28. batch = mnist.train.next_batch(100)
  29. train_step.run(feed_dict={x: batch[0], y_: batch[1]})
  30.  
  31. correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
  32. accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
  33.  
  34. print(accuracy.eval(feed_dict={x: mnist.test.images, y_: mnist.test.labels}))
  35.  
  36.  
  37.  
  38. اگر بخواهیم به جای دادگان فوق تصاویر مثلا 3*20*20 را جایگزین کنیم:
  39.  
  40.  
  41. تعریف داده آموزش training _data با برچسب train_label به صورت زیر:
  42.  
  43. import tensorflow as tf
  44. import numpy as np
  45. import matplotlib.pyplot as plt
  46. try:
  47. from scipy import misc
  48. except ImportError:
  49. !pip install scipy
  50. from scipy import misc
  51.  
  52. training_size = 9
  53. img_size = 20*20*3
  54. training_data = np.empty(shape=(training_size, img_size))
  55.  
  56. import glob
  57. i = 0
  58. for filename in glob.glob('D:/Minutia/*.jpg'):
  59. image = misc.imread(filename)
  60. training_data[i] = image.reshape(-1)
  61. i+=1
  62. print(training_data[0].shape)
  63.  
  64. a = [0, 0, 0,1,1,1,2,2,2]
  65. train_label = tf.one_hot(a,3)
  66. sess = tf.Session()
  67. sess.run(train_label)
  68.  
  69.  
  70.  
  71. test_size = 3
  72. img_size = 20*20*3
  73. test_data = np.empty(shape=(test_size, img_size))
  74.  
  75. import glob
  76. i = 0
  77. for filename in glob.glob('D:/Wrinkle/*.jpg'):
  78. image = misc.imread(filename)
  79. test_data[i] = image.reshape(-1)
  80. i+=1
  81. print(test_data[0].shape)
  82.  
  83. a = [0, 1,2]
  84. test_label = tf.one_hot(a,3)
  85. sess = tf.Session()
  86. sess.run(test_label)
  87.  
  88.  
  89. x = tf.placeholder(tf.float32, shape=[None, 400])
  90. y_ = tf.placeholder(tf.float32, shape=[None, 3])
  91.  
  92. W = tf.Variable(tf.zeros([400,3]))
  93. b = tf.Variable(tf.zeros([3]))
  94.  
  95. sess.run(tf.global_variables_initializer())
  96.  
  97. y = tf.matmul(x,W) + b
  98.  
  99.  
  100.  
  101.  
  102.  
  103. cross_entropy = tf.reduce_mean(
  104. tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y))
  105.  
  106. train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
  107.  
  108. for _ in range(1000):
  109. batch = training_data.next_batch(100)
  110. train_step.run(feed_dict={x: batch[0], y_: batch[1]})
  111.  
  112. correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
  113. accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
  114.  
  115. print(accuracy.eval(feed_dict={x: test_data, y_: test_label}))
  116.  
  117.  
  118.  
  119.  
  120. آیا برنامه فوق درست است؟من موقع اجرا با خطای زیر مواجه می شم...
  121.  
  122. 'numpy.ndarray' object has no attribute 'next_batch'
Advertisement
Add Comment
Please, Sign In to add comment