Softmax regressions con MNIST de TensorFlow

Hoy me divertí un cacho con esto…
Tutorial de MNIST For ML Beginners de TensorFlow.

En esta entrada no pongo explicaciones, sólo los resultados de la consola. La explicación está en el tutorial del link oficial.

Instalando TF:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Last login: Sat Sep 23 14:34:54 on console
MacBook-Pro-de-Farid:/ farid$ sudo su
Password:
sh-3.2# pip install tensorflow
Requirement already satisfied: tensorflow in /Users/farid/anaconda/lib/python2.7/site-packages
Requirement already satisfied: backports.weakref>=1.0rc1 in /Users/farid/anaconda/lib/python2.7/site-packages (from tensorflow)
Requirement already satisfied: wheel in /Users/farid/anaconda/lib/python2.7/site-packages (from tensorflow)
Requirement already satisfied: protobuf>=3.3.0 in /Users/farid/anaconda/lib/python2.7/site-packages (from tensorflow)
Requirement already satisfied: mock>=2.0.0 in /Users/farid/anaconda/lib/python2.7/site-packages (from tensorflow)
Requirement already satisfied: numpy>=1.11.0 in /Users/farid/anaconda/lib/python2.7/site-packages (from tensorflow)
Requirement already satisfied: tensorflow-tensorboard<0.2.0,>=0.1.0 in /Users/farid/anaconda/lib/python2.7/site-packages (from tensorflow)
Requirement already satisfied: six>=1.10.0 in /Users/farid/anaconda/lib/python2.7/site-packages (from tensorflow)
Requirement already satisfied: setuptools in /Users/farid/anaconda/lib/python2.7/site-packages (from protobuf>=3.3.0->tensorflow)
Requirement already satisfied: funcsigs>=1; python_version < "3.3" in /Users/farid/anaconda/lib/python2.7/site-packages (from mock>=2.0.0->tensorflow)
Requirement already satisfied: pbr>=0.11 in /Users/farid/anaconda/lib/python2.7/site-packages (from mock>=2.0.0->tensorflow)
Requirement already satisfied: markdown>=2.6.8 in /Users/farid/anaconda/lib/python2.7/site-packages (from tensorflow-tensorboard<0.2.0,>=0.1.0->tensorflow)
Requirement already satisfied: bleach==1.5.0 in /Users/farid/anaconda/lib/python2.7/site-packages (from tensorflow-tensorboard<0.2.0,>=0.1.0->tensorflow)
Requirement already satisfied: html5lib==0.9999999 in /Users/farid/anaconda/lib/python2.7/site-packages (from tensorflow-tensorboard<0.2.0,>=0.1.0->tensorflow)
Requirement already satisfied: werkzeug>=0.11.10 in /Users/farid/anaconda/lib/python2.7/site-packages (from tensorflow-tensorboard<0.2.0,>=0.1.0->tensorflow)

Iniciando Python:

1
2
3
4
5
6
sh-3.2# python
Python 2.7.13 |Anaconda custom (x86_64)| (default, Dec 20 2016, 23:05:08)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org

Importando el imput data de ejemplo de TF:

1
2
3
4
>>> from tensorflow.examples.tutorials.mnist import imput_data
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: cannot import name imput_data

Corrigiendo la m por la n xD:

1
2
3
4
5
6
7
8
9
10
>>> from tensorflow.examples.tutorials.mnist import input_data
>>> mnist = input_data.read_data_sets("MNIST_data/",one_hot=True)
Successfully downloaded train-images-idx3-ubyte.gz 9912422 bytes.
Extracting MNIST_data/train-images-idx3-ubyte.gz
Successfully downloaded train-labels-idx1-ubyte.gz 28881 bytes.
Extracting MNIST_data/train-labels-idx1-ubyte.gz
Successfully downloaded t10k-images-idx3-ubyte.gz 1648877 bytes.
Extracting MNIST_data/t10k-images-idx3-ubyte.gz
Successfully downloaded t10k-labels-idx1-ubyte.gz 4542 bytes.
Extracting MNIST_data/t10k-labels-idx1-ubyte.gz

Definiendo variables

1
2
3
4
5
6
7
8
9
10
11
12
>>> import tensorflow as tf
>>> x = tf.placeholder(tf.float32, [None, 784])
>>> W = tf.Variable(tf.zeros([784, 10]))
>>> b = tf.Variable(tf.zeros([10]))
>>> y = tf.nn.softmax(tf.matmul(x,W)+b)
>>> y_ = tf.placeholder(tf.float32,[None,10])
>>> cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_*tf.log(y),reduction_indices=[1]))
>>> train_step = tf.train.GradientDescentOptimizer(0.05).minimize(cross_entropy)
>>> sess = tf.InteractiveSession()
2017-09-27 21:35:59.319126: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
2017-09-27 21:35:59.319202: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn'
t compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
>>> tf.global_variables_initializer().run()

Training… mil veces cien puntos random

1
2
3
4
>>> for _ in range(1000):
... batch_xs, batch_ys = mnist.train.next_batch(100)
... sess.run(train_step, feed_dict={ x: batch_xs, y_: batch_ys })
...

Habrá funcionado? Evaluemos el modelo…

1
2
3
4
5
>>> correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_,1))
>>> accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
>>> print(sess.run(accuracy, feed_dict={ x: mnist.test.images, y_: mnist.test.labels}))
0.9016
>>>

90% bastante bien.

Hasta la próxima.

Autor entrada: Farid

Deja un comentario

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *