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