Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- با سلام و احترام...
- در برنامه MNIST For ML Beginners به صورت زیر:
- from tensorflow.examples.tutorials.mnist import input_data
- mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
- import tensorflow as tf
- sess = tf.InteractiveSession()
- x = tf.placeholder(tf.float32, shape=[None, 784])
- y_ = tf.placeholder(tf.float32, shape=[None, 10])
- W = tf.Variable(tf.zeros([784,10]))
- b = tf.Variable(tf.zeros([10]))
- sess.run(tf.global_variables_initializer())
- y = tf.matmul(x,W) + b
- cross_entropy = tf.reduce_mean(
- tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y))
- train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
- for _ in range(1000):
- batch = mnist.train.next_batch(100)
- 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, tf.float32))
- print(accuracy.eval(feed_dict={x: mnist.test.images, y_: mnist.test.labels}))
- اگر بخواهیم به جای دادگان فوق تصاویر مثلا 3*20*20 را جایگزین کنیم:
- تعریف داده آموزش training _data با برچسب train_label به صورت زیر:
- import tensorflow as tf
- import numpy as np
- import matplotlib.pyplot as plt
- try:
- from scipy import misc
- except ImportError:
- !pip install scipy
- from scipy import misc
- training_size = 9
- img_size = 20*20*3
- training_data = np.empty(shape=(training_size, img_size))
- import glob
- i = 0
- for filename in glob.glob('D:/Minutia/*.jpg'):
- image = misc.imread(filename)
- training_data[i] = image.reshape(-1)
- i+=1
- print(training_data[0].shape)
- a = [0, 0, 0,1,1,1,2,2,2]
- train_label = tf.one_hot(a,3)
- sess = tf.Session()
- sess.run(train_label)
- test_size = 3
- img_size = 20*20*3
- test_data = np.empty(shape=(test_size, img_size))
- import glob
- i = 0
- for filename in glob.glob('D:/Wrinkle/*.jpg'):
- image = misc.imread(filename)
- test_data[i] = image.reshape(-1)
- i+=1
- print(test_data[0].shape)
- a = [0, 1,2]
- test_label = tf.one_hot(a,3)
- sess = tf.Session()
- sess.run(test_label)
- x = tf.placeholder(tf.float32, shape=[None, 400])
- y_ = tf.placeholder(tf.float32, shape=[None, 3])
- W = tf.Variable(tf.zeros([400,3]))
- b = tf.Variable(tf.zeros([3]))
- sess.run(tf.global_variables_initializer())
- y = tf.matmul(x,W) + b
- cross_entropy = tf.reduce_mean(
- tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y))
- train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
- for _ in range(1000):
- batch = training_data.next_batch(100)
- 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, tf.float32))
- print(accuracy.eval(feed_dict={x: test_data, y_: test_label}))
- آیا برنامه فوق درست است؟من موقع اجرا با خطای زیر مواجه می شم...
- 'numpy.ndarray' object has no attribute 'next_batch'
Advertisement
Add Comment
Please, Sign In to add comment