Mark
Mark

Reputation: 55

Does "from tensorflow.python.keras.models import load_model" give you the model.predict function?

I just successfully finished training a tf.keras sequential model and wrote a separate "Flask" script where I load the saved model to an app I uploaded to Heroku. Everything worked. But, when I was playing, I realized for my requirements I only had to import Flask, request from flask as well as pandas, numpy and from tensorflow.python.keras.models import load_model:

from tensorflow.python.keras.models import load_model
from flask import Flask, request
import pandas as pd
import numpy as np

With all that model.predict works (after I run load_model() and the necessary pandas and numpy transformations to get the Flask/Post data in the right format (processed_features):

preds = flask_model.predict(processed_features).flatten()

What is allowing me to run model.predict()? Is model.predict() available when load_model is imported (I obviously didn't have to use load_model to run model.predict to run against my test data after training)? Or is model.predict() a generic function in python, numpy or pandas that knows how to execute a prediction through the keras model (somehow just leverages the weights, biases, model shape)?

While everything works, I think I am not understanding how it works.

Upvotes: 0

Views: 1464

Answers (1)

Manoj Mohan
Manoj Mohan

Reputation: 6044

The saved model to disk has both the model architecture and the weights. load_model API deserializes this file, builds and returns a Keras Model object. So, you're essentially invoking predict() on the Keras Model object. You can inspect the model object by invoking the following methods:

type(flask_model)  #check the type
dir(flask_model)   #list the attributes/methods available

Upvotes: 1

Related Questions