Friday, 22 November 2019

Keras functional api multiple input: The list of inputs passed to the model is redundant

I have a huge networt (keras-bert) which works fine for classification. Since my data has two different columns, I'd like to fine-tune a BERT model for each column and connect them in the final layer. But I get the following error:

---> 20 model = keras.models.Model(inputs=[inputs1, inputs2], outputs=outputs)

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/network.py in _validate_graph_inputs_and_outputs(self)

1620 """Validates the inputs and outputs of a Graph Network."""

1621 # Check for redundancy in inputs.

-> 1622 if len(set(self.inputs)) != len(self.inputs):

1623 raise ValueError('The list of inputs passed to the model '

1624 'is redundant. '

TypeError: unhashable type: 'list'

In my code I have two bert models, model1and model2. With just one model it worked fine. The only things I added were that 2 models instead of one are loaded from checkpoint and the second input-layer and the concatenation of dense1 and dense2:

#load_trained_model_from_checkpoint is defined here:
# https://github.com/CyberZHG/keras-bert/blob/master/keras_bert/loader.py
model1 = load_trained_model_from_checkpoint(
    config_path,
    checkpoint_path,
    training=True,
    trainable=True,
    seq_len=SEQ_LEN,
    )
model2 = load_trained_model_from_checkpoint(
    config_path,
    checkpoint_path,
    training=True,
    trainable=True,
    seq_len=SEQ_LEN,
)

inputs1 = model1.inputs[:2] #model 1 for titles
inputs2 = model2.inputs[:2] #model 2 for texts
dense1 = model1.get_layer('NSP-Dense').output
dense2 = model2.get_layer('NSP-Dense').output
outputs = keras.layers.Dense(len(test_title_y[0]), activation='sigmoid')(keras.layers.concatenate([dense1, dense2]))


model = keras.models.Model(inputs=[inputs1, inputs2], outputs=outputs)

What am I overseeing? Do I somehow have to wrap the input?

Edit: I suspect that the problem has something to do with my input being a list of lists: the inputs1 and inputs2 look like that:

[<tf.Tensor 'Input-Token:0' shape=(?, 256) dtype=float32>, <tf.Tensor 'Input-Segment:0' shape=(?, 256) dtype=float32>]
[<tf.Tensor 'Input-Token_1:0' shape=(?, 256) dtype=float32>, <tf.Tensor 'Input-Segment_1:0' shape=(?, 256) dtype=float32>]

Can I somehow reshape or concatenate my input to overcome this error?



from Keras functional api multiple input: The list of inputs passed to the model is redundant

1 comment:

  1. why dont you use tensorflows keras API??? Thats cooler than the standalone api. And I guess its nicer to be on the awesome side...

    ReplyDelete