Skip to content

[已解決] Exception ignored in: bound method BaseSession.__del__ of tensorflow.python.client.session.Session object at 0x7ff1243e0358

問題描述

今天在執行以 Tensorflow 作為後端的 Keras 程式時,意外發生了以下報錯訊息:

Exception ignored in: <bound method BaseSession.__del__ of <tensorflow.python.client.session.Session object at 0x7ff1243e0358>>

上網一查,發覺這與 Python 自動回收資源的機制有關,似乎 Tensorflow 也存在著回收資源的機制,而某一方率先回收了資源,導致另一方回收資源時找到空值。

目前為止,在網路上找到的解決辦法有四項:

  • 提升 Tensorflow 版本(據說在之後的版本中有修正這個問題)
  • 刪除 __pycache__ 資料夾(Python 程式互相參照時自動產生的資料夾)
  • 使用 Keras 模組清空 session
  • 使用 gc 模組回收資源(經過測試可於 Tensorflow==1.12.0 成功排除問題)

使用 Keras 模組清空 session

在程式的開頭匯入以下函式:

import keras.backend as K



在程式的結尾則使用以下函式:

K.clear_session()



也就是說完整程式應為:

import keras.backend as K

# ----------
# Your code
# ----------

K.clear_session()

使用 gc 模組回收資源

依此類推,使用 gc 模組回收資源的方法如下:

import gc 

# ----------
# Your code
# ----------

gc.collect()



References


Read More

Leave a Reply