介于两者之间的条件的 Keras 垂直集成模型

Keras vertical ensemble model with condition in between

我训练了两个不同的模型

我要

我需要的一些伪代码

# Output of modelA will be a vector I presume `(1, None)` where `None` is batch
def ModelC.predict(input):
    outputA = ModelA(input)
    if outputA == 'not-related':
        return outputA
    return ModelB(outputA)

我不知道如何在模型推理中包含 if 逻辑。我怎样才能做到这一点?

只需定义您自己的模型。我很惊讶你的其他模型输出的是字符串而不是数字,但没有更多信息,这就是我能给你的所有信息,所以我假设模型 A 的输出是一个字符串。

import tensorflow as tf

class ModelC(tf.keras.Model):

  def __init__(self, A, B):
    super(ModelC, self).__init__()
    self.A = A
    self.B = B

  def call(self, inputs, training=False):
    x = self.A(inputs, training)
    if x == 'not-related':
        return x
    return self.B(inputs, training)