「ゼロから作るDeep Learning2自然言語処理編」斎藤 康毅 著　第8章　作業報告

dockerで機械学習with anaconda(2)「ゼロから作るDeep Learning2自然言語処理編」斎藤 康毅 著
https://qiita.com/kaizen_nagoya/items/3b80dfc76933cea522c6

第７章まで先月実施した。
dockerで実行するために、ファイル出力に手を加えている。

```shell-session
# cd ch08
# python train.py
../common/trainer.py:7: UserWarning: 
This call to matplotlib.use() has no effect because the backend has already
been chosen; matplotlib.use() must be called *before* pylab, matplotlib.pyplot,
or matplotlib.backends is imported for the first time.

The backend was *originally* set to 'Qt5Agg' by the following code:
  File "train.py", line 5, in <module>
    import matplotlib.pyplot as plt
  File "/opt/conda/lib/python3.6/site-packages/matplotlib/pyplot.py", line 71, in <module>
    from matplotlib.backends import pylab_setup
  File "/opt/conda/lib/python3.6/site-packages/matplotlib/backends/__init__.py", line 16, in <module>
    line for line in traceback.format_stack()
  mpl.use('Agg')
Traceback (most recent call last):
  File "train.py", line 12, in <module>
    from ch07.peeky_seq2seq import PeekySeq2seq
  File "../ch07/peeky_seq2seq.py", line 5, in <module>
    from seq2seq import Seq2seq, Encoder
ModuleNotFoundError: No module named 'seq2seq'
```

別のフォルダに素の状態を入れ直すことにした。

```
# cd /
# mkdir deep
# cd deep
#  git clone https://github.com/oreilly-japan/deep-learning-from-scratch-2.git
# cd deep-learning-from-scratch-2
# cd ch08
# python train.py
Traceback (most recent call last):
  File "train.py", line 12, in <module>
    from ch07.peeky_seq2seq import PeekySeq2seq
  File "../ch07/peeky_seq2seq.py", line 5, in <module>
    from seq2seq import Seq2seq, Encoder
ModuleNotFoundError: No module named 'seq2seq'
```
素の状態でもエラーがでる。プログラムの該当箇所を拝見。


```python:ch07/peeky_seq2seq.py
# coding: utf-8
import sys
sys.path.append('..')
from common.time_layers import *
from seq2seq import Seq2seq, Encoder
```
該当行を下記のように編集。

```python
from ch07.seq2seq import Seq2seq, Encoder
```
とch07.を付け加えて保存。自分のフォルダ内にあるとしても、他から呼ばれた時に、相対位置を示してあればよい。


```shell-session
# python train.py
| epoch 1 |  iter 1 / 351 | time 0[s] | loss 4.08
| epoch 1 |  iter 21 / 351 | time 6[s] | loss 3.09
| epoch 1 |  iter 41 / 351 | time 13[s] | loss 1.90
| epoch 1 |  iter 61 / 351 | time 20[s] | loss 1.72
| epoch 1 |  iter 81 / 351 | time 28[s] | loss 1.46
| epoch 1 |  iter 101 / 351 | time 35[s] | loss 1.19
| epoch 1 |  iter 121 / 351 | time 42[s] | loss 1.14
| epoch 1 |  iter 141 / 351 | time 49[s] | loss 1.09
| epoch 1 |  iter 161 / 351 | time 55[s] | loss 1.06
| epoch 1 |  iter 181 / 351 | time 62[s] | loss 1.04
| epoch 1 |  iter 201 / 351 | time 69[s] | loss 1.03
| epoch 1 |  iter 221 / 351 | time 75[s] | loss 1.02
| epoch 1 |  iter 241 / 351 | time 82[s] | loss 1.02
| epoch 1 |  iter 261 / 351 | time 89[s] | loss 1.01
| epoch 1 |  iter 281 / 351 | time 95[s] | loss 1.00
| epoch 1 |  iter 301 / 351 | time 102[s] | loss 1.00
| epoch 1 |  iter 321 / 351 | time 109[s] | loss 1.00
| epoch 1 |  iter 341 / 351 | time 115[s] | loss 1.00
Q 10/15/94                     
T 1994-10-15
☒ 1978-08-11
---
Q thursday, november 13, 2008  
T 2008-11-13
☒ 1978-08-11
---
Q Mar 25, 2003                 
T 2003-03-25
☒ 1978-08-11
---
Q Tuesday, November 22, 2016   
T 2016-11-22
☒ 1978-08-11
---
Q Saturday, July 18, 1970      
T 1970-07-18
☒ 1978-08-11
---
Q october 6, 1992              
T 1992-10-06
☒ 1978-08-11
---
Q 8/23/08                      
T 2008-08-23
☒ 1978-08-11
---
Q 8/30/07                      
T 2007-08-30
☒ 1978-08-11
---
Q 10/28/13                     
T 2013-10-28
☒ 1978-08-11
---
Q sunday, november 6, 2016     
T 2016-11-06
☒ 1978-08-11
---
val acc 0.000%
| epoch 2 |  iter 1 / 351 | time 0[s] | loss 1.00
| epoch 2 |  iter 21 / 351 | time 7[s] | loss 1.00
| epoch 2 |  iter 41 / 351 | time 13[s] | loss 0.99
| epoch 2 |  iter 61 / 351 | time 20[s] | loss 0.99
| epoch 2 |  iter 81 / 351 | time 27[s] | loss 0.99
| epoch 2 |  iter 101 / 351 | time 33[s] | loss 0.99
| epoch 2 |  iter 121 / 351 | time 40[s] | loss 0.99
| epoch 2 |  iter 141 / 351 | time 47[s] | loss 0.98
| epoch 2 |  iter 161 / 351 | time 53[s] | loss 0.98
| epoch 2 |  iter 181 / 351 | time 60[s] | loss 0.97
| epoch 2 |  iter 201 / 351 | time 67[s] | loss 0.95
| epoch 2 |  iter 221 / 351 | time 73[s] | loss 0.94
| epoch 2 |  iter 241 / 351 | time 80[s] | loss 0.90
| epoch 2 |  iter 261 / 351 | time 87[s] | loss 0.83
| epoch 2 |  iter 281 / 351 | time 93[s] | loss 0.74
| epoch 2 |  iter 301 / 351 | time 100[s] | loss 0.66
| epoch 2 |  iter 321 / 351 | time 107[s] | loss 0.58
| epoch 2 |  iter 341 / 351 | time 113[s] | loss 0.46
Q 10/15/94                     
T 1994-10-15
☑ 1994-10-15
---
Q thursday, november 13, 2008  
T 2008-11-13
☒ 2006-11-13
---
Q Mar 25, 2003                 
T 2003-03-25
☑ 2003-03-25
---
Q Tuesday, November 22, 2016   
T 2016-11-22
☑ 2016-11-22
---
Q Saturday, July 18, 1970      
T 1970-07-18
☑ 1970-07-18
---
Q october 6, 1992              
T 1992-10-06
☑ 1992-10-06
---
Q 8/23/08                      
T 2008-08-23
☑ 2008-08-23
---
Q 8/30/07                      
T 2007-08-30
☒ 2007-08-09
---
Q 10/28/13                     
T 2013-10-28
☒ 1983-10-28
---
Q sunday, november 6, 2016     
T 2016-11-06
☒ 2016-11-08
---
val acc 51.680%
| epoch 3 |  iter 1 / 351 | time 0[s] | loss 0.35
| epoch 3 |  iter 21 / 351 | time 7[s] | loss 0.30
| epoch 3 |  iter 41 / 351 | time 13[s] | loss 0.21
| epoch 3 |  iter 61 / 351 | time 20[s] | loss 0.14
| epoch 3 |  iter 81 / 351 | time 27[s] | loss 0.09
| epoch 3 |  iter 101 / 351 | time 33[s] | loss 0.07
| epoch 3 |  iter 121 / 351 | time 40[s] | loss 0.05
| epoch 3 |  iter 141 / 351 | time 47[s] | loss 0.04
| epoch 3 |  iter 161 / 351 | time 53[s] | loss 0.03
| epoch 3 |  iter 181 / 351 | time 60[s] | loss 0.03
| epoch 3 |  iter 201 / 351 | time 67[s] | loss 0.02
| epoch 3 |  iter 221 / 351 | time 75[s] | loss 0.02
| epoch 3 |  iter 241 / 351 | time 84[s] | loss 0.02
| epoch 3 |  iter 261 / 351 | time 91[s] | loss 0.01
| epoch 3 |  iter 281 / 351 | time 98[s] | loss 0.01
| epoch 3 |  iter 301 / 351 | time 106[s] | loss 0.01
| epoch 3 |  iter 321 / 351 | time 113[s] | loss 0.01
| epoch 3 |  iter 341 / 351 | time 120[s] | loss 0.01
Q 10/15/94                     
T 1994-10-15
☑ 1994-10-15
---
Q thursday, november 13, 2008  
T 2008-11-13
☑ 2008-11-13
---
Q Mar 25, 2003                 
T 2003-03-25
☑ 2003-03-25
---
Q Tuesday, November 22, 2016   
T 2016-11-22
☑ 2016-11-22
---
Q Saturday, July 18, 1970      
T 1970-07-18
☑ 1970-07-18
---
Q october 6, 1992              
T 1992-10-06
☑ 1992-10-06
---
Q 8/23/08                      
T 2008-08-23
☑ 2008-08-23
---
Q 8/30/07                      
T 2007-08-30
☑ 2007-08-30
---
Q 10/28/13                     
T 2013-10-28
☑ 2013-10-28
---
Q sunday, november 6, 2016     
T 2016-11-06
☑ 2016-11-06
---
val acc 99.900%
| epoch 4 |  iter 1 / 351 | time 0[s] | loss 0.01
| epoch 4 |  iter 21 / 351 | time 7[s] | loss 0.01
| epoch 4 |  iter 41 / 351 | time 14[s] | loss 0.01
| epoch 4 |  iter 61 / 351 | time 21[s] | loss 0.01
| epoch 4 |  iter 81 / 351 | time 28[s] | loss 0.01
| epoch 4 |  iter 101 / 351 | time 36[s] | loss 0.01
| epoch 4 |  iter 121 / 351 | time 43[s] | loss 0.00
| epoch 4 |  iter 141 / 351 | time 51[s] | loss 0.01
| epoch 4 |  iter 161 / 351 | time 59[s] | loss 0.00
| epoch 4 |  iter 181 / 351 | time 66[s] | loss 0.00
| epoch 4 |  iter 201 / 351 | time 73[s] | loss 0.00
| epoch 4 |  iter 221 / 351 | time 80[s] | loss 0.00
| epoch 4 |  iter 241 / 351 | time 87[s] | loss 0.00
| epoch 4 |  iter 261 / 351 | time 94[s] | loss 0.00
| epoch 4 |  iter 281 / 351 | time 100[s] | loss 0.00
| epoch 4 |  iter 301 / 351 | time 107[s] | loss 0.00
| epoch 4 |  iter 321 / 351 | time 114[s] | loss 0.00
| epoch 4 |  iter 341 / 351 | time 121[s] | loss 0.00
Q 10/15/94                     
T 1994-10-15
☑ 1994-10-15
---
Q thursday, november 13, 2008  
T 2008-11-13
☑ 2008-11-13
---
Q Mar 25, 2003                 
T 2003-03-25
☑ 2003-03-25
---
Q Tuesday, November 22, 2016   
T 2016-11-22
☑ 2016-11-22
---
Q Saturday, July 18, 1970      
T 1970-07-18
☑ 1970-07-18
---
Q october 6, 1992              
T 1992-10-06
☑ 1992-10-06
---
Q 8/23/08                      
T 2008-08-23
☑ 2008-08-23
---
Q 8/30/07                      
T 2007-08-30
☑ 2007-08-30
---
Q 10/28/13                     
T 2013-10-28
☑ 2013-10-28
---
Q sunday, november 6, 2016     
T 2016-11-06
☑ 2016-11-06
---
val acc 99.900%
| epoch 5 |  iter 1 / 351 | time 0[s] | loss 0.00
| epoch 5 |  iter 21 / 351 | time 7[s] | loss 0.00
| epoch 5 |  iter 41 / 351 | time 13[s] | loss 0.00
| epoch 5 |  iter 61 / 351 | time 20[s] | loss 0.00
| epoch 5 |  iter 81 / 351 | time 27[s] | loss 0.00
| epoch 5 |  iter 101 / 351 | time 33[s] | loss 0.00
| epoch 5 |  iter 121 / 351 | time 40[s] | loss 0.00
| epoch 5 |  iter 141 / 351 | time 47[s] | loss 0.00
| epoch 5 |  iter 161 / 351 | time 54[s] | loss 0.00
| epoch 5 |  iter 181 / 351 | time 60[s] | loss 0.00
| epoch 5 |  iter 201 / 351 | time 67[s] | loss 0.00
| epoch 5 |  iter 221 / 351 | time 74[s] | loss 0.00
| epoch 5 |  iter 241 / 351 | time 80[s] | loss 0.00
| epoch 5 |  iter 261 / 351 | time 87[s] | loss 0.00
| epoch 5 |  iter 281 / 351 | time 94[s] | loss 0.00
| epoch 5 |  iter 301 / 351 | time 101[s] | loss 0.00
| epoch 5 |  iter 321 / 351 | time 107[s] | loss 0.00
| epoch 5 |  iter 341 / 351 | time 114[s] | loss 0.00
Q 10/15/94                     
T 1994-10-15
☑ 1994-10-15
---
Q thursday, november 13, 2008  
T 2008-11-13
☑ 2008-11-13
---
Q Mar 25, 2003                 
T 2003-03-25
☑ 2003-03-25
---
Q Tuesday, November 22, 2016   
T 2016-11-22
☑ 2016-11-22
---
Q Saturday, July 18, 1970      
T 1970-07-18
☑ 1970-07-18
---
Q october 6, 1992              
T 1992-10-06
☑ 1992-10-06
---
Q 8/23/08                      
T 2008-08-23
☑ 2008-08-23
---
Q 8/30/07                      
T 2007-08-30
☑ 2007-08-30
---
Q 10/28/13                     
T 2013-10-28
☑ 2013-10-28
---
Q sunday, november 6, 2016     
T 2016-11-06
☑ 2016-11-06
---
val acc 99.940%
| epoch 6 |  iter 1 / 351 | time 0[s] | loss 0.00
| epoch 6 |  iter 21 / 351 | time 7[s] | loss 0.00
| epoch 6 |  iter 41 / 351 | time 13[s] | loss 0.00
| epoch 6 |  iter 61 / 351 | time 20[s] | loss 0.00
| epoch 6 |  iter 81 / 351 | time 27[s] | loss 0.00
| epoch 6 |  iter 101 / 351 | time 33[s] | loss 0.00
| epoch 6 |  iter 121 / 351 | time 40[s] | loss 0.00
| epoch 6 |  iter 141 / 351 | time 47[s] | loss 0.00
| epoch 6 |  iter 161 / 351 | time 54[s] | loss 0.00
| epoch 6 |  iter 181 / 351 | time 61[s] | loss 0.00
| epoch 6 |  iter 201 / 351 | time 67[s] | loss 0.00
| epoch 6 |  iter 221 / 351 | time 74[s] | loss 0.00
| epoch 6 |  iter 241 / 351 | time 81[s] | loss 0.00
| epoch 6 |  iter 261 / 351 | time 87[s] | loss 0.00
| epoch 6 |  iter 281 / 351 | time 94[s] | loss 0.00
| epoch 6 |  iter 301 / 351 | time 101[s] | loss 0.00
| epoch 6 |  iter 321 / 351 | time 108[s] | loss 0.00
| epoch 6 |  iter 341 / 351 | time 114[s] | loss 0.03
Q 10/15/94                     
T 1994-10-15
☑ 1994-10-15
---
Q thursday, november 13, 2008  
T 2008-11-13
☑ 2008-11-13
---
Q Mar 25, 2003                 
T 2003-03-25
☑ 2003-03-25
---
Q Tuesday, November 22, 2016   
T 2016-11-22
☑ 2016-11-22
---
Q Saturday, July 18, 1970      
T 1970-07-18
☑ 1970-07-18
---
Q october 6, 1992              
T 1992-10-06
☑ 1992-10-06
---
Q 8/23/08                      
T 2008-08-23
☑ 2008-08-23
---
Q 8/30/07                      
T 2007-08-30
☑ 2007-08-30
---
Q 10/28/13                     
T 2013-10-28
☑ 2013-10-28
---
Q sunday, november 6, 2016     
T 2016-11-06
☑ 2016-11-06
---
val acc 91.400%
| epoch 7 |  iter 1 / 351 | time 0[s] | loss 0.03
| epoch 7 |  iter 21 / 351 | time 7[s] | loss 0.02
| epoch 7 |  iter 41 / 351 | time 13[s] | loss 0.01
| epoch 7 |  iter 61 / 351 | time 20[s] | loss 0.01
| epoch 7 |  iter 81 / 351 | time 27[s] | loss 0.01
| epoch 7 |  iter 101 / 351 | time 33[s] | loss 0.00
| epoch 7 |  iter 121 / 351 | time 40[s] | loss 0.00
| epoch 7 |  iter 141 / 351 | time 47[s] | loss 0.00
| epoch 7 |  iter 161 / 351 | time 54[s] | loss 0.00
```

動き始めた。
epoch 1: val acc 0.0%
epoch 2: val acc 51.680%
epoch 3: val acc 99.900%
epoch 3: val acc 99.900%
epoch 5: val acc 99.940%
epoch 6: val acc 91.400%
epoch 7: val acc 99.960%
epoch 8: val acc 100.000%

## 課題
6回目で一旦精度が落ちている。
8回目で100%になったのに動き続けている。
(9回目, 10回目も100%で終了。上記の値は、再度やっても同じだった。)

# 文書履歴(document history)
ver. 0.01 初稿 20190320 午後3時
ver. 0.02 動作 20190320 午後4時
ver. 0.03 再度実行 20190320 午後5時

 p.s.
dockerで動作させているため、次のエラーが出た。

```
Traceback (most recent call last):
  File "train.py", line 58, in <module>
    plt.plot(x, acc_list, marker='o')
  File "/opt/conda/lib/python3.6/site-packages/matplotlib/pyplot.py", line 3347, in plot
    ax = gca()
  File "/opt/conda/lib/python3.6/site-packages/matplotlib/pyplot.py", line 984, in gca
    return gcf().gca(**kwargs)
  File "/opt/conda/lib/python3.6/site-packages/matplotlib/pyplot.py", line 601, in gcf
    return figure()
  File "/opt/conda/lib/python3.6/site-packages/matplotlib/pyplot.py", line 548, in figure
    **kwargs)
  File "/opt/conda/lib/python3.6/site-packages/matplotlib/backend_bases.py", line 161, in new_figure_manager
    return cls.new_figure_manager_given_figure(num, fig)
  File "/opt/conda/lib/python3.6/site-packages/matplotlib/backend_bases.py", line 167, in new_figure_manager_given_figure
    canvas = cls.FigureCanvas(figure)
  File "/opt/conda/lib/python3.6/site-packages/matplotlib/backends/backend_qt5agg.py", line 24, in __init__
    super(FigureCanvasQTAgg, self).__init__(figure=figure)
  File "/opt/conda/lib/python3.6/site-packages/matplotlib/backends/backend_qt5.py", line 234, in __init__
    _create_qApp()
  File "/opt/conda/lib/python3.6/site-packages/matplotlib/backends/backend_qt5.py", line 125, in _create_qApp
    raise RuntimeError('Invalid DISPLAY variable')
RuntimeError: Invalid DISPLAY variable
```

手を加えるとエラーは出たが動き始めた。


##編集したpythonファイル
###1) PNGファイル出力のおまじない:２行追記

```
import matplotlib as mpl
mpl.use('Agg')
```
###2) ファイル出力操作：２行追記、１行注釈化

```
fig = plt.figure()
#plt.show()
fig.savefig('train.png')
```
結果はtrain.pngファイルとして出力

```
train.py:14: UserWarning: 
This call to matplotlib.use() has no effect because the backend has already
been chosen; matplotlib.use() must be called *before* pylab, matplotlib.pyplot,
or matplotlib.backends is imported for the first time.

The backend was *originally* set to 'Qt5Agg' by the following code:
  File "train.py", line 5, in <module>
    import matplotlib.pyplot as plt
  File "/opt/conda/lib/python3.6/site-packages/matplotlib/pyplot.py", line 71, in <module>
    from matplotlib.backends import pylab_setup
  File "/opt/conda/lib/python3.6/site-packages/matplotlib/backends/__init__.py", line 16, in <module>
    line for line in traceback.format_stack()


  mpl.use('Agg')
```
最後のエラーは同じものがでた。

直し方を再検討中。ごめんなさい。

### 最後までおよみいただきありがとうございました。
いいね　💚、フォローをお願いします。
####  Thank you very much for reading to the last sentence.
Please press the like icon 💚　and follow me for your happy life.