月別アーカイブ: 2017年6月

deep-learning-from-scratch

git clone https://github.com/oreilly-japan/deep-learning-from-scratch

でソースコードを入手します。

README.mdをみると

* Python 3.x
* NumPy
* Matplotlib

が必要なのでmac portsでinstallします。
py35-matplotlibをinstallして動作を確認します。
~/.matplotlib/matplotlibrcで
backend : MacOSX
を指定します。

[~/src/Python]python3.5 simple_plot.py --verbose-helpful
machdep: class is not implemented
$HOME=/Users/isomura
matplotlib data path /opt/local/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/matplotlib/mpl-data

*****************************************************************
You have the following UNSUPPORTED LaTeX preamble customizations:

Please do not ask for support with these customizations active.
*****************************************************************

loaded rc file /Users/isomura/.matplotlib/matplotlibrc
matplotlib version 2.0.0
verbose.level helpful
interactive is False
platform is darwin
CACHEDIR=/Users/isomura/.matplotlib
font search path ['/opt/local/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/matplotlib/mpl-data/fonts/ttf', '/opt/local/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/matplotlib/mpl-data/fonts/afm', '/opt/local/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/matplotlib/mpl-data/fonts/pdfcorefonts']
terminate called after throwing an instance of 'char*'
Abort

いろいろやりましたが、ダメでした。python3.6でも同じでした。
python2.7だとmatplotlibは動きました。

[~/src/Python]python2.7 simple_plot.py --verbose-helpful
machdep: class is not implemented
$HOME=/Users/isomura
matplotlib data path /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/mpl-data

*****************************************************************
You have the following UNSUPPORTED LaTeX preamble customizations:

Please do not ask for support with these customizations active.
*****************************************************************

loaded rc file /Users/isomura/.matplotlib/matplotlibrc
matplotlib version 2.0.0
verbose.level helpful
interactive is False
platform is darwin
CACHEDIR=/Users/isomura/.matplotlib
Using fontManager instance from /Users/isomura/.matplotlib/fontList.cache
backend MacOSX version unknown
findfont: Matching :family=sans-serif:style=normal:variant=normal:weight=400:stretch=normal:size=10.0 to DejaVu Sans (u'/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/mpl-data/fonts/ttf/DejaVuSans.ttf') with score of 0.000000

170604

追記
machdep: class is not implemented
が表示されてしまいます。原因を調べてみます。

[lib/python2.7/site-packages]grep -r machdep *
バイナリファイル numpy/core/multiarray.so に一致しました
バイナリファイル objc/_objc.so に一致しました
[lib/python2.7/site-packages]strings numpy/core/multiarray.so|grep mac
sysctl -n machdep.cpu.features | grep -q AVX
[lib/python2.7/site-packages]strings objc/_objc.so|grep mac
macroman
[lib/python2.7/site-packages]sysctl -n machdep.cpu.features
machdep: class is not implemented

multiarray.soで使っているsysctlにmachdepが無いのが原因のようです。
AVX拡張命令セットが有るかを判定しているようです。
sysctl -a hw.machine
hw.machine: Power Macintosh
なので無視しても問題ないと思います。
とりあえず出ないようにしてみます。
sudo mv /usr/sbin/sysctl /usr/sbin/sysctl.orig
にして、シェルスクリプトにします。

cat /usr/sbin/sysctl
#! /bin/sh
if [ "$2" = "machdep.cpu.features" ] 
then
	exit
fi
/usr/sbin/sysctl.orig $*

これで、machdep: class is not implementedはでなくなりました。

準備ができたので動かしてみます。

[Python/deep-learning-from-scratch/ch01]python hungry.py 
I'm hungry!
[Python/deep-learning-from-scratch/ch01]python man.py 
Initilized!
Hello David!
Good-bye David!
[Python/deep-learning-from-scratch/ch01]python simple_graph.py 
[Python/deep-learning-from-scratch/ch01]python sin_graph.py 
[Python/deep-learning-from-scratch/ch01]python sin_cos_graph.py 
[Python/deep-learning-from-scratch/ch01]python img_show.py 
[Python/deep-learning-from-scratch/ch02]python and_gate.py 
(0, 0) -> 0
(1, 0) -> 0
(0, 1) -> 0
(1, 1) -> 1
[Python/deep-learning-from-scratch/ch02]python nand_gate.py 
(0, 0) -> 1
(1, 0) -> 1
(0, 1) -> 1
(1, 1) -> 0
[Python/deep-learning-from-scratch/ch02]python or_gate.py 
(0, 0) -> 0
(1, 0) -> 1
(0, 1) -> 1
(1, 1) -> 1
[Python/deep-learning-from-scratch/ch02]python xor_gate.py 
(0, 0) -> 0
(1, 0) -> 1
(0, 1) -> 1
(1, 1) -> 0
[Python/deep-learning-from-scratch/ch03]python step_function.py 
[Python/deep-learning-from-scratch/ch03]python sigmoid.py 
[Python/deep-learning-from-scratch/ch03]python sig_step_compare.py 
[Python/deep-learning-from-scratch/ch03]python relu.py 
[Python/deep-learning-from-scratch/ch03]python3.6 neuralnet_mnist.py
Accuracy:0.9352
[Python/deep-learning-from-scratch/ch03]python3.6 neuralnet_mnist_batch.py 
Accuracy:0.9352

[Python/deep-learning-from-scratch/ch03]python3.6 mnist_show.py
Traceback (most recent call last):
  File "mnist_show.py", line 6, in 
    from PIL import Image
ModuleNotFoundError: No module named 'PIL'

mac ports で’PIL’を探してinstallします。
py36-Pillow @4.1.1 (python, devel)
Python Imaging Library (fork)
sudo port install py36-Pillow

[Python/deep-learning-from-scratch/ch03]python3.6 mnist_show.py 
5
(784,)
(28, 28)
[Python/deep-learning-from-scratch/ch04]python gradient_1d.py 
0.199999999999

[Python/deep-learning-from-scratch/ch04]python gradient_2d.py
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/axes/_axes.py:545: UserWarning: No labelled objects found. Use label='...' kwarg on individual plots.
  warnings.warn("No labelled objects found. "
D:\cygwin\home\root\src\deep-learning-from-scratch\ch04>python gradient_2d.py
C:\Users\isomura\Anaconda3\lib\site-packages\matplotlib\axes\_axes.py:545: UserWarning: No labelled objects found. Use label='...' kwarg on individual plots.
  warnings.warn("No labelled objects found. "

windowsでも同じ結果になるようです。

[Python/deep-learning-from-scratch/ch04]python gradient_method.py 
[Python/deep-learning-from-scratch/ch04]python gradient_simplenet.py 
[[ 0.44710081  0.02188944 -0.46899026]
 [ 0.67065122  0.03283417 -0.70348539]]
[Python/deep-learning-from-scratch/ch04]python two_layer_net.py 
[Python/deep-learning-from-scratch/ch04]python train_neuralnet.py 
Traceback (most recent call last):
  File "train_neuralnet.py", line 6, in 
    from dataset.mnist import load_mnist
  File "../dataset/mnist.py", line 5, in 
    raise ImportError('You should use Python 3.x')
ImportError: You should use Python 3.x
[Python/deep-learning-from-scratch/ch04]python3.6 train_neuralnet.py
terminate called after throwing an instance of 'char*'

動かないのが出てしまいました。matplotlibを使ってPython3.xを要求するのがダメです。
当然windowsなら動きます。

D:\cygwin\home\root\src\deep-learning-from-scratch\ch04>python train_neuralnet.py
train acc, test acc | 0.112366666667, 0.1135
train acc, test acc | 0.798, 0.8032
train acc, test acc | 0.8754, 0.8783
train acc, test acc | 0.898433333333, 0.9023
train acc, test acc | 0.908833333333, 0.9112
train acc, test acc | 0.915266666667, 0.9173
train acc, test acc | 0.920666666667, 0.9223
train acc, test acc | 0.92485, 0.9247
train acc, test acc | 0.928333333333, 0.9299
train acc, test acc | 0.930466666667, 0.9322
train acc, test acc | 0.935066666667, 0.9361
train acc, test acc | 0.93715, 0.9383
train acc, test acc | 0.9396, 0.9394
train acc, test acc | 0.94245, 0.9419
train acc, test acc | 0.9443, 0.9442
train acc, test acc | 0.946433333333, 0.9452
train acc, test acc | 0.9483, 0.9468

[Python/deep-learning-from-scratch/ch05]python buy_apple.py 
('price:', 220)
('dApple:', 2.2)
('dApple_num:', 110)
('dTax:', 200)
[Python/deep-learning-from-scratch/ch05]python buy_apple_orange.py 
('price:', 715)
('dApple:', 2.2)
('dApple_num:', 110)
('dOrange:', 3.3000000000000003)
('dOrange_num:', 165)
('dTax:', 650)
[Python/deep-learning-from-scratch/ch05]python layer_naive.py
[Python/deep-learning-from-scratch/ch05]python two_layer_net.py 
[Python/deep-learning-from-scratch/ch05]python3.6 train_neuralnet.py
0.081 0.0792
0.903683333333 0.9073
0.923266666667 0.9267
0.935433333333 0.9352
0.944883333333 0.9433
0.951283333333 0.9492
0.956066666667 0.9547
0.959766666667 0.9556
0.964466666667 0.9591
0.9666 0.9618
0.968883333333 0.9644
0.971683333333 0.9663
0.971233333333 0.9644
0.973433333333 0.9665
0.975616666667 0.9682
0.97765 0.9699
0.978683333333 0.9697
[Python/deep-learning-from-scratch/ch05]python3.6 gradient_check.py
W1:2.68363647145e-13
b1:9.32718058604e-13
W2:9.38517990943e-13
b2:1.21014305521e-10

この後は、動かないのが多くなりました。

[Python/deep-learning-from-scratch/ch06]python3.6 batch_norm_gradient_check.py
W1:0.0
b1:0.0
gamma1:0.0
beta1:0.0
W2:0.0
b2:0.0
gamma2:0.0
beta2:0.0507355901743
W3:0.0
b3:1.20126128489e-10
[Python/deep-learning-from-scratch/ch06]python optimizer_compare_naive.py
[Python/deep-learning-from-scratch/ch06]python weight_init_activation_histogram.py
[Python/deep-learning-from-scratch/ch07]python3.6 gradient_check.py 
W1 7.96847710054e-13
b1 8.53629631716e-13
W2 4.16094078313e-13
b2 0.000294700165544
W3 5.09735778418e-13
b3 1.20570220474e-10
[Python/deep-learning-from-scratch/ch07]python3.6 simple_convnet.py
[Python/deep-learning-from-scratch/ch08]python3.6 awesome_net.py
[Python/deep-learning-from-scratch/ch08]python3.6 deep_convnet.py

python3.6がうまく動いていないようです。

[~/src/macports]python3.6
Python 3.6.1 (default, Jun 25 2017, 10:31:32) 
[GCC 5.4.0] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 7/5
Python(94954) malloc: *** error for object 0x866e8: Non-aligned pointer being freed
*** set a breakpoint in malloc_error_break to debug
1.4
>>> Python(94954) malloc: *** error for object 0x866e0: Non-aligned pointer being freed (2)
*** set a breakpoint in malloc_error_break to debug

検索してみると、readlineに関係しているようです。

[~/src/macports]sudo port deactivate py36-readline @6.2.4.1_1
Password:
--->  Deactivating py36-readline @6.2.4.1_1
--->  Cleaning py36-readline
[~/src/macports]python3.6
Python 3.6.1 (default, Jun 25 2017, 10:31:32) 
[GCC 5.4.0] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 7/5
1.4
>>> 

readline extensionは、py36-readlineの他にpy36-gnureadlineが有ったので試してみます。

[Python/deep-learning-from-scratch/ch01]port installed py36-gnureadline py36-readline python36
The following ports are currently installed:
  py36-gnureadline @6.3.3_1 (active)
  py36-readline @6.2.4.1_1
  python36 @3.6.1_1+readline (active)

py36-gnureadlineだと大丈夫なようです。
でもまだmatplotlibを使うとエラーになってしまいます。

追記
matplotlib動くようになりました。

[~/src/Python]python3.6 simple_plot.py --verbose-helpful
$HOME=/Users/isomura
matplotlib data path /opt/local/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/mpl-data
loaded rc file /Users/isomura/.matplotlib/matplotlibrc
matplotlib version 2.1.1
verbose.level helpful
interactive is False
platform is darwin
CACHEDIR=/Users/isomura/.matplotlib
Using fontManager instance from /Users/isomura/.matplotlib/fontList.json
backend MacOSX version unknown
findfont: Matching :family=sans-serif:style=normal:variant=normal:weight=normal:stretch=normal:size=10.0 to DejaVu Sans ('/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/mpl-data/fonts/ttf/DejaVuSans.ttf') with score of 0.050000

クローズをクリックすると

terminate called after throwing an instance of 'std::runtime_error'
  what():  Couldn't close file
Abort

が表示されます。python2.7では問題ありません。
エラーメッセージを検索するとロケール関係の問題のようです。
setenv LANG ja_JP.UTF-8

ソースの1行目には
# coding: UTF-8
になっています。
setenv LANG C
で実行しても同じ結果になります。
locale -a
すると
ja_JP.UTF-8
は有ります。