Home > other >  Trouble combining two saved Keras models
Trouble combining two saved Keras models

Time:01-03

I am loading in two saved functional models with shapes ((28, 28, 1) -> (28, 28, 1)) and ((28, 28, 1) -> (10, 1)). I need to combine them sequentially but I can not use Sequential(), since I want to admit a loss on one of the intermediate layers (the outputs of the first model).

This is what I am trying. The model compiles fine but .fit() throws the following error: TypeError: If shallow structure is a sequence, input must also be a sequence. Input has type: <class 'tensorflow.python.framework.ops.Tensor'>.

train_df = pd.read_csv("mnist/mnist_train.csv", header=None)
X = np.array(train_df.iloc[:, 1:].to_numpy()).reshape((-1, 28, 28, 1)).astype(np.float16)
X = X / 255.
Y = np.array(train_df.iloc[:, 0].to_numpy().reshape((-1)))
Y = to_categorical(Y, num_classes=10)


model_1 = models.load_model("model1")
model_2 = models.load_model("model2")
model_2.trainable = False


model = Model(inputs=[model_1.inputs], outputs=[model_1.outputs, model_2(model_1.outputs)])
model.compile(optimizers.Adam(), loss=[losses.MeanSquaredError(), losses.BinaryCrossentropy()], loss_weights=[0.5, 1])
plot_model(model, show_shapes=True)


model.fit(x=[X], y=[X, Y], epochs=10)

I have never had this issue with this exact data since it's just a numpy array, so I am unable to diagnose the issue here. The model compiles fine from what I understand:

plot

Edit: It seems that I am able to train either of the outputs individually by compiling single-output models, but it seems that I am both unable to repeat the same output (experimentally, has worked before in my experience) or get both outputs as a list.

CodePudding user response:

This line outputs=[model_1.outputs, model_2(model_1.outputs)]) creates a list of lists. Note: model_1.outputs is a list. You need to convert it to a single list,

outputs=[model_1.outputs[0], model_2(model_1.outputs)]
  • Related