我一直在尝试导入TensorFlow_federated,但偶然发现了一个错误。在互联网上进行了广泛的搜索之后,我还没有找到任何人遇到过同样的情况:
import collections
import numpy as np
import tensorflow as tf
import tensorflow_federated as tff
np.random.seed(0)
tff.federated_computation(lambda: 'Hello, World!')()
上面返回以下内容,有人遇到过类似的情况吗?
/usr/local/lib/python3.8/dist-packages/tensorflow_federated/python/learning/metrics/keras_utils.py in <module>
38 metrics_constructor: Union[MetricConstructor, MetricsConstructor,
39 MetricConstructors]
---> 40 ) -> Tuple[Callable[[], StateVar], Callable[[StateVar, ...], StateVar],
41 Callable[[StateVar], Any]]:
42 """Turn a Keras metric construction method into a tuple of pure functions.
/usr/lib/python3.8/typing.py in __getitem__(self, params)
814 f" Got {args}")
815 params = (tuple(args), result)
--> 816 return self.__getitem_inner__(params)
817
818 @_tp_cache
/usr/lib/python3.8/typing.py in inner(*args, **kwds)
259 except TypeError:
260 pass # All real errors (not unhashable args) are raised below.
--> 261 return func(*args, **kwds)
262 return inner
263
/usr/lib/python3.8/typing.py in __getitem_inner__(self, params)
837 return self.copy_with((_TypingEllipsis, result))
838 msg = "Callable[[arg, ...], result]: each arg must be a type."
--> 839 args = tuple(_type_check(arg, msg) for arg in args)
840 params = args + (result,)
841 return self.copy_with(params)
/usr/lib/python3.8/typing.py in <genexpr>(.0)
837 return self.copy_with((_TypingEllipsis, result))
838 msg = "Callable[[arg, ...], result]: each arg must be a type."
---> 839 args = tuple(_type_check(arg, msg) for arg in args)
840 params = args + (result,)
841 return self.copy_with(params)
/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.
我也有同样的问题,然后花了一段时间后,我得到了所需的解决方案。
!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()
%load_ext tensorboard
!pip install tensorflow
!pip install --quiet tensorflow-federated==0.20.0
import pandas as pd
import collections
import numpy as np
np.random.seed(0)
import tensorflow as tf
import tensorflow_federated as tff
np.random.seed(0)
tff.federated_computation(lambda: 'Hello, World!')()
以上代码运行良好,将能够解决Tensorflow与Colab联合的问题。