问题
I am using the keras subclassing module to re-make a previously functional model that requires two inputs and two outputs. I cannot find any documentation on if/how this is possible.
Does the TF2.0/Keras subclassing API allow for mutli-input?
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])
回答1:
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.
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>)
来源:https://stackoverflow.com/questions/59743161/tensorflow-model-subclassing-mutli-input