Map your Mind.

Medical student. Jazz.

Installing Tensorflow and Keras on MacOS via Anaconda3

こんにちは!思うところあって最近機械学習を勉強しています。自分の勉強のために英語で記事を書きますが、質問コメントなど日本語でもいただけたらと思います。よろしくお願いします!

Hi. I am a Japanese student majoring in Biology and Medicine. For my practice, I try to post in English. However, feel free to contact me in English!

Main Message

If you are a beginner, follow only one website or package!!

Background

Here I describe what I wanted to do and obstacles I met.
In order to learn Machine Learning, I took a course of Eduonix.
https://www.eduonix.com/applied-machine-learning-for-healthcare
At the very beginning of this course, I had to import Keras. However, I could not import it smoothly possibly due to my environment.
Since I spent plenty of time overcoming this issue, I would like to share what occured and what I did.

Environment

MacBook Pro (2018) 2.3GHz Intel Core i5 16 GB LPDDR3
Intel Iris Plus Graphics 655 1536 MB
macOS Mojave 10.14.1
Anaconda 3.5.0
Tensorflow 0.6.0 Python 3.7.1 (reffered later)

Installation

I initially installed Python and Tensorflow reffering these Qiita(s):
Anaconda でTensorFlowを設定する
Mac + Python + Anaconda を利用してTensorFlowを動かしてみる
【Mac】Python3(Anaconda)でTensorFlow環境を構築してみる

One of my fault was that I had tried so many methods to install Python, Tensorflow, etc. At first, based on the first website, I had set up virtual environment. Then, I also installed them via pip or homebrew reffering the other two websites or more.
Though there were no trouble installing Tensorflow, when I try to import Keras, an error occured:

$ python
Python 3.7.0 (default, Jun 28 2018, 07:39:16) 
[Clang 4.0.1 (tags/RELEASE_401/final)] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import keras
Using TensorFlow backend.
Traceback (most recent call last):
  File "<stdin>", line 1, in    <module>
  File "/anaconda3/lib/python3.7/site-packages/keras/__init__.py", line 3, in <module>
from . import utils
  File "/anaconda3/lib/python3.7/site-packages/keras/utils/__init__.py", line 6, in <module>
from . import conv_utils
  File "/anaconda3/lib/python3.7/site-packages/keras/utils/conv_utils.py", line 9, in <module>
from .. import backend as K
  File "/anaconda3/lib/python3.7/site-packages/keras/backend/__init__.py", line 89, in <module>
from .tensorflow_backend import *
  File "/anaconda3/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py", line 8, in <module>
from tensorflow.python.ops import tensor_array_ops
ImportError: cannot import name 'tensor_array_ops' from 'tensorflow.python.ops' (/anaconda3/lib/python3.7/site-packages/tensorflow/python/ops/__init__.py)

No matter how much I goooooogled, I could find no solution (some websites reported similar symptoms; they recommended installation of tensorflow up-to-date, which in my case already installed).

Etiology

This happend probably because of imcompatibility of Tensorflow 0.6.0 with Python 3.7.1.
I installed that "out-of-date" Tensorflow 0.6.0 because it was the only version that pip supported.

Solution

Finally I hit upon this website.
Keras 環境構築(Mac)
While following this instruction, I realized what virtual environments are. Then I came up with an idea of applying Anaconda navigator.
Taking advantage of Anaconda navigator, which enables easy establishment and installation of packages, I could successfully install Python 3.6.1 and Tensorflow 1.12.0 in the same environment.
In this way, I finally introduced Keras :)

For the future

  • Always aware of versions which we install
  • Follow ONLY ONE website or book when you install something (especially before you got used to)
  • ASK SOMEONE in 15 minutes (be organized about what you're doing)