【问题标题】:How to get result of NN, modify result, then pass it to another NN?如何获取 NN 的结果,修改结果,然后将其传递给另一个 NN?
【发布时间】:2019-12-27 16:41:28
【问题描述】:

我是神经网络的初学者,计划使用 Tensorflow 估计器,请多多包涵。

是否可以从经过训练的神经网络中获取输出,对其进行修改,然后将结果传递给另一个模型?

我希望从第一个 NN 得到一个数字作为输出(如果可以通过 TensorFlow 实现的话),对该数字进行一些计算,然后将其传递给第二个 NN。

我需要为此使用两个单独的估算器吗?我可以将这两个 NN 放在同一个模型中,同时确保第一个 NN 的输出是一个数字吗?

如果这些问题中的任何一个是常识,我很抱歉,但我肯定对它们有点迷茫。

【问题讨论】:

    标签: python tensorflow neural-network


    【解决方案1】:

    为了更容易使用tf.estimator

    tf.estimators 上缺乏适当的文档使得这个问题很难仅用estimators 解决。您必须定义一个自定义估算器,但正如我所提到的,截至今天,没有关于如何完成此任务的端到端指南。
    我的建议是通过tf.keras API 定义您的模型,然后通过tf.keras.estimator.model_to_estimator 将编译后的模型转换为估计器(更多信息,请查看this link

    加入两个模型

    Keras Lambda Layer 允许将您的自定义操作定义为模型层。 您可以通过例如tf.keras.Sequential 定义您的两个模型,然后将它们与您的自定义 lambda 层“连接”。

    (可选)不稳定的替代方法:子类化 API

    您可以使用Keras Subclassing API 定义您自己的具有自定义前向传递的模型类,这将包括您的操作。然而,Subclassing API 存在一些序列化问题。来自Tensorflow Docs
    A subclassed model differs in that it's not a data structure, it's a piece of code. The architecture of the model is defined via the body of the call method. This means that the architecture of the model cannot be safely serialized.

    因此,我不确定这是否可以安全地转换为 tf.estimator

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-14
      • 2015-12-23
      • 2021-11-24
      • 1970-01-01
      • 1970-01-01
      • 2014-03-05
      • 1970-01-01
      相关资源
      最近更新 更多