TensorFlow (TF) 和 TensorFlow Federated (TFF) 是不同的功能层,旨在很好地协同工作(顾名思义)。
不过,它们是不同的东西,旨在解决不同的问题。
我想知道以一种既可用于普通 TF 又可用于 TFF 工作负载的方式来描述计算的最佳方式是什么,以及人们可能希望避免的陷阱类型。
请您参考如下方法:
好问题。事实上,至少有 3 种方法来组合 TensorFlow 代码以与 TFF 一起使用,每种方法都有自己的优点。
- 使用 TensorFlow 的组合机制 (defuns) 是推荐的方法,假设它适用于您的特定情况。 TensorFlow 已经有了编写代码的机制,我们不想重新发明轮子。我们在 TFF (@tff.tf_computation) 中创建自己的组合机制的原因是为了处理特定的限制(例如在 TF 的接口(interface)级别缺乏对数据集的支持,以及 TF 组件需要与TFF 的其余部分),我们最好将此机制的使用限制在真正需要它的情况下。
如果可能,使用@tf.function 装饰 TensorFlow 组件,并将整个 TensorFlow block 包装为仅在顶层的 @tff.tf_computation,然后再将其嵌入 @tff.federated_computation。这样做的众多好处之一是,它允许您使用标准 TensorFlow 工具在 TFF 之外测试组件。
因此,鼓励并首选以下内容:
# here using TensorFlow's compositional mechanism (defuns)
# rather than TFF's to decorate "foo"
@tf.function(...)
def foo(...):
...
@tff.tf_computation(...)
def bar(...):
# here relying on TensorFlow to embed "foo" as a component of "bar"
...foo(...)...
- 使用 Python 的组合机制(简单的未修饰的 Python 函数)也是一个不错的选择,尽管它不如 (1) 更可取,因为它只会导致一个代码体在定义时嵌入到另一个代码体中,因为 TFF 会跟踪所有 TFF 修饰的 Python 函数都可以构建要执行的计算的序列化表示,而不会为您提供隔离或任何其他特殊好处。
您可能仍然希望使用此模式来允许您的组件在 TFF 之外进行测试,或者在 (1) 或 (3) 都不起作用的情况下进行测试。
因此,如果 (1) 不起作用,以下是您应该首先考虑的替代方案:
# here composing things in Python, no special TF or TFF mechanism employed
def foo(...):
# keep in mind that in this case, "foo" can access and tamper with
# the internal state of "bar" - you get no isolation benefits
...
@tff.tf_computation(...)
def bar(...):
# here effectively just executing "foo" within "bar" at the
# time "bar" is traced
...foo(...)...
- 不建议使用 TFF 的组合机制 (@tff.tf_computation),除非 - 如上所述 - 在需要它的情况下,例如当 TensorFlow 组件需要接受数据集作为参数时,或者如果它要只能从 @tff.federated_computation 调用。请记住,TFF 对数据集作为参数的支持仍处于试验阶段,虽然在某些情况下它可能是唯一的解决方案,但您仍可能会遇到问题。您可以期待实现的发展。
不鼓励(尽管目前有时需要):
# here using TFF's compositional mechanism
@tff.tf_computation(...)
def foo(...):
# here you do get isolation benefits - "foo" is traced and
# serialized by TFF, but you can expect that e.g., some
# tf.data.Dataset features won't work
...
@tff.tf_computation(...)
def bar(...):
# here relying on TFF to embed "foo" within "bar"
...foo(...)...
