Colab中导入TensorFlow Federated遇TypeError问题求助
问题
在Google Colab中执行以下代码时触发TypeError:
import numpy as np import tensorflow as tf import tensorflow_federated as tff
错误回溯信息:
TypeError Traceback (most recent call last) in 5 import numpy as np 6 import tensorflow as tf ----> 7 import tensorflow_federated as tff 14 frames /usr/lib/python3.8/typing.py in _type_check(arg, msg, is_argument) 147 return arg 148 if not callable(arg): --> 149 raise TypeError(f"{msg} Got {arg!r:.100}.") 150 return arg 151 TypeError: Callable[[arg, ...], result]: each arg must be a type. Got Ellipsis.
已尝试安装TensorFlow和TensorFlow Federated的低兼容版本,但问题未解决。
解决方法
- 升级Colab的Python版本到3.9或更高:该错误源于Python 3.8的
typing模块不支持TensorFlow Federated新版本中使用的Callable[[..., Result]]语法,升级到3.9+即可解决。在Colab中执行以下代码切换版本:!sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 1 !sudo update-alternatives --config python3 # 执行后重启Colab运行时 - 指定Python 3.8兼容的版本组合:若不想升级Python,可安装适配的旧版本,执行以下命令:
!pip uninstall -y tensorflow tensorflow-federated !pip install tensorflow==2.8.0 tensorflow-federated==0.20.0 - 强制重启运行时:版本切换或安装完成后,必须重启Colab运行时才能使配置生效。
内容的提问来源于stack exchange,提问作者ChakrabortyM
相关产品推荐
相关产品推荐

