Try deep learning with TensorFlow

TensorFlow A machine learning library made by Google, announced in November 2015. It is said to read "tensor flow". It is actually used in the company's service.

Some people have written various things, but since the head family is the best, I will try to put it in immediately https://www.tensorflow.org/versions/r0.8/get_started/os_setup.html

#Preparations
sudo easy_install pip
sudo easy_install --upgrade six
sudo pip install --upgrade https://storage.googleapis.com/tensorflow/mac/tensorflow-0.8.0-py2-none-any.whl
sudo pip install --upgrade virtualenv
sudo virtualenv --system-site-packages ~/tensorflow
source ~/tensorflow/bin/activate

#Install TensorFlow
pip install --upgrade https://storage.googleapis.com/tensorflow/mac/tensorflow-0.8.0-py2-none-any.whl
source ~/tensorflow/bin/activate 
python

First of all, do the prepared test to python

>>> import tensorflow as tf
>>> hello = tf.constant('Hello, TensorFlow!')
>>> hello
<tf.Tensor 'Const:0' shape=() dtype=string>
>>> sess = tf.Session()
>>> print(sess.run(hello))
Hello, TensorFlow!
>>> a = tf.constant(10)
>>> b = tf.constant(32)
>>> print(sess.run(a + b))
42

It worked

Do a tutorial for beginners

After all see the head family https://www.tensorflow.org/versions/master/tutorials/mnist/beginners/index.html

image Machine learning is performed using a data set of the above handwritten numerical images called MNIST. This is positioned as a confirmation of basic operation like Hello World of programming.

First of all, I get the data, but it seems that I can already get it automatically by typing the following.

>>> 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

What kind of data is, for example, like this train-images-idx3-ubyte.gz: training set images (9912422 bytes)

0000 0803 0000 ea60 0000 001c 0000 001c 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0312 1212 7e88 af1a

So, I will actually try it, but I will skip the explanation of the calculation method inside because it is heavy.

>>> 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)

First, I'm making a box to put it in. Since the image is 28x28 = 784 pixels, we need a 784-dimensional vector, and since it is a number, it is a box for 10-dimensional = 10 features from 0 to 9.

image

The formula is y = softmax (w * x + b) image


I will make a training part from here

>>> 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.5).minimize(cross_entropy)

For learning, we have to make a definition of what is good and what is bad. Here, we will train using the cost function "cross entropy". The correct data box y_ is also created here. Let's calculate the cross-entropy, and here we use the gradient descent algorithm with a learning rate of cross_entropy 0.5 to find the TensorFlow to minimize. Finally initialize with the session

>>> init = tf.initialize_all_variables()
>>> sess = tf.Session()
>>> sess.run(init)

Now that you're ready, let's! Training ... 1000 steps

for i in range(1000):
  batch_xs, batch_ys = mnist.train.next_batch(100)
  sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})

Get a "batch" of 100 random data points from the MNIST training set and perform the steps


Evaluate the model. First, make a calculation set

>>> correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
>>> accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

Run

>>> print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))
0.9188

It seems that the correct answer rate is 90%. However, this is not high in accuracy, and with fine adjustment, it can be raised to 99.7% ... Magica w

There seems to be a detailed explanation here as well https://drive.google.com/file/d/0B04ol8GVySUubjVsUDdXc0hla00/view

I will do various things to improve the accuracy. Continue to Part 2 http://qiita.com/northriver/items/4f4690053e1770311335

Recommended Posts

Try deep learning with TensorFlow
Try deep learning with TensorFlow Part 2
Try Deep Learning with FPGA
Try Bitcoin Price Forecasting with Deep Learning
Try with Chainer Deep Q Learning --Launch
Try deep learning of genomics with Kipoi
Try regression with TensorFlow
Deep Kernel Learning with Pyro
Try machine learning with Kaggle
Generate Pokemon with Deep Learning
Try TensorFlow MNIST with RNN
Deep Learning
Cat breed identification with deep learning
Reinforcement learning 13 Try Mountain_car with ChainerRL.
Make ASCII art with deep learning
Solve three-dimensional PDEs with deep learning.
Try machine learning with scikit-learn SVM
Check squat forms with deep learning
Categorize news articles with deep learning
Forecasting Snack Sales with Deep Learning
Try Common Representation Learning with chainer
Make people smile with Deep Learning
Try data parallelism with Distributed TensorFlow
Try to build a deep learning / neural network with scratch
[Evangelion] Try to automatically generate Asuka-like lines with Deep Learning
Try Distributed TensorFlow
Classify anime faces with deep learning with Chainer
Deep learning / Deep learning from scratch 2-Try moving GRU
Deep Learning Memorandum
Stock price forecast using deep learning (TensorFlow)
Start Deep learning
Sentiment analysis of tweets with deep learning
Python Deep Learning
Deep learning × Python
Introduction to Deep Learning (2) --Try your own nonlinear regression with Chainer-
Dealing with tensorflow suddenly stopped working using GPU in deep learning
Zundokokiyoshi with TensorFlow
Breakout with Tensorflow
Try TensorFlow RNN with a basic model
Reinforcement learning 11 Try OpenAI acrobot with ChainerRL.
Coursera's TensorFlow introductory course to get you started with Deep Learning implementations
The story of doing deep learning with TPU
Try Tensorflow with a GPU instance on AWS
Memory Leak occurred after learning DQN with tensorflow == 2.0.0
99.78% accuracy with deep learning by recognizing handwritten hiragana
Stock Price Forecast Using Deep Learning (TensorFlow) -Part 2-
Try scraping with Python.
First Deep Learning ~ Struggle ~
Learning Python with ChemTHEATER 03
"Object-oriented" learning with python
Learning Python with ChemTHEATER 05-1
Python: Deep Learning Practices
Deep learning / activation functions
Deep Learning from scratch
Reading data with TensorFlow
Kyotei forecast with TensorFlow
Deep learning 1 Practice of deep learning
Deep learning / cross entropy
First Deep Learning ~ Preparation ~
First Deep Learning ~ Solution ~
[AI] Deep Metric Learning