""" .................................................... Számjegyfelismerés perceptronnal (MNIST, tensorflow) .................................................... """ print(__doc__) import tensorflow as tf import numpy as np from tensorflow.examples.tutorials.mnist import input_data mnist = input_data.read_data_sets('MNIST_data', one_hot=True) #print(mnist.train.images.shape) #print(mnist.train.labels.shape) #print(mnist.validation.images.shape) #print(mnist.validation.labels.shape) #print(mnist.test.images.shape) #print(mnist.test.labels.shape) # placeholders for inputs x = tf.placeholder(tf.float32, shape=[None,784]) # input images placeholder d = tf.placeholder(tf.float32, shape=[None,10]) # labels placeholder W = tf.Variable(tf.zeros([784,10])) # súlyok b = tf.Variable(tf.zeros([10])) # torzítás y = tf.matmul(x,W) + b # y mérete: batchsize x 10 loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=d, logits=y)) # veszteségfv. # optimalizálás: loss-t minimalizáljuk optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) # optimalizáló eljárás: gradient descent, learning rate: 0.5 ############# # soros tanítás: #with tf.Session() as sess: # sess.run(tf.global_variables_initializer()) # inicializáljuk W-t és b-t # for _ in range(1000): # nincs szükség az indexre, csak csinálja meg 1000x (1000 epoch) # i = np.random.randint(0,55000) # optimizer.run(feed_dict={x: mnist.train.images[i].reshape(1,784), d: mnist.train.labels[i].reshape(1,10)}) # correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(d,1)) # accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) # print(loss.eval(feed_dict={x: mnist.test.images, d: mnist.test.labels})) # print(accuracy.eval(feed_dict={x: mnist.test.images, d: mnist.test.labels})) ############# # kötegelt tanítás, batch size: 100 with tf.Session() as sess: sess.run(tf.global_variables_initializer()) # inicializáljuk W-t és b-t for _ in range(1000): # nincs szükség az indexre, csak csinálja meg 1000x (1000 epoch) batch = mnist.train.next_batch(100) optimizer.run(feed_dict={x: batch[0], d: batch[1]}) correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(d,1)) # hol van egyezés: y=d accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) print(accuracy.eval(feed_dict={x: mnist.test.images, d: mnist.test.labels}))