2016년 3월 30일 수요일

(가상환경) Anaconda commands

conda -V
conda info --envs
conda update conda
conda create -n yourenvname python=2.7 anaconda
source activate yourenvname #windows에서는 source 생략
deactivate
conda remove -n yourenvname --all

conda info -e
conda install -n yourenvname [package]

windows에서 새로운 가상환경을 생성 때는 가상환경 생성 후, 그 환경 내에서도 mingw, libpython 설치 필요.

For ubuntu,
source bin/activate ~/anaconda2/
conda info --envs
...
source bin/activate /home/user/anaconda2/envs/keras1.0.1

theano/keras 실행 시는
THEANO_FLAGS=floatX=float32,device=gpu python

bash shell prompt 축약
PS1='\u:\W\$ '


(Ref.)
1. http://uoa-eresearch.github.io/eresearch-cookbook/recipe/2014/11/20/conda/
2. http://antilibrary.org/m/post/766
3. http://askubuntu.com/questions/145618/how-can-i-shorten-my-command-line-bash-prompt

2016년 3월 16일 수요일

(Windows) keras 설치

Backend를 Theano로 사용하므로 아래 설명은 theano설치도 포함한다.

먼저, cuda(v7.5), vs2013, git설치
git은 설치 중 옵션에서 "Run Git from the WIndows command prompt" 선택

TDM GCC x64 설치
Anaconda x64 설치

cmd 창을 관리자 모드로 연다(방법은 아래).
(or open Anaconda prompt)
conda update conda
conda update --all
conda install mingw libpython

Install latest version of Theano
git clone git://github.com/Theano/Theano.git
(or pip install git+git://github.com/Theano/Theano.git)
cd Theano
python setup.py develop

Install keras
git clone https://github.com/fchollet/keras
(pip install keras)
cd keras
python setup.py develop

Test
cd examples
ipython mnist_mlp.py


제어판->시스템->고급시스템설정->환경변수->사용자변수에 가서
PATH 변수에 cuda의 nvcc가 있는 bin폴더 추가
THEANO_FLAGS 변수 만들고 다음 사항 추가
"floatX=float32,device=gpu,nvcc.fastmath=True"


cmd 창을 관리자 모드로 여는법
시작->프로그램 및 파일검색
cmd 입력
리스트에 나타난 cmd를 클릭해서 “관리자모드로 시작”.


설치를 확인하는 법
cmd창에서 
where cl
where gcc
where nvcc
where g++


cuDNN을 같이 설치하면 학습 속도가 더욱 빨라진다. 
nvidia에서 cuDNN 4.0을 다운 받아 압축을 풀면 bin, include, lib의 3개의 폴더가 있다. 
내부의 파일들을   
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v7.5
아래의 해당 위치에 옮겨주면 된다.



출처:
1. Setup a Deep Learning Environment on Windows (Theano & Keras with GPU Enabled)
2. How to install Keras and Theano in Anaconda Python 2.7 in Windows
3. FunMV
4. Installing Keras, Theano and Dependencies on Windows 10












2016년 3월 2일 수요일

tflearn 설치

Mac의 anaconda 가상환경 아래에 tflearn을 설치한다. 

을 참고해서 먼저 tensorflow를 설치후, "pip install tflearn"으로 설치 하였으나, 

titanic 예제를 실행해 보니 포함되지 않은 lib가 많아 

git에 들어가서 소스를 다운 받은 후에 

python setup.py install

명령으로 설치하였다. 




2016년 3월 1일 화요일

keras test

# 레이어 중간 값을 확인할 수 있는 방법
from keras.layers.core import Dense, Dropout, Activation, Flatten
from keras.layers.convolutional import Convolution2D
from keras.layers.normalization import BatchNormalization
from keras import backend as K
from keras.layers import Input
from keras.datasets import mnist, cifar10

(X_train, y_train), (X_test, y_test) = cifar10.load_data()
X_train = X_train.reshape(X_train.shape[0], 3, 32, 32).astype('float32')
X_train = (X_train - np.mean(X_train))/np.std(X_train)

inputs = Input(shape=(3,32,32))
bn1 = BatchNormalization(axis=1)(inputs)
act1 = Activation('relu')(bn1)
conv1 = Convolution2D(32, 3, 3, border_mode='same')(act1)
func1 = K.function([inputs], [conv1]) #conv1대신 act1 등 입력가능
val1 = func1([X_train[0:1]])[0]  # [0]을 붙인 것은 val1이 list로 나오기 때문



# test용 입력을 만드는 방법
inp = np.random.randn(2,3,32,32)
rez = model.predict(inp[0:1])
rez.shape


# layers의 shape 출력
model.summay()




(Ref.)
[1] Keras FAQ.