Skip to content

Models

Models are created on the rsrv and are then stored in s3 in the bucket predict.s3.withcubed.com.

Note

Although we use keres to create and load our models we use the keres packages inside of the Tensorflow API.

Loading models into the accounts.

Models are loaded into account using Account.update_models() when loading a model into an account (or using it for prediction later) you must do it within the context of a keras session:

    with CustomObjectScope({'GlorotUniform': glorot_uniform()}):
        # set session
        Model.session = tf.compat.v1.Session()
        with Model.session.as_default():
            # set the graph on this thread.
            with Model.session.graph.as_default():
                Model.model = load_model(file_path + Model.name, custom_objects=None, compile=False)
                Model.last_updated = model_train_time
We set the Object scope to GlorotUniform as this is also the scope used to build the model over on the rsrv, we then ensure we have a session set up before we load the model.

Predicting probability for a product.

    def compute_prediction(self, model, data, visit):
    prediction = [[0.0]]
    with model.session.as_default() as sess:
        with model.session.graph.as_default() as gph:
            try:
                prediction = model.model.predict_proba(data)
            except ValueError as ex:
                self.logging_exception(ex, visit)

When predict_proba() is called on the model, keres needs to build a load of stuff internally, the first time this happens prediction takes around a second, by keeping both the loading of the model and the prediction of the product in sessions it allows all subsequent predictions to be speedy.