Translating to Keras shape issue

I need to translate an onnx model to keras (I’m using the unmaintained onnx2keras and modifying it as needed), and I’m bumping into an issue that’s proving hard to deal with.

The problem occurs when I attempt to add a numpy array to the output of a Keras layer with Add, or when I try to do the same kind of operation with Concat. I don’t want to use Lambda layers – these are problematic for me due to the fact that I need more follow-up translation steps. I’m able to create the Add layer initially, but part of the process involves getting a config dictionary from the Keras model and then recreating the model from the config. If I do this identity operation - model to config and back to model - I get a Keras error on dimension comparison between the layer output and the numpy array. The numpy array is of shape (1,63) and the layer output is (None,63) in the case of Add. The Keras shape checking code seems to think that the array has shape (1,(),(),()… 63 times(!)).

Can anyone help me with this issue? Unfortunately I’m not so knowledgeable in Keras, so I could be missing something simple.


To give a simple example of what I’m trying to do:

import tensorflow as tf
import numpy as np

input1 = tf.keras.layers.Input(shape=(63,))
x1 = tf.keras.layers.Dense(63, activation='relu')(input1)
input2 = tf.keras.layers.Input(shape=(63,))
x2 = tf.keras.layers.Dense(63, activation='relu')(input1)
x3 = tf.random.normal((1, 63))
x3 = tf.keras.backend.constant(x3)
added = tf.keras.layers.Add()([x1, x3])
model = tf.keras.models.Model(inputs=[input1], outputs=added)
config = model.get_config()
model2 = tf.keras.Model.from_config(config)


This fails at from_config in shape checking, though it fails in a slightly different way than my full example.