tensorflow 各种报错总结

本文主要解析了在使用TensorFlow进行深度学习模型开发时遇到的一些典型错误,包括属性错误、版本不一致导致的问题以及常见的ValueError等,并给出了相应的解决办法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 'list' object has no attribute 'lower' 报错如下
Traceback (most recent call last):
  File "H:/FasionAI/MyNet/train.py", line 27, in <module>
    train_logits = model.inference(train_batch, BATCH_SIZE, N_CLASSES)
  File "H:\FasionAI\MyNet\CnnNet.py", line 60, in inference
    pool2 = tf.nn.pool(conv2, [1, 2, 2, 1], [1, 2, 2, 1], padding='SAME', name='pooling2')
  File "E:\softinstall\Anaconda\lib\site-packages\tensorflow\python\ops\nn_ops.py", line 931, in pool
    (pooling_type.lower()), [input]) as scope:
AttributeError: 'list' object has no attribute 'lower'

问题原因:是tf.nn.max_pool()而不是tf.nn.pool()

2.tensorflow版本不一致

    1)AttributeError: module 'tensorflow' has no attribute 'scalar_summary'

         tf.image_summary('images', images)改为:tf.summary.image('images', images)

    2)AttributeError: 'module' object has no attribute 'scalar_summary'

        tf.scalar_summary('images', images)改为:tf.summary.scalar('images', images)

     3)AttributeError: module 'tensorflow' has no attribute 'merge_all_summaries'

        tf.merge_all_summaries()改为:summary_op = tf.summary.merge_all()

      4)AttributeError: 'module' object has no attribute 'SummaryWriter'

         tf.train.SummaryWritter改为tf.summary.FileWriter

3.pycharm程序无法识别自己写的程序import时显示红线

    1)打开File--》Setting—》打开 Console下的Python Console,把选项(Add source roots to PYTHONPAT)点击勾选上

    2)右键点击自己的工作空间,找下面的Mark Directory as 选择Source Root,就可以解决上面的问题了

4.ValueError: Both labels and logits must be provided.

  File "H:\FasionAI\MyNet\resnetmodel\resnet_train.py", line 35, in train
    loss_ = loss(logits=logits, labels=labels)
  File "H:\FasionAI\MyNet\resnetmodel\resnet.py", line 148, in loss
    cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=labels,logits=logits)
  File "E:\softinstall\Anaconda\lib\site-packages\tensorflow\python\ops\nn_ops.py", line 1935, in sparse_softmax_cross_entropy_with_logits
    labels, logits)
  File "E:\softinstall\Anaconda\lib\site-packages\tensorflow\python\ops\nn_ops.py", line 1715, in _ensure_xent_args
    raise ValueError("Both labels and logits must be provided.")
ValueError: Both labels and logits must be provided.

    查看源码后发现是:

  if labels is None or logits is None:
    raise ValueError("Both labels and logits must be provided.")

    也就是说有一个输入为空,后检查发现调用函数 inference_small_config(x, c)时没有return 正确应该是

   return inference_small_config(x, c)  



评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AICVer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值