Tensorflow实践报错汇总

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
安装tf-slim库

# slim = tf.contrib.slim
import tensorflow.compat.v1 as tf
import tf_slim as slim

tf.disable_v2_behavior()

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值