在TensorFlow2.XX上运行1.XX版本报错:AttributeError: module 'tensorflow' has no attribute 'XXX'等。
在网上找到相关解决方法。如:
(https://blog.csdn.net/flowingfog/article/details/93597697)解决方式:
使用
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
替换
import tensorflow as tf
或者使用:
import tensorflow as tf
if ('2' == tf.__version__[0]):
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
assert tf.__version__.startswith('2.')
else:
assert tf.__version__.startswith('1.')
修改后,运行正常,说明转换成功。但是运行到RNN模型的时候,又报错诸如:AttributeError:module 'tensorflow' has no attribute 'contrib'、AttributeError: module 'tensorflow._api.v1.compat' has no attribute 'v1'等问题。
定位代码:
cell = tf.contrib.rnn.BasicLSTMCell(n_hidden_units)
分析发现是RNN模型调用方式的问题。
修改为:
cell = tf.nn.rnn_cell.BasicLSTMCell(n_hidden_units)
运行成功!