You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Heroku部署TensorFlow 2.0.0应用遇AttributeError问题求助

解决TensorFlow 2.0.0 + textgenrnn在Heroku上的AttributeError问题

问题根源

你遇到的AttributeError: module 'tensorflow_core._api.v2.config' has no attribute 'get_visible_devices'是因为TensorFlow 2.0.0版本中不存在tf.config.get_visible_devices()这个API——这个方法是在TensorFlow 2.1.0及更高版本才正式引入的。而你使用的textgenrnn库在检测GPU设备时调用了这个方法,导致在TF2.0环境下触发错误。

解决方案

方案1:升级TensorFlow版本(推荐,最简洁可靠)

这是最直接的解决方式,升级后不仅能解决API缺失问题,还能获得TF后续版本的bug修复和性能提升:

  1. 打开项目根目录下的requirements.txt文件(没有的话直接新建)
  2. 将TensorFlow的版本指定为2.1.0或更高,同时确保textgenrnn版本兼容,示例配置:
    tensorflow>=2.1.0
    textgenrnn>=1.4.1
    requests>=2.24.0
    # 其他依赖按需添加
    
  3. 重新提交代码到Heroku部署:
    git add requirements.txt
    git commit -m "Upgrade TensorFlow to 2.1.0+ for GPU API compatibility"
    git push heroku main
    
  4. 部署完成后用heroku logs --tail查看日志,确认错误消失。

方案2:修改textgenrnn的GPU检测代码(仅当无法升级TF时使用)

如果因特定原因必须保留TensorFlow 2.0.0,可以修改textgenrnn源码兼容旧版本:

  1. 将textgenrnn库的代码复制到项目目录中(避免直接修改pip安装的全局库)
  2. 找到textgenrnn/model.py第79行的代码:
    use_cudnnlstm = K.backend() == 'tensorflow' and len(config.get_visible_devices('GPU')) > 0
    
  3. 替换为TF2.0支持的GPU检测逻辑:
    import tensorflow as tf
    use_cudnnlstm = K.backend() == 'tensorflow' and tf.test.is_gpu_available()
    

    注意:tf.test.is_gpu_available()在TF2.1+版本已被标记为弃用,此方案仅作临时兼容,长期仍推荐升级TF。

额外部署提示

  • 确保使用Heroku的worker dyno运行训练任务(而非web dyno),匹配你的后台训练场景
  • 你的num_epochs=512设置可能导致训练时长超出Heroku免费dyno的运行限制,建议先减小数值测试部署流程,确认正常后再逐步调整。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.07 22:57:49