我正在colab笔记本中工作,并且导入tff (import tensorflow_federated as tff)工作了几个月,但是突然,现在当我像往常一样尝试导入tff时,我遇到了这个问题。
!pip install --quiet --upgrade tensorflow-federated
!pip install --quiet --upgrade tensorflow-model-optimization
!pip install --quiet --upgrade nest-asyncio
import nest_asyncio
nest_asyncio.apply()
import numpy as np
import tensorflow as tf
import tensorflow_federated as tff
from tensorflow_model_optimization.python.core.internal import tensor_encoding as te
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-7-c8d605e9ca2e> in <module>()
2 import numpy as np
3 import tensorflow as tf
----> 4 import tensorflow_federated as tff
5
6 from tensorflow_model_optimization.python.core.internal import tensor_encoding as te
7 frames
/usr/local/lib/python3.7/dist-packages/tensorflow_federated/python/common_libs/structure.py in <module>()
263
264 def to_odict(struct: Struct,
--> 265 recursive: bool = False) -> collections.OrderedDict[str, Any]:
266 """Returns `struct` as an `OrderedDict`, if possible.
267
TypeError: 'type' object is not subscriptable
即使我在colab教程中运行它!在这个链接https://colab.research.google.com/github/tensorflow/federated/blob/master/docs/tutorials/federated_learning_for_image_classification.ipynb我有同样的问题!
感谢任何想法或建议!
最新的tensorflow_federated包需要Python版本更新到3.9,希望这对您有所帮助。
我在Google Colab上遇到了同样的问题,因为默认的Google Colab是在Python 3.7中。下面是我将Google Colab升级到Python 3.9的步骤
!wget -O mini.sh https://repo.anaconda.com/miniconda/Miniconda3-py39_4.9.2-Linux-x86_64.sh
!chmod +x mini.sh
!bash ./mini.sh -b -f -p /usr/local
!conda install -q -y jupyter
!conda install -q -y google-colab -c conda-forge
!python -m ipykernel install --name "py39" --user
希望这能帮助你像我一样继续前进!