[PYTHON] Create an environment for "Deep Learning from scratch" with Docker

Deep Learning from scratch

I had no knowledge of python or machine learning, but there are many topics in recent news, and when I was hoping that I could study just by touching something, O'Reilly's [Deep Learning from scratch] ](Https://www.oreilly.co.jp/books/9784873117584/) was easy to understand and I bought it.

The mathematical part was omitted moderately, and it was very interesting to read for a light person who wants to know a strict proof or a more image like myself. Also, it was very easy to understand that what was explained was explained at the source level.

So, I think that people who usually write python or something like that are good, but if you want to try it for the time being, you can also add python, numpy, and matplotlib (you can make it once). ) It's a little messy, isn't it?

Then I thought I would make it with Docker, but I was a little addicted to the graph drawing (GUI) with matplolib that comes out in this book, so I will leave it as an article. (However, I later realized that it might be a little subtle, such as putting some libraries on the mac side and work before starting the container.)

Environment

At first, I thought about putting it in a set easily, but I thought about Jupiter notebook, but it was not easy because there was about 4G of Docker image, and this" Deep Learning from scratch "itself is" with the minimum library. It seems to be a concept of "let's make it from scratch", so I thought that I wanted an environment where only the necessary libraries could be put in, so I made it myself.

Caution However, since my environment is mac, the main GUI part is for mac.

Mandatory environment

--Python3 environment

Seems to be what you need in the sample of this book.

Of these, the graph drawing part (matplotlib) that often appears in this book can be recommended if there is a GUI environment, but if you do this in the Docker environment, you can draw the graph from the Docker environment. will become necessary.

I've never done that before, so I wasn't sure if I could do it, but when I looked it up

Let's skip the GUI of the Docker container to the Mac side and display it

There was a nice article. When I made it based on this, I created an environment where I can recommend this book compactly.

Docker environment construction

Since the Docker file itself only contains the library, insert the required library based on the official python image of Docker hub.

FROM python:3.5.2

WORKDIR /var/python

RUN pip install \
        ipython \
        numpy \
        matplotlib

CMD ["ipython"]

So, in the Docker container created in this environment, for example,

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(-3, 3, 0.1)
y = np.sin(x)
plt.plot(x, y)
plt.show()

I want to execute a script like this and draw a graph on the mac side.

The contents are the same as the above link, but in order to have the contents of the graph sent from the Docker container side based on the IP address on the mac side,

--Install the X server (XQuartz) on the mac side. --Set a port to receive the drawing contents from the Docker container side with socat and pass it to the X server in mac

By doing these two points, I was able to draw the contents of the graph from python running on Docker to the mac side.

Installation of required libraries

Put socat and XQuartz in brew.

brew install socat
brew install caskroom/cask/brew-cask
brew cask install xquartz

You are now ready.

Build Docker

The Docker build itself is done as usual with the Docker file above. Here, the image name is python-dl.

docker build -t python-dl .

Run!

First, prepare X on the mac side.

socat TCP-LISTEN:6000,reuseaddr,fork UNIX-CLIENT:\"$DISPLAY\"

In the reference article, I explicitly ran ʻopen -a XQuartz` in advance, but as far as I tried, it seemed to be started without permission even if I did not run it in advance. ..

Now that you're ready, start the docker container.

When starting the image built earlier, by passing the IP address of the mac side as the address of the X server to the container side, the GUI is drawn from the mouth created with socat to the mac side.

I think that many people can get the IP address on the mac side with ʻipconfig en0`, so in order to get the IP address from this content

ifconfig en0 | grep -v inet6 | grep inet | awk '{print $2 ":0"}'`

(Actually, the IP address of mac is passed to the container: 0, so the script is as above).

Also, in order to mount the sources of various samples, start the container with the following command in consideration of mounting the current directory with -v.

docker run --rm -it -e DISPLAY=$(ifconfig en0 | grep -v inet6 | grep inet | awk '{print $2 ":0"}') -v $(pwd):/var/python python-dl bash

This will start bash on the container side, so as a sample, in samples / sample01.py,

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(-3, 3, 0.1)
y = np.sin(x)
plt.plot(x, y)
plt.show()

If there is

python samples/sample01.py

So, if the graph below is drawn on the mac side, it is successful.

スクリーンショット 2016-12-10 13.56.37.png

Other

The contents including the environment (+ α) so far

https://github.com/pocari/python-dl-docker

In

Get the sample code of the book together

The sample of this book is registered on github, so the repository of it isbook-sample Since it is registered as a submodule of git under the folder called

--Repository for this article clone

  git clone https://github.com/pocari/python-dl-docker

--Clone the code in the book sample code directory

  git submodule update -i

If you do, you can also run a sample book.

Example) After entering the container with the above docker run command

cd samples/book-sample/ch07
python apply_filter.py

Then, the following result will be displayed on the mac side. スクリーンショット 2016-12-11 0.46.41.png

Recommended Posts

Create an environment for "Deep Learning from scratch" with Docker
Realize environment construction for "Deep Learning from scratch" with docker and Vagrant
Create a machine learning environment from scratch with Winsows 10
Prepare the environment for O'Reilly's book "Deep Learning from scratch" with apt-get (Debian 8)
[Windows 10] "Deep Learning from scratch" environment construction
Deep Learning from scratch
Create an arbitrary machine learning environment with GCP + Docker + Jupyter Lab
Deep Learning from scratch 1-3 chapters
Create an environment with virtualenv
Create an environment for test automation with AirtestIDE (Tips)
Rebuilding an environment for machine learning with Miniconda (Windows version)
An amateur stumbled in Deep Learning from scratch Note: Chapter 1
Making from scratch Deep Learning ❷ An amateur stumbled Note: Chapter 5
Making from scratch Deep Learning ❷ An amateur stumbled Note: Chapter 2
Deep learning from scratch (cost calculation)
An amateur stumbled in Deep Learning from scratch Note: Chapter 3
From environment construction to deployment for flask + Heroku with Docker
An amateur stumbled in Deep Learning from scratch Note: Chapter 7
An amateur stumbled in Deep Learning from scratch Note: Chapter 5
Making from scratch Deep Learning ❷ An amateur stumbled Note: Chapter 7
Making from scratch Deep Learning ❷ An amateur stumbled Note: Chapter 1
Making from scratch Deep Learning ❷ An amateur stumbled Note: Chapter 4
Create a Todo app with Django ① Build an environment with Docker
An amateur stumbled in Deep Learning from scratch Note: Chapter 4
Deep Learning memos made from scratch
An amateur stumbled in Deep Learning from scratch Note: Chapter 2
Making from scratch Deep Learning ❷ An amateur stumbled Note: Chapter 6
Deep learning tutorial from environment construction
I built an environment for machine learning from scratch (windows10 + Anaconda + VSCode + Tensorflow + GPU version)
[Deep Learning from scratch] Main parameter update methods for neural networks
Machine Learning with docker (42) Programming PyTorch for Deep Learning By Ian Pointer
Create an environment for Django x Apache x mod_wsgi with Vagrant (Ubuntu 16.04)
Create a development environment for Go + MySQL + nginx with Docker (docker-compose)
Write an impression of Deep Learning 3 framework edition made from scratch
[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]
Create Python + uWSGI + Nginx environment with Docker
[Learning memo] Deep Learning made from scratch [Chapter 6]
"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"
[Deep Learning from scratch] About hyperparameter optimization
"Deep Learning from scratch" Self-study memo (Part 12) Deep learning
[Learning memo] Deep Learning made from scratch [~ Chapter 4]
Build a "Deep learning from scratch" learning environment on Cloud9 (jupyter miniconda python3)
"Deep Learning from scratch" self-study memo (unreadable glossary)
"Deep Learning from scratch" Self-study memo (9) MultiLayerNet class
Create execution environment for each language with boot2docker
Good book "Deep Learning from scratch" on GitHub
Building an Anaconda environment for Python with pyenv
Deep Learning from scratch Chapter 2 Perceptron (reading memo)
[Learning memo] Deep Learning from scratch ~ Implementation of Dropout ~
Various commands for building an environment with Apache
Create Nginx + uWSGI + Python (Django) environment with docker
Machine learning starting from scratch (machine learning learned with Kaggle)
Python vs Ruby "Deep Learning from scratch" Summary
Try building an environment for MayaPython with VisualStudioCode
"Deep Learning from scratch" Self-study memo (10) MultiLayerNet class