¸Ó½Å·¯´×(Tensorflow) »ç¿ë±â 2 (Tensorflow ¼³Ä¡)

   Á¶È¸ 24813   Ãßõ 8    

안녕하세요

Tensorflow 두번째 강좌 입니다.


이 강좌는 Tensorflow 홈페이지에 나와있는 과정을 따라해 본 내용입니다.

원문을 보고 싶으시면 아래 링크를 참조 해 주세요

https://www.tensorflow.org/versions/r0.9/get_started/os_setup.html#download-and-setup


Tensorflow 는 리눅스 환경에서 동작하고 Python 으로 실행됩니다.

따라서 리눅스와 Python을 기본으로 설치해야 하고

거기에 Tensorflow를 추가로 설치 해야 합니다.


이를 위해서 크게보면 3가지 방법이 가능한데요

1. 리눅스에 파이선을 설치하는 법

2. 리눅스에 가상 파이선 환경을 설치(Virtualenv, Anaconda) 하는 법

3. Windows에 Docker를 설치하고 그 위에서 실행 하는 법


가상 파이선 환경을 구축하는 것이 여러프로젝트를 진행 할때는

좋다고 하는데 오늘은 제일 기본이 되는 1번 방법을 이용해서 설치 해 보겠습니다.


가. 리눅스가 설치되어 있는 머신이 있다고 가정하고 진행하겠습니다.

    저는 google cloud에서 Virtual machine을 사용해서 환경을 구축했습니다.

    사용해 보니 좋더라구요. 클릭 몇번으로 원하는 코어에 원하는 운영체제로

    가상 머신을 만들어 줍니다.

    개발 환경은 Ubuntu14.04 LTS 로 진행 했습니다.  


나. Python 설치

   Ubuntu14.04에는 기본으로 python 2.7.6이 설치 되어 있습니다.


다. PIP를 설치 해 줍니다.

   sudo apt-get install python-pip python-dev

   이것은 Python으로 작성된 Library를 관리하는 프로그램으로

   Python에서 하나의 라이브러리를 사용하려면 다른 라이브러리에 의존성이 있어서

   다른 필요한 것들을 설치 해 줘야 합니다.

   설치가 한번에 되지 않을 수 있는데요      위와 같은 에러가 발생하면 sudo apt-get update 를 해 주시고

   sudo apt-get install python-pip 를 실행 켜주시면 완료 됩니다.


라. Tensorflow 설치

  텐서플로우에는 여러가지 버전이 있습니다.

  Python 버전별로 설치 버전이 달라지고 GPU 이용여부에 따라서도 버전이달라집니다.

  제 PC는 CPU 밖에 없어서 CPU only, Python 2.7 버전으로 설치 해 보겠습니다.

# Ubuntu/Linux 64-bit, CPU only, Python 2.7
$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.9.0-cp27-none-linux_x86_64.whl

# Ubuntu/Linux 64-bit, GPU enabled, Python 2.7 
# Requires CUDA toolkit 7.5 and CuDNN v4. For other versions, see "Install from sources" below.
$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-0.9.0-cp27-none-linux_x86_64.whl

# Mac OS X, CPU only, Python 2.7:
$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/mac/tensorflow-0.9.0-py2-none-any.whl

# Ubuntu/Linux 64-bit, CPU only, Python 3.4
$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.9.0-cp34-cp34m-linux_x86_64.whl

# Ubuntu/Linux 64-bit, GPU enabled, Python 3.4 
# Requires CUDA toolkit 7.5 and CuDNN v4. For other versions, see "Install from sources" below.
$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-0.9.0-cp34-cp34m-linux_x86_64.whl

# Ubuntu/Linux 64-bit, CPU only, Python 3.5
$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.9.0-cp35-cp35m-linux_x86_64.whl

# Ubuntu/Linux 64-bit, GPU enabled, Python 3.5 
# Requires CUDA toolkit 7.5 and CuDNN v4. For other versions, see "Install from sources" below.
$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-0.9.0-cp35-cp35m-linux_x86_64.whl

# Mac OS X, CPU only, Python 3.4 or 3.5:
$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/mac/tensorflow-0.9.0-py3-none-any.whl


아래 두 명령어를 실행 시켜 주시면 설치가 완료 됩니다.

export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.9.0-cp27-none-linux_x86_64.whl  
sudo pip install --upgrade $TF_BINARY_URL


마. 설치 확인

  잘 설치 되었는지 확인을 위해서 간단한 코드를 실행 시켜 보겠습니다.

$ python
...
>>> import tensorflow as tf
>>> hello = tf.constant('Hello, TensorFlow!')
>>> sess = tf.Session()
>>> print(sess.run(hello))

간단히 코드를 살펴보면

Tensorflow 에서 모든 변수는 Tensor 라는 단위로 처리됩니다. (그래서 Tensorflow 입니다.)

tf.constant 는 상수형 Tensor를 생성 해 주는 명령이고

tf.Session 은 텐서가 수행되는 환경을 캡슐화 해 줍니다.

결국 상수를 생성하여 Session안에 집어넣고 수행 해주는 구조가 되겠습니다.


또 다른 예제를 보면 아래와 같이 산술 연산도 가능합니다.

 a = tf.constant(10)
>>> b = tf.constant(32)
>>> print(sess.run(a + b))
42


제 PC에서 수행한 결과는 아래와 같습니다.


여기까지 하시면 기본 설치가 완료 됩니다.

수고하셨습니다.


다음에는 간단한 모형을 학습 시켜 보겠습니다.

gpu ¹öÀüµµ ½ÃµµÇØ º¸½ÃÁö¿ä.
CUDAµµ ¾Æ¸¶ compatibility 2.1Àΰ¡ ºÎÅÍ Áö¿øÇÒ °Ì´Ï´Ù.
°¡¼Ó Á¤µµµµ ´ë´ÜÈ÷ ±Ã±ÝÇÕ´Ï´Ù
Á¡Á¡ Èï¹Ì·Ó½À´Ï´Ù. ÂÉ±Ý ¾î·Æ±âµµ ÇÕ´Ï´Ù.
Àü¹®°¡ÀÇ Åº»ýÀÔ´Ï´Ù. È­ÀÌÆÃÀÔ´Ï´Ù.
¸µÅ©Çϳª ¿¬°áÇص帳´Ï´Ù
http://www.complexity.co.kr/?p=2476
     
nshhsn 2016-07
ÇØ º¸°í ½ÍÀºµ¥ Á¦ PCµéÀº Çϳª°°ÀÌ GPU°¡ ¾ø¾î¼­¿ä
º»°ÝÀûÀ¸·Î ÇÏ°Ô µÇ¸é Àåºñ¸¦ °®ÃâÅ×´Ï ±×¶§ ÇØ º¼ ¼ö ÀÖÀ» °Í °°½À´Ï´Ù.

¾È±×·¡µµ Titan X »ç°í ½Í¾î¼­ º¸´Âµ¥ °¡°ÝÀÌ ¸¸¸¸Ä¡ ¾Ê³×¿ä
¾ó¸¥ ¸Ó½Å·¯´×¿ë Ŭ¶ó¿ìµå ¼­ºñ½º°¡ ³ª¿À±â¸¦ ±â´Ù¸®°í ÀÖ½À´Ï´Ù.
Amazon¿¡¼­´Â GPU 4°³Â¥¸® instance°¡ À־
¾Æ½¬¿îµ¥·Î ¿©±â¼­ ÇØ º¼ ¼ö ÀÖÀ» °Í °°½À´Ï´Ù.

°¨»çÇÕ´Ï´Ù.
          
computing capability 3.5ÀÌ»óÀÌ¸é °¡µ¿µÈ´Ù³×¿ä.
https://developer.nvidia.com/cuda-gpus
titan ¾Æ´Ï¶óµµ Àú·ÅÇÏ°Ô µ¹¸± ¼öµµ ÀÖ´Â ¸ð¾çÀÔ´Ï´Ù.
gtx 950 ÀÌ»óÀÌ¸é µÇ´Â °ÍÀ¸·Î ³ª¿É´Ï´Ù.
gtx950¸¸Çصµ Äھ 768°³ÀÔ´Ï´Ù.
     
ÀåÇÑÁÖ 2016-10
AMDÀÇ ½ºÆ®¸²ÇÁ·Î¼¼¼­·Î´Â ºÒ°¡´ÉÇϳª¿ä?
          
nshhsn 2016-10
GpuÂÊÀº nvidia ¸¸ µÇÁö¸¸
CpuÂÊÀº Á¦¾àÀÌ ÀÖ´Ù´Â ¾ê±â¸¦ µèÁö ¸ø Çß½À´Ï´Ù
Á¦ »ý°¢¿¡´Â ¸®´ª½º¸¸ ¼³Ä¡ µÈ´Ù¸é ¹®Á¦ ¾øÀ»°Í °°½À´Ï´Ù
          
amd cpuÀÇ  ³»Àå gpu ¸»¾¸À̽Ű¡¿ä?
±×·¸´Ù¸é ÀÌ °æ¿ì´Â nvidia°¡ ¾Æ´Õ´Ï´Ù. ¾Æ¸¶ ati
ati´Â ¾ÊµË´Ï´Ù.
               
ÀåÇÑÁÖ 2016-10
AMDÀÇ GPU Áï ATIÀÇ ±×·¡ÇÈÄ«µå±º¿¡ ÀÖ´Â ÄíµåÄÚ¾î = ½ºÆ®¸² ÇÁ·Î¼¼¼­¸¦ ¸»ÇÑ°Ì´Ï´Ù. Äí´ÙÄÚ¾î¿Í °°Àº ÆÄÁ¾ÀÔ´Ï´Ù. ±×·±µ¥ ´ëºÎºÐ Áö¿ø¾ÈÇÏ´Â°Í °°´õ¶ó°í¿ä.. À̹ø RXÁ¦Ç°µé¿¡ ½ºÆ®¸² ÇÁ·Î¼¼¼­°¡ Á¦°ÅµÈ°Í °°½À´Ï´Ù..
                    
atiÀÇ gpu´Â CUDA¸¦ Áö¿øÇÏÁö ¾Ê½À´Ï´Ù. TensorFlow´Â ±âº»ÀûÀ¸·Î CUDA ±â¹ÝÀ̱⠶§¹®¿¡
ati ±â¹Ý gpu´Â TensorFlow »ç¿ëÇÒ ¼ö ¾ø½À´Ï´Ù. ati¿ëÀº openCLÀÌ Àִµ¥ TensorFlow´Â
openCL Áö¿øÇÏÁö ¾Ê½À´Ï´Ù. ¾ÕÀ¸·Î´Â Áö¿øµÉ °ÍÀ¸·Î º¾´Ï´Ù¸¸..
hyhyhy 2016-12
sudo pip install --upgrade $TF_BINARY_URLÀ» ÀÔ·ÂÇϴϱî
The directory '/home/USERRR/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/home/USERRR/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
tensorflow-0.9.0-cp35-cp35m-linux_x86_64.whl is not a supported wheel on this platform.
You are using pip version 8.1.1, however version 9.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.¶ó°í ¶ß¸é¼­ ÅÙ¼­Ç÷ο켳ġ°¡ µÇÁö¾Ê½À´Ï´Ù.. ¿µ¾î°íÀÚ¶ó ¸·¸·Çϳ׿ä. ¾î¶»°Ô ÇϸéµÇ´ÂÁö..

USERRR@PC-GOGO:~$ python
Python 2.7.12 (default, Nov 19 2016, 06:48:10)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named tensorflow


Á¦¸ñPage 10/28
2020-07   6605   FOXBI
2020-07   5287   FOXBI
2020-07   5659   FOXBI
2020-07   5887   FOXBI
2020-07   7928   ocarina
2020-06   10570   ¸ÞÆ®
2020-06   8602   FOXBI
2020-06   21937   ±¤ÁÖIµÎ±ÙÀÌ
2020-06   8386   ĵÀ§µå
2020-05   12238   isaiah
2020-05   11131   ³«¿±À̽½1
2020-04   12557   ¸¸¼®±º
2020-04   16494   ½ºÄµl¹ÎÇö±â
2020-04   9185   ¶Ë¶º¾î¸®
2020-02   10194   ½Å¿µÁø
2020-02   8233   ȲÁø¿ì
2020-02   6870   ¸¸¼®±º
2020-02   6238   ±èÁØ¿¬
2020-02   10652   ÅëÅë9
2020-01   9512   »ïÀ°°ø¾ßµå