Forums

Load model just once

Hi

I'm trying to load and predict on a Keras model, i've set backend to theano so it works. However, I don't know how to load the model just once at the start on pythonanywhere - normally i'd set the model to global and put it into if __name__ == '__main__':

however as I discovered this doesn't get executed, so not sure where it goes instead?

[edit by admin: formatting]

Is this a web app, or are you executing it from the console/editor?

In a web app's code, anything that is outside the if __name__ == '__main__' block will be executed when the website is loaded up.

So (assuming you're using Flask):

@app.route("/")
def index():
    return "hello"

do_something()

if __name__ == "__main__":
    do_something_else()

The function call to do_something will be made, but the call to do_something_else will not.