0%

TensorFlow_V2兼容V1

使用pip install tf-nightly-gpu无脑将TensorFlow升级到2.7后,之前的项目中有用1.14版本的TensorFlow出现兼容的问题,可通过如下几行进行适配。

1
2
3
4
5
6
import tensorflow
if tensorflow.__version__.split('.')[0] == '2':
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
else:
import tensorflow as tf