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, model1
and 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?
Edit:
The summaries of model1 looks like that, model2 looks the same but with LAYER_2 for each layer name:
Layer (type) Output Shape Param # Connected to
Input-Token (InputLayer) (None, 256) 0
Input-Segment (InputLayer) (None, 256) 0
Embedding-Token (TokenEmbedding [(None, 256, 768), ( 23440896 Input-Token[0][0]
Embedding-Segment (Embedding) (None, 256, 768) 1536 Input-Segment[0][0]
... (lots of layers in between)
NSP-Dense (Dense) (None, 768) 590592 Extract[0][0]
model1
andmodel2
created from the same original model? Do they use the sameoriginal_model.input
? – Daniel Möller