问题目录
1. Tensorflow版本问题而导致报错
如果项目代码基于tensorflow1.0,而自己机器上安装的是tensorflow2.0,则会出现兼容问题,可用以下方法解决。
#导入tensorflow
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
2. AttributeError: module ‘tensorflow‘ has no attribute ‘compat’
如果解决版本问题时采用如下方式
sess = tf.compat.v1.InteractiveSession()
报错 AttributeError: module ‘tensorflow‘ has no attribute ‘compat’
解决办法同 1. Tensorflow版本问题而导致报错
3. tensorflow2.x中tf.contrib.layers.xavier_initializer()替换问题
tensorflow2.x中删去了tensorflow1.x中contrib,因此报错AttributeError: module 'tensorflow_core.compat.v1' has no attribute 'contrib'
。解决办法如下:
将
tf.contrib.layers.xavier_initializer()
替换成
tf.keras.initializers.glorot_normal()
出错代码:
def conv_op(input_op, name, kh, kw, n_out, dh, dw, p):
n_in = input_op.get_shape()[-1].value
with tf.name_scope(name) as scope:
kernel = tf.get_variable(scope + "w",
shape=[kh, kw, n_in, n_out],
dtype=tf.float32,
#下行报错
initializer=tf.keras.initializers.glorot_normal())
conv = tf.nn.conv2d(input_op, kernel, (1, dh, dw, 1), padding='SAME')
bias_init_val = tf.constant(0.0, shape=[n_out], dtype=tf.float32)
biases = tf.Variable(bias_init_val, trainable=True, name='b')
z = tf.nn.bias_add(conv, biases)
activation = tf.nn.relu(z, name=scope)
p += [kernel, biases]
return activation
4. tf2.x中使用slim报错
参考链接https://blog.csdn.net/lingchuxiao/article/details/109489809
Traceback (most recent call last):
File "/Users/jyc/PycharmProjects/研究生新生教程作业/Tensorflow/GoogleNet.py", line 19, in <module>
slim = tf.contrib.slim
AttributeError: module 'tensorflow_core.compat.v1' has no attribute 'contrib'
原因:
#下行报错
slim = tf.contrib.slim
解决办法:tensorflow2.0中要单独安装tf-slim库
终端运行pip install --upgrade tf_slim
# slim = tf.contrib.slim
import tensorflow.compat.v1 as tf
import tf_slim as slim
tf.disable_v2_behavior()