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.)
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.
--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.
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.
Put socat
and XQuartz
in brew
.
brew install socat
brew install caskroom/cask/brew-cask
brew cask install xquartz
You are now ready.
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 .
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.
The contents including the environment (+ α) so far
https://github.com/pocari/python-dl-docker
In
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.
Recommended Posts