Deep learning tutorial from environment construction

Deep learning has become a hot topic these days,

"I want to study, but I'm not sure what I can do in the first place ..."

For such people, we will introduce the method from environment construction to deep learning execution on Mac.

python environment construction

Speaking of machine learning and deep learning, it is python. For the time being, prepare the python environment construction. First, let's install python. Let's open a terminal and run it in order.

Terminal


brew install pyenv
pyenv install anaconda3-4.0.0

This completes the installation.

Terminal


pyenv versions

And run

* system (set by ~~~)
  anaconda3-4.0.0

If anaconda3-4.0.0 appears like this, the installation is successful.

Next, pass the path of pyenv.

Terminal


echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
echo 'eval "$(pyenv init -)"' >> ~/.bash_profile
source ~/.bash_profile

This is OK!

Next, let's switch to the python version we just put in.

Terminal


pyenv grobal anaconda3-4.0.0

You should have successfully switched.

Terminal


python

By typing in the terminal and executing

Python 3.5.2 |Continuum Analytics, Inc.| (default, Jul  2 2016, 17:52:12)

If Python 3.x.x etc. is displayed, the switch is successful.

keras installation and settings

Next, let's install keras, which is famous for its deep learning framework. If you try to create deep learning from scratch, it will take a lot of effort, but with keras, you can do it in 10 lines.

By the way, I also put in a library that handles h5 format to save the learning results of keras.

pip install h5py
pip install keras

Installation is complete. After that, make some settings.

Terminal


mkdir ~/.keras
echo '{"epsilon": 1e-07, "floatx": "float32", "backend": "theano"}' >> ~/.keras/keras.json

[Tips] What is backend?

You can skip it and read it at all.

By the way, at the root of deep learning algorithms, it is necessary to perform mathematical calculations. Actually, the library that performs this mathematical calculation is called backend, and it is carried by a library different from keras. You can choose either theano or tensorflow, and theano will be installed at the same time you install keras.

Deep learning execution

Sorry to keep you waiting. Let's do deep learning immediately.

keras_tutorial.py


#!/usr/bin/env python
# -*- coding: utf-8 -*-

import numpy as np
import keras

from keras.utils import np_utils
from keras.datasets import mnist
from keras.layers.convolutional import Convolution2D, MaxPooling2D
from keras.layers.core import Activation, Dense, Dropout, Flatten

(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = x_train.reshape(-1, 28, 28, 1).astype('float32')/255
x_test = x_test.reshape(-1, 28, 28, 1).astype('float32')/255
y_train = np_utils.to_categorical(y_train, 10)
y_test = np_utils.to_categorical(y_test, 10)

model = keras.models.Sequential()
model.add(Convolution2D(nb_filter=20, nb_row=5, nb_col=5,
                        border_mode='valid',
                        input_shape=(28, 28, 1)))
model.add(Activation("relu"))
model.add(Convolution2D(nb_filter=15, nb_row=5, nb_col=5))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.5))
model.add(Flatten())
model.add(Dense(128))
model.add(Activation("relu"))
model.add(Dropout(0.5))
model.add(Dense(10))
model.add(Activation("softmax"))
model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=['accuracy'])

model.fit(x_train, y_train,  batch_size=100, nb_epoch=3, verbose=1)

score = model.evaluate(x_test, y_test, show_accuracy=True, verbose=0)

print("test-accuracy: {}".format(score[1]))

model_json_str = model.to_json()
open('model.json', 'w').write(model_json_str)
model.save_weights('weights.h5')

If the following display appears, deep learning can be started safely.

Using Theano backend.
Epoch 1/3
 5100/60000 [=>............................] - ETA: 111s - loss: 1.2484 - acc: 0.5733

Check the results of deep learning

Now, is the learning completed? When complete, you should see something like this:

test-accuracy: 0.989

This means that there is a 98.9% chance that the numbers can be identified against data (test data) that is completely different from training.

Now that you're not sure what characters are being judged and how, let's run the following script.

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import numpy as np
import pylab
import keras

from keras.utils import np_utils
from keras.datasets import mnist

(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_test = x_test.reshape(-1, 28, 28, 1).astype('float32')/255

model = keras.models.model_from_json(open('model.json').read())
model.load_weights('weights.h5')
model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=['accuracy'])

tests = x_test[:20]
labels = model.predict(tests, verbose=1)

for index, (image, label) in enumerate(zip(tests, labels)):
    pylab.subplot(4, 5, index + 1)
    pylab.axis('off')
    pylab.imshow(image.reshape(28, 28), cmap=pylab.cm.gray_r, interpolation='nearest')
    pylab.title(np.argmax(label))
pylab.show()

Here, the learning result from earlier is saved in model.json and weights.h5, so it is reloaded.

I think the results are as follows. It shows which number was judged for each number.

figure_1.png

Introduction

What did you think.

Anyway, I have posted the script for those who want to execute it anyway.

I would be very happy if more people would be interested in this and start studying deep learning.

Finally, I would like to introduce the introductory book.

[・ Deep Learning from scratch The theory and implementation of deep learning learned with Python](https://www.amazon.co.jp/%E3%82%BC%E3%83%AD%E3%81%8B%E3 % 82% 89% E4% BD% 9C% E3% 82% 8BDeep-Learning-Python% E3% 81% A7% E5% AD% A6% E3% 81% B6% E3% 83% 87% E3% 82% A3 % E3% 83% BC% E3% 83% 97% E3% 83% A9% E3% 83% BC% E3% 83% 8B% E3% 83% B3% E3% 82% B0% E3% 81% AE% E7 % 90% 86% E8% AB% 96% E3% 81% A8% E5% AE% 9F% E8% A3% 85-% E6% 96% 8E% E8% 97% A4-% E5% BA% B7% E6 % AF% 85 / dp / 4873117585 / ref = pd_bxgy_14_2? _ Encoding = UTF8 & psc = 1 & refRID = 6D052QXCKJEGZ5ZY4HRV)

This is an introductory book in the form of understanding the theory with an implementation in python. It is recommended for those who want to understand while actually programming.

・ Deep learning

There is no implementation and the main content is theory, but it is explained in a fairly easy-to-understand manner even in the book where the theory is written. The feature is that it covers a wide range from basic contents to topical methods.

Recommended Posts

Deep learning tutorial from environment construction
[Windows 10] "Deep Learning from scratch" environment construction
Deep Learning from scratch
Realize environment construction for "Deep Learning from scratch" with docker and Vagrant
Deep Learning from scratch 1-3 chapters
Machine learning environment construction macbook 2021
Deep Learning / Deep Learning from Zero 2 Chapter 4 Memo
Deep Learning / Deep Learning from Zero Chapter 3 Memo
Deep Learning / Deep Learning from Zero 2 Chapter 5 Memo
Create an environment for "Deep Learning from scratch" with Docker
Deep learning from scratch (cost calculation)
Deep Learning / Deep Learning from Zero 2 Chapter 7 Memo
Deep Learning / Deep Learning from Zero 2 Chapter 8 Memo
From Ubuntu 20.04 introduction to environment construction
Microsoft's Deep Learning Library "CNTK" Tutorial
Deep Learning / Deep Learning from Zero Chapter 5 Memo
Deep Learning / Deep Learning from Zero Chapter 4 Memo
Deep Learning / Deep Learning from Zero 2 Chapter 3 Memo
Deep Learning memos made from scratch
Deep Learning
Deep Learning / Deep Learning from Zero 2 Chapter 6 Memo
"Deep Learning from scratch" self-study memo (No. 15) TensorFlow beginner tutorial
Reinforcement learning to learn from zero to deep
[Learning memo] Deep Learning made from scratch [Chapter 7]
Deep learning from scratch (forward propagation edition)
Deep learning / Deep learning from scratch 2-Try moving GRU
Deep learning / Deep learning made from scratch Chapter 6 Memo
[Learning memo] Deep Learning made from scratch [Chapter 5]
[Learning memo] Deep Learning made from scratch [Chapter 6]
Image alignment: from SIFT to deep learning
Python and machine learning environment construction (macOS)
"Deep Learning from scratch" in Haskell (unfinished)
Deep learning / Deep learning made from scratch Chapter 7 Memo
Learning record of reading "Deep Learning from scratch"
Environment construction for MXNet tutorial (gluon part)
[Deep Learning from scratch] About hyperparameter optimization
Deep Learning from mathematical basics (during attendance)
[Learning memo] Deep Learning made from scratch [~ Chapter 4]
Django environment construction
DeepIE3D environment construction
Linux environment construction
Python environment construction
Deep Learning Memorandum
Environment construction (python)
django environment construction
CodeIgniter environment construction
python environment construction
Python Deep Learning
Python --Environment construction
Deep learning × Python
Golang environment construction
python environment construction
Build a "Deep learning from scratch" learning environment on Cloud9 (jupyter miniconda python3)
Word2vec environment construction
"Deep Learning from scratch" self-study memo (unreadable glossary)
Collecting information from Twitter with Python (Environment construction)
"Deep Learning from scratch" Self-study memo (9) MultiLayerNet class
From Kivy environment construction to displaying Hello World
Deep Learning from scratch ① Chapter 6 "Techniques related to learning"
Good book "Deep Learning from scratch" on GitHub
Python explosive environment construction starting from zero (Mac)