TensorFlow 2.0版本兼容问题建议解答

由于TensorFlow 2.0中已经移除了contrib库,因此出现了TensorFlow 2.0和TensorFlow 1.0不兼容的问题。下面将我做项目时碰到的问题进行简单的展示,供大家参考,不喜勿碰。

1.tensorflow2.0没有contrib包:tensorflow has no attribute “contrib”
TensorFlow 2.0中提供了tensorflow.compat.v1代码包来兼容原有1.x的代码,可以做到几乎不加修改的运行。因此我们只需将源文件中的代码

import tensorflow as tf

替换为以下两行就可以正常的继续使用:

import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()

2.将上述语句替换之后,若没有解决兼容问题,出现:module ‘tensorflow‘ has no attribute ‘tf_slim’。可以安装slim库:

pip install --upgrade tf_slim

再将其导入即可:

import tf_slim as slim

你可能感兴趣的:(tensorflow,深度学习,人工智能)