介于两者之间的条件的 Keras 垂直集成模型
Keras vertical ensemble model with condition in between
我训练了两个不同的模型
- ModelA:检查输入文本是否与我的工作相关(二元分类器[related/not-related])
- ModelB:相关文本的分类器(Classifier [good/normal/bad])。只有相关文本从 ModelA
转发到此模型
我要
- ModelC:输出 [good/normal/bad/不相关]
的集成分类器
- 我会分批训练。并且可以在一批中混合
not-related
和 good/normal/bad
。我需要他们分开。
我需要的一些伪代码
# 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)
我训练了两个不同的模型
- ModelA:检查输入文本是否与我的工作相关(二元分类器[related/not-related])
- ModelB:相关文本的分类器(Classifier [good/normal/bad])。只有相关文本从 ModelA 转发到此模型
我要
- ModelC:输出 [good/normal/bad/不相关] 的集成分类器
- 我会分批训练。并且可以在一批中混合
not-related
和good/normal/bad
。我需要他们分开。
我需要的一些伪代码
# 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)