Comparison between Multiprocessing and Multithreading in Python
Comparison between Multiprocessing and Multithreading in Python
See
github.com
Python Shared Memory
データリードがボトルネックになる時があるので,IPC経由でデータを渡す場合とSharedMemory経由でデータを渡す場合で比較してみた.
SSL Ladder Network
Semi-Supervised LearningにおけるLadder Networkを実装した.Permuration-Invariant MNISTで,100ラベルで98%の正答率が出たので,大体再現出来たと言える.
References
- Rasmus, A., Valpola, H., Honkala, M., Berglund, M., & Raiko, T. (2015). Semi-Supervised Learning with Ladder Networks. Neural and Evolutionary Computing; Learning; Machine Learning.
- Pezeshki, M., Fan, L., Brakel, P., Courville, A., & Bengio, Y. (2016). Deconstructing the Ladder Network Architecture (icml). Icml, 48, 1–15.
Cython基本
Cythonでできるのこと
要するに,pythonぽく書けるので生産性が高くて,高速化も可能.
Installation
pip install cython
コンパイル方法
2通りある.
- pyximport: 外部Cライブラリや特殊なビルド設定が必要ない場合.
- setup.py: 上記以外の場合.自作のCライブラリに依存している等.Makefileと思っておけばいい.
pyximport
import pyximport; pyximport.install(pyimport = True) import (basename ${filename.pyx} .pyx)
な感じで使う.
setup.py
from distutils.core import setup from distutils.extension import Extension from Cython.Distutils import build_ext setup( cmdclass = {'build_ext': build_ext}, ext_modules = [Extension("helloworld", ["helloworld.pyx"])] )
な感じのコードを書く.
build_extはbuildコマンドで,Extensionは,この名前の.soができる.
使い方は,
python setup.py build_ext [--inplace]
静的片付け
def cdef type func(type param) # 関数引数の場合 cdef type param0, param1, ... # 変数の場合
とかく.
サンプルを書いた
結果
kzk@localhost:~/languages/cython/speedup_exmaple $ python main.py 666666.686668 Python: 0.354106903076 [s] 666666.686668 Cython: 0.196939945221 [s] 666666.686668 cdef Cython: 0.0480411052704 [s] 666666.686668 cdef v2 Cython: 0.00502610206604 [s] ### Ratio Comparison### Python: 1.0 Cython: 1.79804509785 cdef Cython: 7.37091499213 cdef v2 Cython: 70.4535837958
すげーはやい.
C/C++ APIの複雑なラップ等は,チュートリアル,ユーザガイド,リファレンスを見ながらやればいい.
現状Cythonを使うというユースケースで思いつくのは,
- pythonのbottle neck codeを置き換える
- GPU Computing
Chainer (1.12.0)をさわってみた
わけあって,Chainerを触らなくてはならなくなった,再び触ってみた.
Installation
環境
- Chainer: 1.12
- OS: Ubuntu15.04
- CUDA: 7.5
- Compiler: cpp-4.9, g++-4.9, gcc-4.9, cc-4.9
- cudnn: v5
CUDA7.5の場合,gcc4.9を使わないとならないようなので注意.