WordPress.com で次のようなサイトをデザイン
始めてみよう

jupyter notebookでkerasが動作しなくなった時の対処方法

症状について TensorFlowのチュートリアルとして公式サイトで紹介されている、「mnist」という手書きの数字を認識させるコードを見様見真似でjupyter notebookで動作させていました…

症状について

TensorFlowのチュートリアルとして公式サイトで紹介されている、「mnist」という手書きの数字を認識させるコードを見様見真似でjupyter notebookで動作させていました。
コードの全文は以下。

import tensorflow as tf
mnist = tf.keras.datasets.mnist

(x_train, y_train),(x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0

model = tf.keras.models.Sequential([
  tf.keras.layers.Flatten(input_shape=(28, 28)),
  tf.keras.layers.Dense(128, activation='relu'),
  tf.keras.layers.Dropout(0.2),
  tf.keras.layers.Dense(10, activation='softmax')
])

model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

model.fit(x_train, y_train, epochs=5)
model.evaluate(x_test, y_test)

これを上から順に動かしていたところ、以下のモデルを用いて学習させるところでつまずきました。

model.fit(x_train, y_train, epochs=5)

実際にjupyter notebook上では、以下のエラーが表示されました。

Kernel Restarting
The kernel appears to have died. It will restart automatically.

これを読むと、kernelが何らかの原因により停止してしまったということは理解できますが、原因がなんなのか全く分かりません。

そこで、ターミナルを確認したところ、以下の通りエラーが出ていました。

OMP: Error #15: Initializing libiomp5.dylib, but found libiomp5.dylib already initialized.
OMP: Hint This means that multiple copies of the OpenMP runtime have been linked into the program. 
That is dangerous, since it can degrade performance or cause incorrect results.
The best thing to do is to ensure that only a single OpenMP runtime is linked into the process, e.g. by avoiding static linking of the OpenMP runtime in any library. 
As an unsafe, unsupported, undocumented workaround you can set the environment variable KMP_DUPLICATE_LIB_OK=TRUE to allow the program to continue to execute, but that may cause crashes or silently produce incorrect results. 
For more information, please see http://www.intel.com/software/products/support/.

解決策

OMPについての知識が全くないのですが、ターミナルのエラーに記載があるように、応急措置として、環境変数を設定することによって対処可能とあります。

実際に、jupyter notebook上で、以下のコードを読み込ませた後に、先ほどエラーが出たコードを動作させたところ、問題なく動作しました。

import os

os.environ['KMP_DUPLICATE_LIB_OK']='True'

ひとまずこれで解決しましたが、ターミナル上のエラーによると、この措置だとプログラムがクラッシュしたり、誤った結果を返す場合があるとのことです。

抜本的な解決策については、今後探していくこととしたいと思います。ひとまずこれにて。
また、正しい解決方法をご存知の方がおりましたらご連絡いただけると幸いです。

(備忘録:参考にしたサイト)
https://github.com/dmlc/xgboost/issues/1715

コメントを残す

以下に詳細を記入するか、アイコンをクリックしてログインしてください。

WordPress.com ロゴ

WordPress.com アカウントを使ってコメントしています。 ログアウト /  変更 )

Facebook の写真

Facebook アカウントを使ってコメントしています。 ログアウト /  変更 )

%s と連携中

%d人のブロガーが「いいね」をつけました。