[英]Tensorflow Model Subclassing Mutli-Input
I am using the keras subclassing module to re-make a previously functional model that requires two inputs and two outputs.我正在使用 keras 子类化模块重新制作一个以前需要两个输入和两个输出的功能模型。 I cannot find any documentation on if/how this is possible.
我找不到任何关于这是否/如何可能的文档。
Does the TF2.0/Keras subclassing API allow for mutli-input? TF2.0/Keras 子类化 API 是否允许多输入?
Input to my functional model, and the build is simple:输入到我的功能模型,构建很简单:
word_in = Input(shape=(None,)) # sequence length
char_in = Input(shape=(None, None))
... layers...
m = Model(inputs=[word_in, char_in], outputs=[output_1, output_2])
Sub-classed model for multiple inputs is no different than like single input model.多输入的子分类模型与单输入模型没有什么不同。
class MyModel(Model):
def __init__(self):
super(MyModel, self).__init__()
# define layers
self.dense1 = Dense(10, name='d1')
self.dense2 = Dense(10, name='d2')
def call(self, inputs):
x1 = inputs[0]
x2 = inputs[1]
# define model
return x1, x2
You can define your layers in in __init__
and define your model in call
method.您可以在
__init__
定义您的层,并在call
方法中定义您的模型。
word_in = Input(shape=(None,)) # sequence length
char_in = Input(shape=(None, None))
model = MyModel()
model([word_in, char_in])
# returns
# (<tf.Tensor 'my_model_4/my_model_4/Identity:0' shape=(?, ?) dtype=float32>,
# <tf.Tensor 'my_model_4/my_model_4_1/Identity:0' shape=(?, ?, ?) dtype=float32>)
assume you have 3 inputs ( for example roberta model QA task)假设您有 3 个输入(例如 roberta 模型 QA 任务)
class MasoudModel2(tf.keras.Model):
def __init__(self):
# in __init__ you define all the layers
super(MasoudModel2, self).__init__()
self.dense1 = tf.keras.layers.Dense(4, activation=tf.nn.relu)
self.dense2 = tf.keras.layers.Dense(10, activation='softmax')
def call(self, inputs):
ids = inputs[0]
toks = inputs[1]
att_mask = inputs[2]
# let's skip real layers
a = self.dense1(ids)
b = self.dense2(att_mask)
return a, b
and then :进而 :
ids = tf.keras.Input((MAX_LEN), dtype = tf.int32)
att_mask = tf.keras.Input((MAX_LEN), dtype = tf.int32)
toks = tf.keras.Input((MAX_LEN), dtype = tf.int32)
model2 = MasoudModel2()
model2([ids, att_mask, toks])
JUST FOR MORE INFO : if you want functional API too.只是为了更多信息:如果您也想要功能性 API。
def functional_type():
ids = tf.keras.Input((MAX_LEN), dtype = tf.int32)
att_mask = tf.keras.Input((MAX_LEN), dtype = tf.int32)
toks = tf.keras.Input((MAX_LEN), dtype = tf.int32)
c = tf.keras.layers.Dense(10, activation='softmax')(ids)
d = tf.keras.layers.Dense(3, activation = 'softmax')(att_mask)
model = tf.keras.Model(inputs=[ids, toks, att_mask], outputs =[c, d])
return model
and then ( Note: last 2 indexes of the first argument are answers. )然后(注意:第一个参数的最后 2 个索引是答案。)
model.fit([input_ids[idxT,], attention_mask[idxT,], token_type_ids[idxT,]], [start_tokens[idxT,], end_tokens[idxT,]],
epochs=3, batch_size=32, verbose=DISPLAY, callbacks=[sv],
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.