You need to enable JavaScript to run this app. TensorFlow调用build_info的cudnn_version_number触发AttributeError
优惠活动
大模型
产品
解决方案
定价
更多

TensorFlow调用build_info的cudnn_version_number触发AttributeError

报错具体原因
  • 你代码中访问的cudnn_version_number属性,是TensorFlow 2.5及更早版本中build_info模块对外暴露的独立字段,专门用来打印编译时关联的CuDNN版本。
  • 从TensorFlow 2.6版本开始,官方重构了build_info模块的返回结构:原本分散的CUDA版本、CuDNN版本、编译标识等信息,全部整合到build_info模块下的build_info字典对象中存储,不再保留独立的cudnn_version_number顶级属性。如果当前安装的TensorFlow版本≥2.6,直接访问旧版的属性名,自然会触发“模块无对应属性”的AttributeError。
  • 如果你安装的是纯CPU版本的TensorFlow,编译阶段本身没有集成CuDNN依赖,对应的版本字段根本不会被包含在模块定义中,同样会触发该类报错。
对应的解决方法
  • 先确认当前安装的TensorFlow版本:
    import tensorflow as tf
    print(tf.__version__)
    
  • 对2.6及以上版本,读取CuDNN版本的正确代码如下:
    from tensorflow.python.platform import build_info as tf_build_info
    # 从整合的字典结构中读取CuDNN版本,无对应字段时返回自定义提示
    cudnn_ver = tf_build_info.build_info.get("cudnn_version", "当前为CPU版TensorFlow,无内置CuDNN关联")
    print(cudnn_ver)
    
  • 如果需要写兼容多TensorFlow版本的通用读取逻辑,可以加上版本判断:
    import tensorflow as tf
    from tensorflow.python.platform import build_info as tf_build_info
    
    # 提取TensorFlow的次版本号做判断
    minor_ver = int(tf.__version__.split(".")[1])
    if minor_ver >= 6:
        cudnn_ver = tf_build_info.build_info.get("cudnn_version", "无CuDNN编译依赖")
    else:
        cudnn_ver = tf_build_info.cudnn_version_number
    print(f"当前TensorFlow关联的CuDNN版本:{cudnn_ver}")
    

内容的提问来源于stack exchange,提问作者user288609

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.06 03:24:00