How to convert a string to a tensorflow model?

0

Issue

So, I have created this model model1 and I am trying to create a function which takes in different models like model1 model2 etc and evaluates them. In the process, I have a string called "model1" which I would like to convert to model1 so I can pass the .evaluate() method to it. Would this be possible?

Solution

Assuming that your previously trained models are saved to disk, you may just want to load them back into memory and evaluate as:

model_names = ["model1", "model2", "model3", ..., "model10"]
for model_name in model_names:
    model = tf.keras.models.load_model("path/to/location/" + model_name)
    score = model.evaluate(X_test, y_test, ...)

Also, if you plan to have lots of models with sequential names, like model1, model2, …, model_n, you can use list comprehension instead of hard coding the model names like:

model_names = ["model" + str(x) for x in range(1, n)]

Please note that these are pseudo-codes and you need to complete … according to your needs.

Answered By – ihpar

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More