0
votes

I have 8 Keras models, the outputs from which I use to feed the final model.

Each model is a variation of the following:

model1 = Sequential()
model1.add(Dense(units = 32, kernel_initializer='glorot_normal', activation = 'relu', input_shape=(shape1,)))
model1.add(Dense(units = 32, kernel_initializer='glorot_normal', activation = 'relu'))
model1.add(Dense(units = 1, kernel_initializer='glorot_normal', activation = 'relu'))
model1.compile(optimizer = 'adam', loss='mean_absolute_error', metrics=['mse', 'mae', 'mape'])
model1.fit(X1, y, batch_size = 512, epochs=20)
X1_predictions = model1.predict(X1)

I train all 8 models separately, output predictions and then append in numpy...

X_final = np.append(np.append(X1_predictions, X2_predictions, axis=1), ..., X8_predictions, axis=1)

Then input X_final into a model.

Is there a more sane way to do this by horizontally stacking the 8 separate models into a single model? (i.e., where I wouldn't compile/fit/predict each individual model, but do it all in one?

1

1 Answers

1
votes

Use Functional API:

input = (
  tf.keras.layers.Input(shape=(shape1,)), 
  )
x1 = model1(input)
x2 = model2(input)
x3 = model3(input)
x4 = model4(input)
x5 = model5(input)
x6 = model6(input)
x7 = model7(input)
x8 = model8(input)
x = tf.concat((x1, x2, x3, x4, x5, x6, x7, x8), axis=1)
x = final_model(x)
model = tf.keras.Model(inputs=input, outputs=x)