windows10環境でChainer2.0を動かすまでにハマった罠

#注意書き
先人が書いた記事や書籍を一部引用させていただきましたが、ディスる気は一切ないのでご了承ください :bow:

#cuDNNはオプションだからなくても動くよ
**→Chainer2.0からcupyが分離されたため、cuDNNも必須になった。**
cuDNNはディープラーニングの計算を高速かつ省メモリで行うライブラリ。

下記のような「cuDNNはオプションだからなくても動くよ」という記事や書籍はChainer2.0未満の時の情報なので注意！


>![q1.png](https://qiita-image-store.s3.amazonaws.com/0/189055/c2349440-376b-e764-07bb-e920a9effa29.png)

　

>![q2.png](https://qiita-image-store.s3.amazonaws.com/0/189055/64a79a8a-515e-9b45-dcfb-734e465863df.png)

　

>![q3.png](https://qiita-image-store.s3.amazonaws.com/0/189055/7dea2a22-e721-9af3-857a-a98e479f16f8.png)




# システム環境変数が少なすぎるよ

システム環境変数が少ない（パスが通ってない）事を起因とするエラーに色々ハマった（nvccらへんとか特に）。
[ploodstoneさんの記事](https://qiita.com/ploodstone/items/b279b0a80f821ef5a4c2)を参考にシステム環境変数を増やしたらうまく動いた。

* PATH

C:\Program Files (x86)\Microsoft Visual Studio\Shared\14.0\VC\bin
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\bin
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\libnvvp
C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common

<br>

* INCLUDE

C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\include
C:\Program Files (x86)\Windows Kits\10\Include\10.0.15086.0\ucrt
C:\Program Files (x86)\Microsoft Visual Studio\Shared\14.0\VC\include
C:\Program Files (x86)\Windows Kits\10\Include\10.0.15086.0\shared

<br>

* LIB

C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\lib\x64
C:\Program Files (x86)\Windows Kits\10\Lib\10.0.15086.0\um\x64
C:\Program Files (x86)\Windows Kits\10\Lib\10.0.15086.0\ucrt\x64
C:\Program Files (x86)\Microsoft Visual Studio\Shared\14.0\VC\lib




下記のような環境変数がやけに少ない記事はChainer2.0未満の時の情報なので注意！

>![q21.png](https://qiita-image-store.s3.amazonaws.com/0/189055/d9e86ff1-15a8-93f5-7db2-0929342f35eb.png)

　

>![q22.png](https://qiita-image-store.s3.amazonaws.com/0/189055/e2089405-2edb-e16e-2a67-0ffb4fbab2c8.png)





# progressbarがインストールできないよ



```
$ python seq2seq.py
Traceback (most recent call last):
  File "seq2seq.py", line 7, in <module>
    import progressbar
ModuleNotFoundError: No module named 'progressbar'
```

```
$ pip install progressbar -vvvv
Config variable 'Py_DEBUG' is unset, Python ABI tag may be incorrect
Config variable 'WITH_PYMALLOC' is unset, Python ABI tag may be incorrect
Collecting progressbar
  1 location(s) to search for versions of progressbar:
  * https://pypi.python.org/simple/progressbar/
  Getting page https://pypi.python.org/simple/progressbar/
  Looking up "https://pypi.python.org/simple/progressbar/" in the cache
  Current age based on date: 679
  Freshness lifetime from max-age: 600
  Freshness lifetime from request max-age: 600
  The cached response is "stale" with no etag, purging
  Starting new HTTPS connection (1): pypi.python.org
  "GET /simple/progressbar/ HTTP/1.1" 200 378
  Updating cache with response from "https://pypi.python.org/simple/progressbar/"
  Caching b/c date exists and max-age > 0
  Analyzing links from page https://pypi.python.org/simple/progressbar/
    Found link https://pypi.python.org/packages/06/ff/c6dbd25ba8ef7838114ac54f5812ed3a065bf5590f61bc7243276f740b33/progressbar-2.2.tar.gz#md5=8ea4e2c17a8ec9e7d153767c5f2a7b28 (from https://pypi.python.org/simple/progressbar/), version: 2.2
    Found link https://pypi.python.org/packages/0f/e1/f30b72ecabba259c7c38dd0eb944a173effced3fd7de2c9c2902bd6f649a/progressbar-2.3.tar.gz#md5=cb6359e54edbb5a1ede42f2b3da5fd75 (from https://pypi.python.org/simple/progressbar/), version: 2.3
    Found link https://pypi.python.org/packages/a7/9f/677b7705b66f17c02a21ee10b64de24af90303c59590fc0a1a5f0b42d598/progressbar-2.1.tar.gz#md5=a51d18b04fc752df66ea5aec7abb917f (from https://pypi.python.org/simple/progressbar/), version: 2.1
  Using version 2.3 (newest of versions: 2.1, 2.2, 2.3)
  Looking up "https://pypi.python.org/packages/0f/e1/f30b72ecabba259c7c38dd0eb944a173effced3fd7de2c9c2902bd6f649a/progressbar-2.3.tar.gz" in the cache
  Current age based on date: 679
  Freshness lifetime from max-age: 31557600
  The response is "fresh", returning cached response
  31557600 > 679
  Using cached progressbar-2.3.tar.gz
  Downloading from URL https://pypi.python.org/packages/0f/e1/f30b72ecabba259c7c38dd0eb944a173effced3fd7de2c9c2902bd6f649a/progressbar-2.3.tar.gz#md5=cb6359e54edbb5a1ede42f2b3da5fd75 (from https://pypi.python.org/simple/progressbar/)
  Running setup.py (path:C:\Users\xxx\AppData\Local\Temp\pip-build-0g4qhnk1\progressbar\setup.py) egg_info for package progressbar
    Running command python setup.py egg_info
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "C:\Users\xxx\AppData\Local\Temp\pip-build-0g4qhnk1\progressbar\setup.py", line 5, in <module>
        import progressbar
      File "C:\Users\xxx\AppData\Local\Temp\pip-build-0g4qhnk1\progressbar\progressbar\__init__.py", line 59, in <module>
        from progressbar.widgets import *
      File "C:\Users\xxx\AppData\Local\Temp\pip-build-0g4qhnk1\progressbar\progressbar\widgets.py", line 121, in <module>
        class FileTransferSpeed(Widget):
      File "C:\Users\xxx\Anaconda3\lib\abc.py", line 133, in __new__
        cls = super().__new__(mcls, name, bases, namespace)
    ValueError: 'format' in __slots__ conflicts with class variable
Cleaning up...
Command "python setup.py egg_info" failed with error code 1 in C:\Users\xxx\AppData\Local\Temp\pip-build-0g4qhnk1\progressbar\
Exception information:
Traceback (most recent call last):
  File "C:\Users\xxx\Anaconda3\lib\site-packages\pip\basecommand.py", line 215, in main
    status = self.run(options, args)
  File "C:\Users\xxx\Anaconda3\lib\site-packages\pip\commands\install.py", line 335, in run
    wb.build(autobuilding=True)
  File "C:\Users\xxx\Anaconda3\lib\site-packages\pip\wheel.py", line 749, in build
    self.requirement_set.prepare_files(self.finder)
  File "C:\Users\xxx\Anaconda3\lib\site-packages\pip\req\req_set.py", line 380, in prepare_files
    ignore_dependencies=self.ignore_dependencies))
  File "C:\Users\xxx\Anaconda3\lib\site-packages\pip\req\req_set.py", line 634, in _prepare_file
    abstract_dist.prep_for_dist()
  File "C:\Users\xxx\Anaconda3\lib\site-packages\pip\req\req_set.py", line 129, in prep_for_dist
    self.req_to_install.run_egg_info()
  File "C:\Users\xxx\Anaconda3\lib\site-packages\pip\req\req_install.py", line 439, in run_egg_info
    command_desc='python setup.py egg_info')
  File "C:\Users\xxx\Anaconda3\lib\site-packages\pip\utils\__init__.py", line 707, in call_subprocess
    % (command_desc, proc.returncode, cwd))
pip.exceptions.InstallationError: Command "python setup.py egg_info" failed with error code 1 in C:\Users\xxx\AppData\Local\Temp\pip-build-0g4qhnk1\progressbar\
```

pip install progressbarで一番最初に出てくるパッケージはpython3に対応していないのが原因。
python3の人はprogressbar2をインストールしよう。

```
$ pip install progressbar2
```

# UnicodeDecodeErrorが出るよ


```
Traceback (most recent call last):
  File "seq2seq.py", line 305, in <module>
    main()
  File "seq2seq.py", line 221, in main
    source_ids = load_vocabulary(args.SOURCE_VOCAB)
  File "seq2seq.py", line 162, in load_vocabulary
    word_ids = {line.strip(): i + 2 for i, line in enumerate(f)}
  File "seq2seq.py", line 162, in <dictcomp>
    word_ids = {line.strip(): i + 2 for i, line in enumerate(f)}
UnicodeDecodeError: 'cp932' codec can't decode byte 0x93 in position 372: illegal multibyte sequence
```

Windowsの標準出力であるCP932というエンコーディングに変換しようとしてコケているのが原因。
回避方法は色々あるのだが、とりあえずこのように書き換えればエラーは出なくなる。

```py:before.py
def count_lines(path):
    with open(path) as f:
        return sum([1 for _ in f])


def load_vocabulary(path):
    with open(path) as f:
        # +2 for UNK and EOS
        word_ids = {line.strip(): i + 2 for i, line in enumerate(f)}
    word_ids['<UNK>'] = 0
    word_ids['<EOS>'] = 1
    return word_ids
```

```py:after.py
def count_lines(path):
    with open(path,mode='r',encoding='utf-8') as f:
        return sum([1 for _ in f])


def load_vocabulary(path):
    with open(path,mode='r',encoding='utf-8') as f:
        # +2 for UNK and EOS
        word_ids = {line.strip(): i + 2 for i, line in enumerate(f)}
    word_ids['<UNK>'] = 0
    word_ids['<EOS>'] = 1
    return word_ids
```

# まだUnicodeDecodeErrorが出るよ

```
Traceback (most recent call last):
  File "C:\Users\xxx\Anaconda3\lib\site-packages\pip\basecommand.py", line 215, in main
    status = self.run(options, args)
  File "C:\Users\xxx\Anaconda3\lib\site-packages\pip\commands\install.py", line 335, in run
    wb.build(autobuilding=True)
  File "C:\Users\xxx\Anaconda3\lib\site-packages\pip\wheel.py", line 749, in build
    self.requirement_set.prepare_files(self.finder)
  File "C:\Users\xxx\Anaconda3\lib\site-packages\pip\req\req_set.py", line 380, in prepare_files
    ignore_dependencies=self.ignore_dependencies))
  File "C:\Users\xxx\Anaconda3\lib\site-packages\pip\req\req_set.py", line 634, in _prepare_file
    abstract_dist.prep_for_dist()
  File "C:\Users\xxx\Anaconda3\lib\site-packages\pip\req\req_set.py", line 129, in prep_for_dist
    self.req_to_install.run_egg_info()
  File "C:\Users\xxx\Anaconda3\lib\site-packages\pip\req\req_install.py", line 439, in run_egg_info
    command_desc='python setup.py egg_info')
  File "C:\Users\xxx\Anaconda3\lib\site-packages\pip\utils\__init__.py", line 676, in call_subprocess
    line = console_to_str(proc.stdout.readline())
  File "C:\Users\xxx\Anaconda3\lib\site-packages\pip\compat\__init__.py", line 75, in console_to_str
    return s.decode('utf_8')
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x83 in position 95: invalid start byte
```

chcpコマンドで65001を指定し文字コードをUTF-8に変更しよう。

```
$ chcp 65001
```


# out of memory エラーが出るよ

```
Traceback (most recent call last):
  File "cupy/cuda/memory.pyx", line 361, in cupy.cuda.memory.SingleDeviceMemoryPool.malloc (cupy\cuda\memory.cpp:7150)
  File "cupy/cuda/memory.pyx", line 263, in cupy.cuda.memory._malloc (cupy\cuda\memory.cpp:5853)
  File "cupy/cuda/memory.pyx", line 264, in cupy.cuda.memory._malloc (cupy\cuda\memory.cpp:5739)
  File "cupy/cuda/memory.pyx", line 35, in cupy.cuda.memory.Memory.__init__ (cupy\cuda\memory.cpp:1801)
  File "cupy/cuda/runtime.pyx", line 207, in cupy.cuda.runtime.malloc (cupy\cuda\runtime.cpp:3430)
  File "cupy/cuda/runtime.pyx", line 130, in cupy.cuda.runtime.check_status (cupy\cuda\runtime.cpp:2242)
cupy.cuda.runtime.CUDARuntimeError: cudaErrorMemoryAllocation: out of memory

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "cupy/cuda/memory.pyx", line 367, in cupy.cuda.memory.SingleDeviceMemoryPool.malloc (cupy\cuda\memory.cpp:7324)
  File "cupy/cuda/memory.pyx", line 263, in cupy.cuda.memory._malloc (cupy\cuda\memory.cpp:5853)
  File "cupy/cuda/memory.pyx", line 264, in cupy.cuda.memory._malloc (cupy\cuda\memory.cpp:5739)
  File "cupy/cuda/memory.pyx", line 35, in cupy.cuda.memory.Memory.__init__ (cupy\cuda\memory.cpp:1801)
  File "cupy/cuda/runtime.pyx", line 207, in cupy.cuda.runtime.malloc (cupy\cuda\runtime.cpp:3430)
  File "cupy/cuda/runtime.pyx", line 130, in cupy.cuda.runtime.check_status (cupy\cuda\runtime.cpp:2242)
cupy.cuda.runtime.CUDARuntimeError: cudaErrorMemoryAllocation: out of memory

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "seq2seq.py", line 305, in <module>
    main()
  File "seq2seq.py", line 301, in main
    trainer.run()
  File "T:\anaconda3\envs\chainer2\lib\site-packages\chainer\training\trainer.py", line 296, in run
    update()
  File "T:\anaconda3\envs\chainer2\lib\site-packages\chainer\training\updater.py", line 223, in update
    self.update_core()
  File "T:\anaconda3\envs\chainer2\lib\site-packages\chainer\training\updater.py", line 236, in update_core
    optimizer.update(loss_func, **in_arrays)
  File "T:\anaconda3\envs\chainer2\lib\site-packages\chainer\optimizer.py", line 533, in update
    loss.backward()
  File "T:\anaconda3\envs\chainer2\lib\site-packages\chainer\variable.py", line 718, in backward
    gxs = func.backward(in_data, out_grad)
  File "T:\anaconda3\envs\chainer2\lib\site-packages\chainer\functions\connection\n_step_rnn.py", line 474, in backward
    dw = cuda.cupy.zeros_like(self.w)
  File "T:\anaconda3\envs\chainer2\lib\site-packages\cupy\creation\basic.py", line 165, in zeros_like
    return zeros(a.shape, dtype=dtype)
  File "T:\anaconda3\envs\chainer2\lib\site-packages\cupy\creation\basic.py", line 142, in zeros
    a = cupy.ndarray(shape, dtype, order=order)
  File "cupy/core/core.pyx", line 92, in cupy.core.core.ndarray.__init__ (cupy\core\core.cpp:6509)
  File "cupy/cuda/memory.pyx", line 283, in cupy.cuda.memory.alloc (cupy\cuda\memory.cpp:5911)
  File "cupy/cuda/memory.pyx", line 434, in cupy.cuda.memory.MemoryPool.malloc (cupy\cuda\memory.cpp:9168)
  File "cupy/cuda/memory.pyx", line 450, in cupy.cuda.memory.MemoryPool.malloc (cupy\cuda\memory.cpp:9074)
  File "cupy/cuda/memory.pyx", line 347, in cupy.cuda.memory.SingleDeviceMemoryPool.malloc (cupy\cuda\memory.cpp:7762)
  File "cupy/cuda/memory.pyx", line 372, in cupy.cuda.memory.SingleDeviceMemoryPool.malloc (cupy\cuda\memory.cpp:7502)
  File "cupy/cuda/memory.pyx", line 263, in cupy.cuda.memory._malloc (cupy\cuda\memory.cpp:5853)
  File "cupy/cuda/memory.pyx", line 264, in cupy.cuda.memory._malloc (cupy\cuda\memory.cpp:5739)
  File "cupy/cuda/memory.pyx", line 35, in cupy.cuda.memory.Memory.__init__ (cupy\cuda\memory.cpp:1801)
  File "cupy/cuda/runtime.pyx", line 207, in cupy.cuda.runtime.malloc (cupy\cuda\runtime.cpp:3430)
  File "cupy/cuda/runtime.pyx", line 130, in cupy.cuda.runtime.check_status (cupy\cuda\runtime.cpp:2242)
cupy.cuda.runtime.CUDARuntimeError: cudaErrorMemoryAllocation: out of memory
```
- garbage collectionする
- バッチサイズを下げる
- データの品質を落とす
- 並列実行数を増やす
- それでも駄目なら[Titan X](http://www.nvidia.co.jp/graphics-cards/geforce/pascal/jp/titan-x-pascal)を買う


#感想
- Qiitaの情報を過信してはいけない（戒め）。
- [公式GitHub](https://github.com/chainer/chainer)のREADME.mdが一番信用できる。
- GPU周りで色々ハマるので、とりあえずSample動かしたいって人はCPU版使うのも手。