<目次>
(1-1) 事象
(1-2) 原因
(1-3) 対策(応急処置)
(1-4) 対策(恒久)
(1) TensorFlowで「AttributeError: module ‘tensorflow’ has no attribute ‘xxxxxx’」が出た時の原因と対策について
(1-1) 事象
Traceback (most recent call last): File "c:\dev\Python\00_LocalPythonApp\Deeplearning\python_no_attribute_error.py", line 12, in main() File "c:\dev\Python\00_LocalPythonApp\Deeplearning\python_no_attribute_error.py", line 8, in main x = tf.placeholder(tf.float32, shape=[None,2]) AttributeError: module 'tensorflow' has no attribute 'placeholder'
import numpy as np import os os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' import tensorflow as tf def main(): x = tf.placeholder(tf.float32, shape=[None,2]) t = tf.placeholder(tf.float32, shape=[None,1]) if __name__ == "__main__": main()
(1-2) 原因
import tensorflow as tf print(tf.__version__)
(1-3) 対策(応急処置)
import tensorflow.compat.v1 as tf1 tf1.disable_v2_behavior()
import numpy as np import os os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' import tensorflow.compat.v1 as tf1 tf1.disable_v2_behavior() def main(): x = tf1.placeholder(tf1.float32, shape=[None,2]) t = tf1.placeholder(tf1.float32, shape=[None,1]) if __name__ == "__main__": main()
WARNING:tensorflow:From C:\Users\XXXXX\.pyenv\pyenv-win\versions\3.9.6\lib\site-packages\tensorflow\python\compat\v2_compat.py:107: disable_resource_variables (from tensorflow.python.ops.variable_scope) is deprecated and will be removed in a future version. Instructions for updating: non-resource variables are not supported in the long term
(1-4) 対策(恒久)
●①「tf_upgrade_v2.exe」の確認
(格納場所の例)
C:\Users\XXXXXXXXXXXXX\.pyenv\pyenv-win\versions\3.9.6\Scripts\tf_upgrade_v2.exe
●②変換の実行
& "C:\Users\XXXX\.pyenv\pyenv-win\versions\3.9.6\Scripts\tf_upgrade_v2.exe" --infile ./python_no_attribute_error.py --outfile ./python_no_attribute_error_v2.py
●③変換結果の確認
x = tf.placeholder(tf.float32, shape=[None,2])
x = tf.compat.v1.placeholder(tf.float32, shape=[None,2])