갈루아의 반서재

텐서플로는 구글이 오픈소스로 공개한 차세대 딥 러닝 시스템이다. 텐서플로는 데이터 플로우 그래프를 사용하여 수치 연산을 하는 라이브러리라고 볼 수 있다. 그래프의 노드는 수학 연산을 나타내고, 노드를 연결하는 그래프의 에지는 다차원 데이터 배열을 나타낸다.


1. 설치환경


포스팅에 언급된 환경은 Ubuntu 14.04 LTS x64 + CPU Only 기반이다(아래와 같이 그래픽카드 확인해본 결과 GPU 가 아니라 CPU Only 였다).

* 리눅스에서 하드웨어를 확인하는 명령은 다음 사이트를 참고한다. http://blog.simplism.kr/?p=1778


1
2
root@localhost:~# lspci | grep VGA
00:02.0 VGA compatible controller: Cirrus Logic GD 5446
cs


1
(tensorflow)root@localhost:~/tensorflow# lspci | grep -i nvidi
cs



2. 설치요구사항


텐서플로 파이썬 API 는 파이썬 2.7 또는 3.3 이상을 지원한다.

본 포스팅에서는 아나콘다 환경에서 파이썬 2.7 기반에서 텐서플로를 설치해본다.

이외의 설치방법에 대해서는 아래 링크를 참고한다.


  • Pip install: Install TensorFlow on your machine, possibly upgrading previously installed Python packages. May impact existing Python programs on your machine.
  • Virtualenv install: Install TensorFlow in its own directory, not impacting any existing Python programs on your machine.
  • Anaconda install: Install TensorFlow in its own environment for those running the Anaconda Python distribution. Does not impact existing Python programs on your machine.
  • Docker install: Run TensorFlow in a Docker container isolated from all other programs on your machine.
  • Installing from sources: Install TensorFlow by build


3. 아나콘다 가상환경 구축



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
root@localhost:~# conda create -n tensorflow python=2.7
Fetching package metadata: ....
Solving package specifications: .
Package plan for installation in environment /root/anaconda/envs/tensorflow:
 
The following NEW packages will be INSTALLED:
 
    openssl:    1.0.2j-0
    pip:        8.1.2-py27_0
    python:     2.7.12-1
    readline:   6.2-2
    setuptools: 27.2.0-py27_0
    sqlite:     3.13.0-0
    tk:         8.5.18-0
    wheel:      0.29.0-py27_0
    zlib:       1.2.8-3
 
Proceed ([y]/n)? y
 
Linking packages ...
[      COMPLETE      ]|##################################################| 100%
#
# To activate this environment, use:
# $ source activate tensorflow
#
# To deactivate this environment, use:
# $ source deactivate
#
root@localhost:~#
cs



4. 가상환경 활성화


* 가상환경을 삭제할 때는 다음과 같이 한다. conda remove --name tensorflow --all

1
2
3
4
root@localhost:~# source activate tensorflow
discarding /root/anaconda/bin from PATH
prepending /root/anaconda/envs/tensorflow/bin to PATH
(tensorflow)root@localhost:~# cd tensorflow
cs


5. 텐서플로 설치에 필요한 적절한 바이너리 선택


아래는  Ubuntu/Linux 64-bit, CPU only, Python 2.7 의 경우이다. 기타 환경에 대한 내용은 https://www.tensorflow.org/versions/r0.11/get_started/os_setup.html#anaconda-installation 참고한다.

1
2
(tensorflow)root@localhost:~/tensorflow# export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.9.0-cp27-none-linux_x86_64.whl
 
cs


6. 텐서플로 설치


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
(tensorflow)root@localhost:~/tensorflow# pip install --upgrade $TF_BINARY_URL
Collecting tensorflow==0.9.0 from https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.9.0-cp27-none-linux_x86_64.whl
  Downloading https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.9.0-cp27-none-linux_x86_64.whl (27.6MB)
    100|████████████████████████████████| 27.6MB 29kB/s
Collecting numpy>=1.8.2 (from tensorflow==0.9.0)
  Downloading numpy-1.11.2-cp27-cp27mu-manylinux1_x86_64.whl (15.3MB)
    100|████████████████████████████████| 15.3MB 44kB/s
Collecting six>=1.10.0 (from tensorflow==0.9.0)
  Using cached six-1.10.0-py2.py3-none-any.whl
Collecting protobuf==3.0.0b2 (from tensorflow==0.9.0)
  Downloading protobuf-3.0.0b2-py2.py3-none-any.whl (326kB)
    100|████████████████████████████████| 327kB 2.7MB/s
Requirement already up-to-date: wheel in /root/anaconda/envs/tensorflow/lib/python2.7/site-packages (from tensorflow==0.9.0)
Collecting setuptools (from protobuf==3.0.0b2->tensorflow==0.9.0)
  Downloading setuptools-28.6.1-py2.py3-none-any.whl (471kB)
    100|████████████████████████████████| 481kB 1.9MB/s
Installing collected packages: numpy, six, setuptools, protobuf, tensorflow
  Found existing installation: setuptools 27.2.0
Cannot remove entries from nonexistent file /root/anaconda/envs/tensorflow/lib/python2.7/site-packages/easy-install.pth
cs

위와 같이 easy_install
관련 에러가 발생하면 --ignore-installed 플래그를 추가하여 설치한다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
(tensorflow)root@localhost:~/tensorflow# pip install --upgrade --ignore-installed $TF_BINARY_URL
Collecting tensorflow==0.9.0 from https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.9.0-cp27-none-linux_x86_64.whl
  Using cached https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.9.0-cp27-none-linux_x86_64.whl
Collecting numpy>=1.8.2 (from tensorflow==0.9.0)
  Using cached numpy-1.11.2-cp27-cp27mu-manylinux1_x86_64.whl
Collecting six>=1.10.0 (from tensorflow==0.9.0)
  Using cached six-1.10.0-py2.py3-none-any.whl
Collecting protobuf==3.0.0b2 (from tensorflow==0.9.0)
  Using cached protobuf-3.0.0b2-py2.py3-none-any.whl
Collecting wheel (from tensorflow==0.9.0)
  Downloading wheel-0.29.0-py2.py3-none-any.whl (66kB)
    100|████████████████████████████████| 71kB 745kB/s
Collecting setuptools (from protobuf==3.0.0b2->tensorflow==0.9.0)
  Using cached setuptools-28.6.1-py2.py3-none-any.whl
Installing collected packages: numpy, six, setuptools, protobuf, wheel, tensorflow
Successfully installed numpy-1.11.2 protobuf-3.0.0b2 setuptools-27.2.0 six-1.10.0 tensorflow-0.9.0 wheel-0.29.0
(tensorflow)root@localhost:~/tensorflow#
cs



7. 텐서플로 설치테스트


Hello, Tensorflow! 출력과 간단한 곱셉 연산을 테스트해보자.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
(tensorflow)root@localhost:~/tensorflow# python
Python 2.7.12 |Continuum Analytics, Inc.| (default, Jul  2 201617:42:40)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux2
Type "help""copyright""credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org
>>> import tensorflow as tf
>>> hello = tf.constant('Hello, Tensorflow!')
>>> sess = tf.Session()
>>> print(sess.run(hello))
Hello, Tensorflow!
>>> a = tf.constant(10)
>>> b = tf.constant(32)
>>> print(sess.run(a+b))
42
>>>
cs