Pylearn2的使用簡介

std1984發表於2014-07-30
環境: ubuntu 12.4

Pylearn2是基於theano上封裝的深度學習包。 它實現一些常見的模型,具體請參考: ,比theano在做實際的專案節約時間,只需要配置一些引數來實現模型的訓練。 
下面來講解實際的安裝和使用:

1. 安裝    jerry@hq:~$sudo pip install --upgrade --no-deps git+git://github.com/Theano/Theano.git --user

2. 下載Pylearn2
    jerry@hq:~$git clone git://github.com/lisa-lab/pylearn2.git 

3.  安裝pylearn2
    jerry@hq:~$cd pylearn2
    jerry@hq:~$sudo python setup.py develop --user

4. 測試安裝成功
    jerry@hq:~$python
    import pylearn2
    能載入包即安裝ok

5. 設定PYTHON2_DATA_PATH, PYLEARN2_VIEWR_COMMAND
    vi ~/.bashrc
    新增 
    export PYLEARN2_DATA_PATH=/u01/lisa/data
     export PYLEARN2_VIEWER_COMMAND=/usr/bin/eog

如何執行一個示例

1. 下載資料 
   cd /u01/lisa/data/cifar10
    wget ~kriz/cifar-10-python.tar.gz
   tar xvf cifar-10-python.tar.gz

2. 修改make_dataset.py檔案 ,指定路徑/u01/lisa/data/ (由於本機上/空間不足,只能把資料放在其它路徑上)
   jerry@hq:~$vi /home/jerry/pylearn2/pylearn2/scripts/tutorials/grbm_smd/make_dataset.py
  修改成這樣:
   """
    path = pylearn2.__path__[0]
    train_example_path = os.path.join(path, 'scripts', 'tutorials', 'grbm_smd')
    train.use_design_loc(os.path.join(train_example_path, 'cifar10_preprocessed_train_design.npy'))
    train_pkl_path = os.path.join(train_example_path, 'cifar10_preprocessed_train.pkl')
    """
    train_pkl_path = os.path.join('/u01/lisa/data/', 'cifar10_preprocessed_train.pkl')
    serial.save(train_pkl_path, train)

3. 對下載資料進行資料預處理
   python /home/jerry/pylearn2/pylearn2/scripts/tutorials/grbm_smd/make_dataset.py
    處理完後在目錄/u01/lisa/data下有一個檔案 cifar10_preprocessed_train.pkl,大概652M左右

4. 對資料進行訓練
   cd /u01/lisa/data
   python ~/pylearn2/pylearn2/scripts/train.py ~/pylearn2/pylearn2/scripts/tutorials/grbm_smd/cifar_grbm_smd.yaml

5. 檢視結果
    python ~/pylearn2/pylearn2/scripts/show_weights.py ~/pylearn2/pylearn2/scripts/tutorials/grbm_smd/cifar_grbm_smd.pkl    

   python ~/pylearn2/pylearn2/scripts/plot_monitor.py ~/pylearn2/pylearn2/scripts/tutorials/grbm_smd/cifar_grbm_smd.pkl

   python ~/pylearn2/pylearn2/scripts/print_monitor.py ~/pylearn2/pylearn2/scripts/tutorials/grbm_smd/cifar_grbm_smd.pkl

    python ~/pylearn2/pylearn2/scripts/summarize_model.py ~/pylearn2/pylearn2/scripts/tutorials/grbm_smd/cifar_grbm_smd.pkl


6. 直接檢視生成引數的檔案cifar_grbm_smd.pkl

載入模型檔案
>>> from pylearn2.utils import serial
>>> model = serial.load('/home/jerry/pylearn2/pylearn2/scripts/tutorials/grbm_smd/cifar_grbm_smd.pkl')
查下檔案結構
>>> dir(model)
獲取權重引數
>>> model.get_weights()
獲取引數名
>>> model.get_params()
獲取引數值
>>> model.get_param_values()

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/16582684/viewspace-1242503/,如需轉載,請註明出處,否則將追究法律責任。