LoginSignup
2
1

More than 3 years have passed since last update.

あたらしい Pythonで動かして学ぶ! 機械学習の教科書 伊藤 真 著 numpy/keras注意!

Last updated at Posted at 2020-05-13

背景

「あたらしい Pythonで動かして学ぶ! 機械学習の教科書」 伊藤 真 著 !
第二版を使用
numpyを最新版1.18.2にupdateしたところ、エラーが発生した。1.18.xはNG
(Google Colaboratoryで動かそうとした時にも1.18.4で発生)。kerasでも有り

使用環境(version):

Mac Book 2016 3.3GHz Intel Corei7
Catalina 10.15.4

Anaconda 1.9.12
JupyterLab 1.2.6
JupyterNotebook 6.0.3
Colaboratory
Python 3.7.3, 3.6.9(colab)
Numpy 1.18.1,2,4 (colab)
Matplotlab 3.1.3, 3.2.1(colab)
Tensorflow 2.0.0, 2.2.0rs4(colab)
Keras 2.3.1

【1】 numpy np.shape(xx0, xn* xn, 1) ⇒ order="F" 変更必須

numpy version 1.18.2以降で発生

version 1.17.2 では問題なし。(1.17.xはOK)

変更が必要な箇所

変更前:np.reshape(xx1, xn*xn, 1)
変更後:np.reshape(xx1, xn*xn, order ="F") :従来より推奨されていた

参考文献
numpy.reshape version1.18.x : ValueError: Non-string object detected for the array ordering. Please pass in 'C', 'F', 'A', or 'K' instead エラー

1. リスト 4-5-(3) 142頁(あたり)
#リスト 4-5-(3)

# 等高線表示 --------------------------------
def show_contour_gauss(mu, sig):
    途中略
    xx0, xx1 = np.meshgrid(x0, x1)
    x = np.c_[np.reshape(xx0, xn * xn, order="F"), np.reshape(xx1, xn * xn, order="F")] #変更後

# 3D 表示 ----------------------------------
def show3d_gauss(ax, mu, sig):
    途中
    xx0, xx1 = np.meshgrid(x0, x1)
    x = np.c_[np.reshape(xx0, xn * xn, order="F"), np.reshape(xx1, xn * xn, order="F")] #変更後
2.リスト 7-1-(10) 274頁(あたり)
3.リスト 7-1-(14) 294頁(あたり)
4.リスト 7-2-(4) 304頁(あたり)
5.リスト 9-2-(6) 364頁(あたり)

【2】KeyError: 'acc' and KeyError: 'val_acc' Errors in Keras 2.3.x ⇒accuracyへ

参考文献

Fixing the KeyError: 'acc' and KeyError: 'val_acc' Errors in Keras 2.3.x

Kerasの2.3.xリリース以降は、metrixsで"accuracy"を使用した場合には、keyとしては"accuracy"を使うとの事です。val_accuracyも同様です。

According to the 2.3.0 Release Notes:
"Metrics and losses are now reported under the exact name specified by the user (e.g. if you pass metrics=['acc'], your metric will be reported under the string "acc", not "accuracy", and inversely metrics=['accuracy'] will be reported under the string "accuracy"."
You can read the official release notes here: https://github.com/keras-team/keras/releases/tag/2.3.0

What this means is that if you specify metrics=["accuracy"] in the model.compile(), then the history object will have the keys as 'accuracy' and 'val_accuracy'. While if you specify it as metrics=["acc"] then they will be reported with the keys 'acc' and 'val_acc'.

6.リスト 7-2-(4) 〜303頁

# リスト 7-2-(4)
"""精度表示"""
plt.subplot(1,3,2)
plt.plot(history.history['accuracy'],"k",label="training")
plt.plot(history.history["val_accuracy"],"cornflowerblue",label="test")
plt.legend()
2
1
0

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
2
1