46
30

More than 5 years have passed since last update.

普通のpython実行ファイル(argparseを含むファイル)をJupyter notebookで実行するときのメモ書き

Last updated at Posted at 2017-02-03

普通に
https://github.com/pfnet/chainer/blob/master/examples/mnist/train_mnist.py
などサンプルpythonファイルをjupyterで実行すると下記のようなエラーが出る.

usage: __main__.py [-h] [--batchsize BATCHSIZE] [--epoch EPOCH] [--gpu GPU]
                   [--out OUT] [--resume RESUME] [--unit UNIT]
__main__.py: error: unrecognized arguments: -f /run/user/2049/jupyter/kernel-84d4df02-bc59-4802-9d99-c3228bb4e71f.json
An exception has occurred, use %tb to see the full traceback.

SystemExit: 2


/home/mil/watanabe/.pyenv/versions/anaconda3-4.2.0/lib/python3.5/site-packages/IPython/core/interactiveshell.py:2889: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D.
  warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)

このようにargsまわりでエラーが出る.

そんなときは"easydict"を入れて,

pip install easydict

下記のように変更すれば手っ取り早く試せる.

sample.py
# parser = argparse.ArgumentParser(description='Chainer example: MNIST')
# parser.add_argument('--batchsize', '-b', type=int, default=100,
#                     help='Number of images in each mini-batch')
# parser.add_argument('--epoch', '-e', type=int, default=20,
#                     help='Number of sweeps over the dataset to train')
# parser.add_argument('--gpu', '-g', type=int, default=-1,
#                     help='GPU ID (negative value indicates CPU)')
# parser.add_argument('--out', '-o', default='result',
#                     help='Directory to output the result')
# parser.add_argument('--resume', '-r', default='',
#                     help='Resume the training from snapshot')
# parser.add_argument('--unit', '-u', type=int, default=1000,
#                     help='Number of units')
# args = parser.parse_args()
import easydict
args = easydict.EasyDict({
        "batchsize": 100,
        "epoch": 20,
        "gpu": 0,
        "out": "result",
        "resume": False,
        "unit": 1000
})
46
30
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
46
30