레이블이 keras인 게시물을 표시합니다. 모든 게시물 표시
레이블이 keras인 게시물을 표시합니다. 모든 게시물 표시

2018년 4월 5일 목요일

AC922 Redhat에 Theano와 Keras 설치 및 테스트

AC922 Redhat에 python 3.6을 설치하는 것은 다음과 같이 Anaconda를 설치하는 것이 가장 간단하고 가장 편리합니다.

[user1@ac922 ~]$ wget https://repo.continuum.io/archive/Anaconda3-5.1.0-Linux-ppc64le.sh    (python 2.x을 설치하려는 경우는 Anaconda3 대신 Anaconda2로 대체)

[user1@ac922 ~]$ chmod a+x Anaconda3-5.1.0-Linux-ppc64le.sh
[user1@ac922 ~]$ ./Anaconda3-5.1.0-Linux-ppc64le.sh

이렇게 설치가 끝나면 거기에 딸린 pip를 통해서 쉽게 theano와 keras를 설치할 수 있습니다.

[user1@ac922 ~]$ which python
~/anaconda3/bin/python
[user1@ac922 ~]$ python --version
Python 3.6.4 :: Anaconda, Inc.

Keras 2.0.9를 골라서 설치하려면 다음과 같이 하시면 됩니다. 

[user1@ac922 ~]$ pip install keras==2.0.9
Collecting keras==2.0.9
  Using cached Keras-2.0.9-py2.py3-none-any.whl
Requirement already satisfied: pyyaml in ./anaconda3/lib/python3.6/site-packages (from keras==2.0.9)
Requirement already satisfied: scipy>=0.14 in ./anaconda3/lib/python3.6/site-packages (from keras==2.0.9)
Requirement already satisfied: numpy>=1.9.1 in ./anaconda3/lib/python3.6/site-packages (from keras==2.0.9)
Requirement already satisfied: six>=1.9.0 in ./anaconda3/lib/python3.6/site-packages (from keras==2.0.9)
Installing collected packages: keras
Successfully installed keras-2.0.9

Keras를 테스트해보시려면 먼저 tensorflow를 설치하셔야 합니다.  그건 이미 올려둔 아래 posting에 따라 설치하시면 됩니다.

http://hwengineer.blogspot.kr/2018/04/ac922-redhat-74-python-36-tensorflow.html

이제 아래와 같이 keras에서 GPU를 사용하는지 확인해봅니다.

[user1@ac922 ~]$ python
Python 3.6.4 |Anaconda, Inc.| (default, Feb 11 2018, 08:19:13)
[GCC 7.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.

>>> from keras import backend as K
/home/user1/anaconda3/lib/python3.6/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
  from ._conv import register_converters as _register_converters
Using TensorFlow backend.
....
tensorflow/core/common_runtime/gpu/gpu_device.cc:1195] Creating TensorFlow device (/device:GPU:3) -> (device: 3, name: Tesla V100-SXM2-16GB, pci bus id: 0035:04:00.0, compute capability: 7.0)

>>> K.tensorflow_backend._get_available_gpus()
['/device:GPU:0', '/device:GPU:1', '/device:GPU:2', '/device:GPU:3']

잘 되는 것을 확인하실 수 있습니다.


이제 theano를 설치합니다.  먼저, theano 1.0.0이 GPU를 쓰기 위해서는 pygpu 0.7 이상을 설치해야 합니다.  기본적으로 설치되는 pygpu는 0.6.9이므로, 이걸 그대로 사용하면 다음과 같은 error를 겪게 됩니다.

ERROR (theano.gpuarray): pygpu was configured but could not be imported or is too old (version 0.7 or higher required)

따라서 먼저 pygpu 0.7을 설치해야 하는데, 이건 다음과 같이 libgpuarray라는 package를 설치하면 됩니다.

[user1@ac922 ~]$ git clone https://github.com/Theano/libgpuarray.git

[user1@ac922 ~]$ cd libgpuarray

[user1@ac922 libgpuarray]$ cmake3 .

[user1@ac922 libgpuarray]$ make && sudo make install

이러면 libgpuarray.so가 /usr/local/lib 밑에 설치됩니다.  이것이 우선적으로 loading되도록 LD_LIBRARY_PATH를 아래와 같이 설정합니다.

[user1@ac922 ~]$ export LD_LIBRARY_PATH=/usr/local/lib:/usr/local/cuda-9.1/lib64:/usr/lib:/usr/lib64

그리고 python binding을 위해 아래와 같이 마무리합니다.

[user1@ac922 libgpuarray]$ python setup.py install

이제 pip list를 해보면 다음과 같이 pygpu가 설치된 것을 보실 수 있습니다.

[user1@ac922 ~]$ pip list --format=columns | grep pygpu
pygpu                              0.7.5+11.g04c2892.dirty

이제 theano를 설치합니다.   최신 버전은 1.0.1입니다만 여기서는 1.0.0을 선택합니다.

[user1@ac922 ~]$ pip install theano==1.0.0
Collecting theano==1.0.0
  Downloading Theano-1.0.0.tar.gz (2.9MB)
    100% |████████████████████████████████| 2.9MB 390kB/s
Requirement already satisfied: numpy>=1.9.1 in ./anaconda3/lib/python3.6/site-packages (from theano==1.0.0)
Requirement already satisfied: scipy>=0.14 in ./anaconda3/lib/python3.6/site-packages (from theano==1.0.0)
Requirement already satisfied: six>=1.9.0 in ./anaconda3/lib/python3.6/site-packages (from theano==1.0.0)
Building wheels for collected packages: theano
  Running setup.py bdist_wheel for theano ... done
  Stored in directory: /home/user1/.cache/pip/wheels/e1/38/41/29fead4ea90d8fb9e23af0ba80d24222f8ba6effe93896ecbf
Successfully built theano
Installing collected packages: theano
Successfully installed theano-1.0.0

여기서 끝나는 것이 아니라, theano의 환경 설정을 아래와 같이 해줘야 합니다.

[user1@ac922 ~]$ vi ~/.theanorc
[global]
device = cuda0
floatX = float32

[cuda]
cuda.root = /usr/local/cuda-9.1

[dnn]
library_path = /usr/local/cuda-9.1/targets/ppc64le-linux/lib
include_path = /usr/local/cuda-9.1/targets/ppc64le-linux/include
enabled = True


확인은 다음과 같이 합니다.

[user1@ac922 ~]$ pip list --format=columns | grep -e 'Keras\|Theano'
Keras                              2.0.9
Theano                             1.0.0


확인차 아래와 같이 연습용 theano sample을 하나 돌려보겠습니다. 

[user1@ac922 files]$ vi theano_ex1.py
from theano import function, config, shared, tensor
import numpy
import time
vlen = 10 * 30 * 768  # 10 x #cores x # threads per core
iters = 1000
rng = numpy.random.RandomState(22)
x = shared(numpy.asarray(rng.rand(vlen), config.floatX))
f = function([], tensor.exp(x))
print(f.maker.fgraph.toposort())
t0 = time.time()
for i in range(iters):
    r = f()
t1 = time.time()
print("Looping %d times took %f seconds" % (iters, t1 - t0))
print("Result is %s" % (r,))
if numpy.any([isinstance(x.op, tensor.Elemwise) and
              ('Gpu' not in type(x.op).__name__)
              for x in f.maker.fgraph.toposort()]):
    print('Used the cpu')
else:
    print('Used the gpu')

여기서 "device=cpu" 또는 "device=cuda#"이라는 THEANO_FLAGS를 이용하여 CPU 또는 GPU를 쓰도록 제어할 수 있습니다.   먼저 CPU로 연산하도록 하는 예입니다.

[user1@ac922 files]$ time THEANO_FLAGS=device=cpu python theano_ex1.py
[Elemwise{exp,no_inplace}(<TensorType(float64, vector)>)]
Looping 1000 times took 6.145363 seconds
Result is [ 1.23178032  1.61879341  1.52278065 ...,  2.20771815  2.29967753
  1.62323285]
Used the cpu

real    0m6.787s
user    0m6.739s
sys     0m0.050s

이제 dev=cuda#으로 해봅니다.

[user1@ac922 files]$ time THEANO_FLAGS=device=cuda0 python theano_ex1.py
Traceback (most recent call last):
  File "__init__.pxd", line 1011, in numpy.import_array
RuntimeError: module compiled against API version 0xc but this version of numpy is 0xb
...

억, error입니다.   Googling해보면 이는 numpy의 버전이 낮아서 생기는 문제입니다.  다음과 같이 버전을 높여주면 됩니다.

[user1@ac922 ~]$ pip install numpy --upgrade
Collecting numpy
Installing collected packages: numpy
  Found existing installation: numpy 1.13.3
    Uninstalling numpy-1.13.3:
      Successfully uninstalled numpy-1.13.3
Successfully installed numpy-1.14.2

이제 다시 수행해봅니다. 

[user1@ac922 files]$ time THEANO_FLAGS=device=cuda0 python theano_ex1.py
Using cuDNN version 7005 on context None
Mapped name None to device cuda0: Tesla V100-SXM2-16GB (0004:04:00.0)
[GpuElemwise{exp,no_inplace}(<GpuArrayType<None>(float32, vector)>), HostFromGpu(gpuarray)(GpuElemwise{exp,no_inplace}.0)]
Looping 1000 times took 0.107720 seconds
Result is [1.2317803 1.6187935 1.5227807 ... 2.2077181 2.2996776 1.623233 ]
Used the gpu

real    0m4.753s
user    0m2.746s
sys     0m2.001s

잘 됩니다.  CPU를 이용하는 것보다 당연히 더 빠릅니다.  아까 $HOME/.theanorc에 device를 cuda0로 해놓았기 때문에, 사실 여기서는 THEANO_FLAGS를 따로 지정하지 않아도 default로 cuda0, 즉 GPU 0번을 이용하여 수행됩니다.

[user1@ac922 files]$ time python theano_ex1.py
Using cuDNN version 7005 on context None
Mapped name None to device cuda0: Tesla V100-SXM2-16GB (0004:04:00.0)
[GpuElemwise{exp,no_inplace}(<GpuArrayType<None>(float32, vector)>), HostFromGpu(gpuarray)(GpuElemwise{exp,no_inplace}.0)]
Looping 1000 times took 0.139874 seconds
Result is [1.2317803 1.6187935 1.5227807 ... 2.2077181 2.2996776 1.623233 ]
Used the gpu

real    0m4.725s
user    0m2.656s
sys     0m2.057s

혹시 더 최신의 Theano를 source로부터 직접 build하고 싶으시면 다음과 같이 하시면 됩니다.

[user1@ac922 files]$ pip install -U git+https://github.com/Theano/Theano.git
Collecting Theano from git+https://github.com/Theano/Theano.git#egg=Theano
  Cloning https://github.com/Theano/Theano.git to /tmp/pip-build-wpolv6wn/Theano
Collecting numpy>=1.9.1 (from Theano)
  Downloading numpy-1.14.2.zip (4.9MB)
    100% |████████████████████████████████| 4.9MB 232kB/s
Collecting scipy>=0.14 (from Theano)
  Downloading scipy-1.0.1.tar.gz (15.5MB)
    100% |████████████████████████████████| 15.5MB 75kB/s
Requirement already up-to-date: six>=1.9.0 in /home/user1/anaconda3/lib/python3.6/site-packages (from Theano)
Building wheels for collected packages: numpy, scipy
  Running setup.py bdist_wheel for numpy ... done
  Stored in directory: /home/user1/.cache/pip/wheels/7d/03/d4/050b7a6ff45c26a43b5a5b9e4ad1983b35b2aa1df42d44b5b1
  Running setup.py bdist_wheel for scipy ... done
  Stored in directory: /home/user1/.cache/pip/wheels/bc/15/5b/4b524f3adb3be2b81d59001f2b06ac758b7b73e77b4345a8b7
Successfully built numpy scipy
Installing collected packages: numpy, scipy, Theano
  Found existing installation: numpy 1.13.3
    Uninstalling numpy-1.13.3:
      Successfully uninstalled numpy-1.13.3
  Found existing installation: scipy 1.0.0
    Uninstalling scipy-1.0.0:
      Successfully uninstalled scipy-1.0.0
  Found existing installation: Theano 1.0.0
    Uninstalling Theano-1.0.0:
      Successfully uninstalled Theano-1.0.0
  Running setup.py install for Theano ... done
Successfully installed Theano-1.0.1+70.gd9b8a7c numpy-1.14.2 scipy-1.0.1

[user1@ac922 files]$ pip list --format=columns | grep -i theano
Theano                             1.0.1+70.gd9b8a7c


2017년 5월 15일 월요일

Minsky 서버에 한국어 text 분석을 위한 기초 환경 (gensim, keras, KoNLPy, MeCab) 및 Oracle client 버전 설치하기

Minsky 서버에 gensim 2.0.0, keras 2.0.4, KoNLPY, MeCab 및 Oracle client 버전을 설치하는 방법을 정리했습니다.

gensim은 python library로서 document로부터 어의(semantics)를 추출하는데 사용됩니다.  pip 명령으로 설치하면 되는데, 특정 버전을 설치하기 위해서는 다음과 같이 '==version'의 형태를 사용하면 됩니다.

u0017496@sys-87250:~$ sudo pip install gensim==2.0.0
Collecting gensim==2.0.0
  Downloading gensim-2.0.0.tar.gz (14.1MB)
    100% |████████████████████████████████| 14.2MB 70kB/s
Requirement already satisfied (use --upgrade to upgrade): numpy>=1.3 in /usr/lib/python2.7/dist-packages (from gensim==2.0.0)
Requirement already satisfied (use --upgrade to upgrade): scipy>=0.7.0 in /usr/local/lib/python2.7/dist-packages (from gensim==2.0.0)
Requirement already satisfied (use --upgrade to upgrade): six>=1.5.0 in /usr/lib/python2.7/dist-packages (from gensim==2.0.0)
Requirement already satisfied (use --upgrade to upgrade): smart_open>=1.2.1 in /usr/local/lib/python2.7/dist-packages (from gensim==2.0.0)
Requirement already satisfied (use --upgrade to upgrade): boto>=2.32 in /usr/lib/python2.7/dist-packages (from smart_open>=1.2.1->gensim==2.0.0)
Requirement already satisfied (use --upgrade to upgrade): bz2file in /usr/local/lib/python2.7/dist-packages (from smart_open>=1.2.1->gensim==2.0.0)
Requirement already satisfied (use --upgrade to upgrade): requests in /usr/lib/python2.7/dist-packages (from smart_open>=1.2.1->gensim==2.0.0)
Installing collected packages: gensim
  Running setup.py install for gensim ... done
Successfully installed gensim-2.0.0


keras는 high-level neural networks API로서, python으로 작성되어 tensorflow나 theano 위에서 사용됩니다.  역시 pip 명령으로 설치하면 됩니다.

u0017496@sys-87250:~$ sudo pip install keras==2.0.4
Collecting keras==2.0.4
  Downloading Keras-2.0.4.tar.gz (199kB)
    100% |████████████████████████████████| 204kB 2.5MB/s
Collecting theano (from keras==2.0.4)
  Downloading Theano-0.9.0.tar.gz (3.1MB)
    100% |████████████████████████████████| 3.1MB 310kB/s
Collecting pyyaml (from keras==2.0.4)
  Downloading PyYAML-3.12.tar.gz (253kB)
    100% |████████████████████████████████| 256kB 2.3MB/s
Requirement already satisfied (use --upgrade to upgrade): six in /usr/lib/python2.7/dist-packages (from keras==2.0.4)
Requirement already satisfied (use --upgrade to upgrade): numpy>=1.9.1 in /usr/lib/python2.7/dist-packages (from theano->keras==2.0.4)
Requirement already satisfied (use --upgrade to upgrade): scipy>=0.14 in /usr/local/lib/python2.7/dist-packages (from theano->keras==2.0.4)
Installing collected packages: theano, pyyaml, keras
  Running setup.py install for theano ... done
  Running setup.py install for pyyaml ... done
  Running setup.py install for keras ... done
Successfully installed keras-2.0.4 pyyaml-3.12 theano-0.9.0


KoNLPy는 Korean National Language Processing in python으로서, 글자 그대로 한국어를 python으로 처리할 때 사용됩니다.  '콘엔엘파이'라고 읽습니다.  이 역시 pip로 설치합니다.

u0017496@sys-87250:~$ sudo pip install konlpy
Collecting konlpy
  Downloading konlpy-0.4.4-py2.py3-none-any.whl (22.5MB)
    100% |████████████████████████████████| 22.5MB 46kB/s
Collecting JPype1>=0.5.7; python_version == "2.6" or python_version == "2.7" (from konlpy)
  Downloading JPype1-0.6.2.tar.gz (147kB)
    100% |████████████████████████████████| 153kB 3.7MB/s
Installing collected packages: JPype1, konlpy
  Running setup.py install for JPype1 ... done
Successfully installed JPype1-0.6.2 konlpy-0.4.4


MeCab은 일본어 문자의 형태학적 분석을 위해 사용되는 패키지인데, 이는 Ubuntu OS 안에 다음과 같은 여러가지 package로 포함되어 있습니다.  다음과 같이 apt-get 명령으로 설치하면 됩니다.

u0017496@sys-87250:~$ sudo apt-get install mecab-utils libmecab-dev libmecab2 libmecab-jni ruby-mecab mecab-ipadic python-mecab libmecab-java mecab
...
reading /usr/share/mecab/dic/ipadic/Postp-col.csv ... 91
emitting double-array: 100% |###########################################|
reading /usr/share/mecab/dic/ipadic/matrix.def ... 1316x1316
emitting matrix      : 100% |###########################################|

done!
Setting up python-mecab (0.99.6-1build2) ...
Setting up ruby-mecab (0.99.6-2build7) ...
Processing triggers for libc-bin (2.23-0ubuntu7) ...

위에서 보셨다시피 python-mecab이 이미 포함되어 있으므로, pip 명령으로 mecab-python을 설치하려고 해보면 이미 충족되었다고 나옵니다.

u0017496@sys-87250:~$ sudo pip install mecab-python
Requirement already satisfied (use --upgrade to upgrade): mecab-python in /usr/lib/python2.7/dist-packages


alien은 Ubuntu에서 주로 사용되는 debian 패키지와 Redhat에서 주로 사용되는 rpm 패키지 등을 상호 변환해주는 tool이며, Ubuntu OS에 기본 패키지로 포함되어 있습니다.  따라서 다음과 같이 apt-get 명령으로 설치만 해주면 됩니다.  이러면 rpm 등 필요한 패키지를 자동으로 함께 설치합니다.

u0017496@sys-87250:~$ sudo apt-get install alien

---------------------------

이제 Oracle client 버전을 설치해보겠습니다.   Oracle DBMS의 서버 버전은 Minsky 서버의 processor 아키텍처인 ppc64le에 포팅이 되어 있지 않고, 앞으로도 그럴 일은 없어 보입니다.  (IBM에서는 왜 Linux에 값비싼 Oracle을 쓰느냐, Linux를 쓰려거든 당연히 MariaDB나 PostgreSQL, MySQL 등의 오픈소스 DBMS를 쓰는 것이 맞는 방향이다 라고 말하고 있습니다.)  그러나 Linux건 UNIX건 Oracle DBMS가 차지하는 막대한 비중을 생각할 때, Linux 서버들이 Oracle 서버에는 접속할 필요가 있다고 보았는지 Oracle client 버전은 최신 12 버전까지 모두 포팅되어 있습니다.  아래의 Oracle site에서 무료 회원 가입하고 download 받으시면 됩니다.

http://www.oracle.com/technetwork/topics/linux-power-le-2835260.html



u0017496@sys-87250:/usr/local$ ls ~/
instantclient-basic-linux.leppc64-12.1.0.2.0.zip
instantclient-jdbc-linux.leppc64-12.1.0.2.0.zip
instantclient-odbc-linux.leppc64-12.1.0.2.0.zip
instantclient-sdk-linux.leppc64-12.1.0.2.0.zip
instantclient-sqlplus-linux.leppc64-12.1.0.2.0.zip

이것들을 unzip으로 다음과 같이 풀어놓으면 됩니다.

u0017496@sys-87250:/usr/local$ sudo unzip ~/instantclient-basic-linux.leppc64-12.1.0.2.0.zip
u0017496@sys-87250:/usr/local$ sudo unzip ~/instantclient-jdbc-linux.leppc64-12.1.0.2.0.zip
u0017496@sys-87250:/usr/local$ sudo unzip ~/instantclient-odbc-linux.leppc64-12.1.0.2.0.zip
u0017496@sys-87250:/usr/local$ sudo unzip ~/instantclient-sdk-linux.leppc64-12.1.0.2.0.zip
u0017496@sys-87250:/usr/local$ sudo unzip ~/instantclient-sqlplus-linux.leppc64-12.1.0.2.0.zip

내용을 살펴보면 다음과 같이 sqlplus 실행파일과 각종 shared object (so) 파일들이 들어 있습니다.

u0017496@sys-87250:/usr/local/instantclient_12_1$ ls
adrci                  libheteroxa12.so  libsqlplus.so             sdk
BASIC_README           libnnz12.so       libsqora.so.12.1          sqlplus
genezi                 libocci.so.12.1   ODBC_IC_Readme_Unix.html  SQLPLUS_README
glogin.sql             libociei.so       odbc_update_ini.sh        uidrvci
help                   libocijdbc12.so   ojdbc6.jar                xstreams.jar
JDBC_README            libons.so         ojdbc7.jar
libclntshcore.so.12.1  liboramysql12.so  orai18n.jar
libclntsh.so.12.1      libsqlplusic.so   orai18n-mapping.jar


이제 LD_LIBRARY_PATH와 PATH 등 환경변수를 설정하고 sqlplus를 구동해보겠습니다.

u0017496@sys-87250:/usr/local/instantclient_12_1$ cd
u0017496@sys-87250:~$ export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/instantclient_12_1
u0017496@sys-87250:~$ export PATH=$PATH:/usr/local/instantclient_12_1

u0017496@sys-87250:~$ which sqlplus
/usr/local/instantclient_12_1/sqlplus

u0017496@sys-87250:~$ sqlplus
sqlplus: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory

놀랍게도 error가 납니다.  놀라실 것 없습니다.  AIX 시절의 기억을 살려보면, Oracle은 async I/O를 사용하지요.  Linux에도 aio library가 있고, apt-get 명령으로 설치할 수 있습니다.

u0017496@sys-87250:~$ sudo apt-get install libaio-dev libaio1 libaio1-dbg

이제 다시 sqlplus를 구동해보겠습니다.  잘 되는 것을 보실 수 있습니다.

u0017496@sys-87250:~$ sqlplus

SQL*Plus: Release 12.1.0.2.0 Production on Sun May 14 21:10:37 2017

Copyright (c) 1982, 2015, Oracle.  All rights reserved.

Enter user-name: