Crazzay1903
Crazzay1903

Reputation: 45

Parallel execution of model prediction in a for loop

I want to run predictions of a keras model on my input several times in a for loop. Is there a way to parallelize this for loop as each iteration of the loop is independent of the other? I just want to store the predictions of each iteration in the mc_predictions array.

mc_predictions = []
for i in range(100):
    y_p = model.predict(x)
    mc_predictions.append(y_p)        

I am using a dropout layer in my model wheer I have set the training attribute to True. Hence, every time I would be getting different results as different neurons would be deactivated in each iteration. I am using monte carlo estimation here.

Upvotes: 2

Views: 893

Answers (2)

user3666197
user3666197

Reputation: 1

Q : "Is there a way to parallelize this for loop as each iteration of the loop is independent of the other?"

Well, without a deeper look inside the actual model-instance not.

The model-instance could easily be a state-full-system and changing some part of its internal state by each call to the .predict()-method ... so one cannot be sure a-priori, without re-inspecting the model's implementation, could one?
– user3666197 6 min ago

As the trailer note was added "...different neurons would be deactivated in each iteration." the landscape has changed.

There is no way to make the by-definition pure-[SERIAL] process to "happen" become a True-[PARALLEL].

Q.E.D.


But is this really a pure serial process? I mean, each iteration is independent. What neurons get deactivated in an iteration does not matter as long as random neurons are getting deactivated.
Crazzay1903 4 hours ago

In the most probable case yes, it is a pure-[SERIAL] process ( unless somehow & thoroughly proven not to be - but, then it would also fail in a trap of not remaining a repeatable & re-validation ready science ( one may argue it could legally become a principally non-deterministic random process - fair, ok - yet, even then the proof of having achieved such a robustly non-deterministic random process, so as to be able to proof it does not lose any of its ultimate-randomness-quality property under some True-[PARALLEL] process implementation ( if there were any such after having been tried & proofed to be such ) but then, the .predict()-method must, and cannot otherwise, becomes more a clairvoyance, than any rigorous, quantitatively supported fair scientific method, doesn't it? ) which is not a goal, is it? ).

Q.E.D.

Upvotes: 2

vbhargav875
vbhargav875

Reputation: 887

You would get the same result at each iteration, I don't get the point of why you would want to do this.

Maybe you're be looking for something like a stratified k-fold approach, where you model trains and validates across different samples of data.

Upvotes: 0

Related Questions