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

TensorFlow导入错误:无法导入experimental_functions_run_eagerly

解决TensorFlow导入错误:ImportError: cannot import name 'experimental_functions_run_eagerly'

问题详情

执行import tensorflow as tf时触发错误:

ImportError: cannot import name 'experimental_functions_run_eagerly' from 'tensorflow.python.eager.def_function'

用户使用的导入代码:

import tensorflow_datasets as tfds
from tensorflow_examples.models.pix2pix import pix2pix
import keras

import os
import time
import matplotlib.pyplot as plt
from IPython.display import clear_output
import cuda

AUTOTUNE = tf.data.AUTOTUNE

import PIL
from PIL import Image
import numpy as np
import pandas as pd

from tensorflow.python.client import device_lib

关键环境与包版本:

  • Python 3.9.13
  • TensorFlow 2.11.0
  • Keras 2.11.0
  • 环境:VSCode + 本地Jupyter Notebook,WSL2 Ubuntu虚拟环境(为GPU计算升级),此前未使用Anaconda/Miniconda
  • 此前导入正常,今日突发报错,注释前3行后导入keras仍报错,已尝试按官方文档安装tensorflow(替代tensorflow-gpu)、升级WSL到2但未解决

解决方案

1. 彻底重装TensorFlow及关联包

该错误多因安装文件损坏或版本冲突导致,执行以下步骤重装:

  • 卸载相关包:
    pip uninstall -y tensorflow tensorflow-estimator keras tensorflow-datasets
    
  • 清理pip缓存:
    pip cache purge
    
  • 重新安装匹配版本的包:
    pip install tensorflow==2.11.0 keras==2.11.0 tensorflow-datasets
    

2. 排查环境包冲突

确认当前Python环境中无多版本TensorFlow/Keras共存:

  • 执行命令查看相关包版本:
    pip list | grep -E "tensorflow|keras"
    
    确保仅存在一组匹配的版本(如TensorFlow 2.11.0与Keras 2.11.0)

3. 修正WSL2 GPU配置

若为GPU使用问题,确认WSL2环境的CUDA配置:

  • 确保Windows主机安装了最新NVIDIA驱动
  • 在WSL2终端检查CUDA版本:
    nvcc --version
    
    需保证CUDA版本与TensorFlow 2.11.0兼容(支持CUDA 11.2-11.7)

4. 调整导入顺序与方式

避免独立Keras包与TensorFlow内置Keras冲突:

  • 修改导入代码,优先导入TensorFlow,使用tensorflow.keras替代独立keras:
    import tensorflow as tf
    from tensorflow import keras
    import tensorflow_datasets as tfds
    from tensorflow_examples.models.pix2pix import pix2pix
    
    # 其余导入代码保持不变
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 13:40:42