Dkova
Dkova

Reputation: 1097

Keras - combine multiple model into one model and use predict_prob to calculate variance

I'm using Keras and built 5 different models for binary classification. on each model, I'm using the predict_proba to get the probability of the classification.

The 5 models are logistic regression:

def build_logistic_model(input_dim, output_dim):
    model = Sequential()
    model.add(Embedding(input_dim, embed, input_length=max_length))
    model.add(Flatten())
    model.add(Dense(output_dim, input_dim=embed, activation='softmax'))

so, now I have a list of 5 models. and I want to merge the output of those model into a new Keras model and to get out the AVG and STD of the probability of those 5 models.

is there a way to do it so, in the end, I will get 1 model that merge into him those 5 models? I will send input to those 5 models and will get the avg and std?

Upvotes: 0

Views: 493

Answers (1)

Thibault Bacqueyrisses
Thibault Bacqueyrisses

Reputation: 2331

You can create a new model like that:

from keras import backend as K


def std_layer(input):
    return K.std(input)


model_input = Input(shape=input_dim)

def get_avg_std_model(models, model_input):


    outputs = [model.outputs[0] for model in models]
    avg = Average()(outputs)
    a = Concatenate()(outputs)
    std = Lambda(std_layer)(a)
    model = Model(model_input, [avg, std], name='get_avg_std')

    return model

models = [model1 , model2, model3, model4, model5]


get_avg_std = get_avg_std_model(models, model_input)

You will need to define all your models like that :

model_input = Input(shape=input_dim)

def model_example(model_input):

    x = Dense(1)(model_input)

    model1 = Model(inputs=model_input, outputs=x)

    return model 

model1 = model_example(model_input)
model1.compile(optimizer='adadelta', loss='mse')

All that should gives you what you need !
Keep me in touch.

Upvotes: 1

Related Questions