This is a good record for a Python inexperienced person to prepare the execution environment and comfortable coding environment of TensorFlow, which is a machine learning library published by Google. I also wanted to code Python itself comfortably, so I will try to build an environment where TensorFlow's MNIST demo can be executed with one click on Mac with Python 3.5.2 + PyCharm, which is the latest version at the time of writing this article.
First install the package management tool Homebrew
console
$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Even if it is already installed, it is recommended to update it just in case.
console
$ brew update
console
$ xcode-select --install
Python is installed on the Mac from the beginning, but the version is 2.x series, and because of the SIP introduced from El Capitan, it is inflexible. I think it's a good idea to put the latest version of Python from pyenv and then use pyenv-virtualenv to isolate the environment.
Please install the tool from the following command
console
$ brew install pyenv-virtualenv
First, install the latest version of Python (here 3.5.2 is installed)
Type pyenv install -l
to see a list of installs
console
$ pyenv install 3.5.2
$ pyenv rehash
If you get an error with pyenv install, installing the Xcode command line tool may help (second time)
console
$ xcode-select --install
In order to make the execution environment of TensorFlow independent, create an environment with a name based on the installed Python 3.5.2. By doing this, you can reduce errors due to conflicts between plugins and feel free to try different versions, making it closer to a virtual environment.
The "TensorFlow" part below is the name of the environment, so change it accordingly.
console
$ pyenv virtualenv 3.5.2 TensorFlow
$ pyenv rehash
Use pyenv global
to change to the created TensorFlow environment
console
$ pyenv global TensorFlow
Then hit the python command and it should change to 3.5.2.
console
$ python -V
Python 3.5.2
If the python command does not change the reference even though it is switched properly in pyenv as shown below,
console
$ pyenv version
TensorFlow (set by /Users/hogehoge/.pyenv/version)
$ python -V
Python 2.7.10
Try adding settings around here (maybe just the third line)
console
$ echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
$ echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
$ echo 'eval "$(pyenv init -)"' >> ~/.bash_profile
Also, after installing TensorFlow, you can restore the environment with the following if necessary
console
$ pyenv global system
Since the environment is specified by PyCharm described later, the python command does not necessarily have to be in the TensorFlow environment. Of course it is necessary when executing from the python command.
Next, install pip, a plugin management tool for Python. If you have the latest Python installed, it seems to be included from the beginning, but just in case.
console
$ sudo easy_install pip
$ sudo pip install --upgrade pip
At this point, we will finally move on to installing TensorFlow.
console
$ pip install --upgrade https://storage.googleapis.com/tensorflow/mac/tensorflow-0.9.0-py3-none-any.whl
Since the installation URL of TensorFlow seems to specify the version, please check the latest version from Official. Here, v0.9 is specified.
And to build a comfortable Python coding environment, download PyCharm, an IDE developed by JetBrain Nemousu, from the following. https://www.jetbrains.com/pycharm/download/
Complements and breakpoints work perfectly, and Community Edition can be used for free. Great!
After starting PyCharm, create a project appropriately. At that time, specify "3.5.2 virtualenv at ~ / .pyenv / versions / TensorFlow" from the Interpreter pull-down on the Create Project dialog.
If you want to change the execution environment later, you can also change it from the pull-down menu of Preferences-> Project: PROJECT_NAME-> Project Interpreter.
The implementation of the MNIST demo of TensorFlow itself is sourced from the Official Tutorial and other commentary articles. Place the data (round throw) It was easy to just move it for the time being, so I followed the procedure on the ↓ site. http://www.trifields.jp/try-tutorial-mnist-for-ml-beginners-of-tensorflow-1713
Once the source and data are ready, click Run and you should see the result on the console at the bottom of the screen.
I don't think there are any people who are already using Python, but if you are a person like me who is new to Python because you want to run TensorFlow, you may stumble in various ways, so it flows as a memorandum. I summarized.
I referred to this article for the installation procedure http://qiita.com/hatapu/items/054dbab03607c47cb84f
Recommended Posts