【问题标题】:What is the recommended way to mix TensorFlow and TensorFlow Federated code?混合 TensorFlow 和 TensorFlow Federated 代码的推荐方法是什么?
【发布时间】:2019-03-21 18:09:56
【问题描述】:

TensorFlow (TF) 和 TensorFlow Federated (TFF) 是不同的功能层,旨在很好地协同工作(顾名思义)。

不过,它们是不同的东西,旨在解决不同的问题。

我想知道以原始 TF 和 TFF 工作负载都可以使用的方式来描述计算的最佳方式是什么,以及人们可能希望避免的那种陷阱。

【问题讨论】:

    标签: python tensorflow tensorflow-federated


    【解决方案1】:

    很好的问题。实际上,至少有 3 种方法可以组合 TensorFlow 代码以用于 TFF,每种方法都有其优点。

    1. 使用 TensorFlow 的组合机制 (defuns) 是推荐的方式,假设它适用于您的特定情况。 TensorFlow 已经有了编写代码的机制,我们不想重新发明轮子。我们在 TFF (@tff.tf_computation) 中创建自己的组合机制的原因是为了处理特定的限制(例如,在 TF 中缺乏对接口级别的数据集的支持,以及 TF 组件需要与TFF 的其余部分),理想情况下,我们会将这种机制的使用限制在真正需要它的情况下。

    如果可能,使用 @tf.function 装饰 TensorFlow 组件,并仅在顶层将整个 TensorFlow 块包装为 @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(...)...
    
    1. 使用 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(...)...
    
    1. 不推荐使用 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(...)...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-16
      • 2022-11-14
      • 1970-01-01
      • 1970-01-01
      • 2015-11-09
      • 2010-09-26
      相关资源
      最近更新 更多