Aggregate tensorflow keras models - keras

Let say I have trained 300 different models, they all have the same architecture, only the weights are different.
Now, rather than calling my 300 models one after the others, how can I aggregate those models together so effectively having one aggregated X feature that would be fed into that model, and output results for the 300 model in one go?

Related

Average the same model over multiple runs or take an ensamble of a multitude of the same model?

Currently I'm averaging predictions using the same model, I re-initialize the model and fit to a different test set of data from the same data sample, then I average over all of the prediction accuracies
Seems there are multiple possible ways to do this.
Average together the predictions such as in this question.
Average all the final model weights together such as here.
I could find an average ensamble (but using the same model for all of the input models), or go a step further and make it a weighted average ensamble.
I could stack ensambles to create a model that learns which models are the best predictors.
While 1 and 2 deal with the same type of model e.g. keras models, in 3 and 4 I can use multiple different models. But is it a good approach to use 3 and 4 instead of 1 and 2 by just making all the models in the ensamble the same (though acting on different training sets). Furthermore, the ensamble approach allows for different types of models. It seems that 3 and 4 could be used instead of 1 or 2 as they are more general? That is, for example, using 3, finding a weighted average of N copies of the same model? If so, would stacking ensambles (in 4) be better than just weighting them (in 3), that is, creating a higher level model to learn which of the lower level models make better predictions?

combine multiple spacy textcat_multilabel models into a single textcat_multilabel model

Problem: I have millions of records that need to be transformed using a bunch of spacy textcat_multilabel models.
// sudo code
for model in models:
nlp = spacy.load(model)
for groups_of_records in records: // millions of records
new_data = nlp.pipe(groups_of_records) // data is getting processed bulk
// process data
bulk_create_records(new_data)
My current loop is as follows:
load a model
loop through records / transform data using model / save
As you can imagine, the more records i process, and the more models i include, the longer this entire process will take. The idea is to make a single model, and just process my data once, instead of (n * num_of_models)
Question: is there a way to combine multiple textcat_multilabel models created from the same spacy config, into a single textcat_multilabel model?
There is no basic feature to just combine models, but there are a couple of ways you can do this.
One is to source all your components into the same pipeline. This is very easy to do, see the double NER project for an example. The disadvantage is that this might not save you much processing time, since separately trained models will still have their own tok2vec layers.
You could combine your training data and train one big model. But if your models are actually separate that would almost certainly cause a reduction in accuracy.
If speed is the primary concern, you could train each of your textcats separately while freezing your tok2vec. That would result in decreased accuracy, though maybe not too bad, and it would allow you to then combine the textcat models in the same pipeline while removing a bunch of tok2vec processing. (This is probably the method I've listed with the best balance of implementation complexity, speed advantage, and accuracy sacrificed.)
One thing that I don't think has been tested is that you could try training separate textcat models at the same time with separate sets of labels by manually specifying the labels to each component in their configs. I am not completely sure that would work but you could try it.

Average weights from two .h5 folders in keras

I have trained two models on different datasets and saved weights of each model as ModelA.h5 and ModelB.h5
I want to average these weights and create a new folder called ModelC.h5 and load it on the same model architechture.
How do I do it?
Model trained on different datasets can't just be added like this. It looks something like this. Let's say like this, train one person to classify 1000 images into 5 classes, then, train another person to classify another 1000 images into same 5 classes. Now, you want to combine them into one.
Rather, what you can do is take ensemble of both the networks. There are multiple ways to ensemble the predictions of both models using Max Voting, Averaging or Weighted Average, Bagging and Boosting, etc. Ensemble helps to boost the weak classifiers into one strong classifier.
You can refer to this link to read more about different types of ensemble: Link

concatenation of two models feature vector for fusion

I've trained two models on VGG16 both models are trained on a different dataset.
I want to concatenate these two models for predicting new image.
So,My query is how can i concatenate these two models.

is there way to ensemble the prediction other than the take the mean average?

right now I'm just taking the mean average of 3 models predictions
predictions_model = [y_pred_xceptionAug,y_pred_Dense121_Aug,y_pred_resnet50Aug]
predictions = np.mean(predictions_model,axis=0)
is there a better way to ensemble other than just take a mean average?
One neural network based approach is to use the 3 model predictions as input to a further neural network.
More advanced approaches include bootstrap aggregating, where each model trains on a subset of the entire dataset before predictions are aggregated across models.

Resources