Traveling towards the Gobi desert, Mongolia 2011.

# Porting trained models using different machine-learning frameworks.

## A word of background

One problem frequently encountered in machine learning pipeline development is the need to distribute the tasks between training of the model and using a trained model for making new prediction. While training requires much more heavy computation power, once the weights have been found, using the model for prediction is a relatively simple task. Naturally, it seems fairly useful to export it at that point, and import it later (not necessarily on the same machine) and use it for predictions.

In this post, we are going to demonstrate how models in Scikit-Learn and Tensorflow can be exported after training by giving a simple one-dimensional linear regression problem example. By giving this example, which is a sort-of classical “hello world” application within machine learning language, we are going to show differences between the two frameworks. Once our model is trained and exported, we will later import it in a different script and and give it new examples.

## Scikit-Learn

### Simple model definition

First, let's have a look at our core model.

This is a very simple model and it takes hardly any time to train.

### Exporting

If we wish to save it now, we may do it at the level of the entire model itself, simply by serializing it's class.

### Importing

Importing it is even easier.

In fact, since we serialized the whole class, it is not necessary to import sklearn module again. The class takes care of it all. It "remembers".

Still, pickling should be used with caution, especially when working with different versions of either python of the libraries... or when unpickling somebody else files.

## Tensorflow

### The logic behind it

The logic of Tensorflow is quite different, and it may appear more difficult at first. Tensorflow defines all operations in terms of graphs and sessions. The first concept - graphs - can be compared to making a "skeleton" for the model. Given the constrains, such as different unknowns, mathematical operations, neural network architecture, etc. it maps the problem for us. Having it defined, it then executes different operations such as training the model, updating the weights, reading a value, or setting a new one, which is referred to as running a session. A session, can simply be imagined as a particular variables/operations state of a graph. Although it does sound much more complicated, it is in fact more CPU-efficient, since graphs are in a sense declared.

If we wish to reuse our model, we need to find a way to store and restore both of these. First, we need to save the graph and then we need to save the corresponding session, which holds trained weights.

### Model definition

Let’s start again with building our linear regression model.

As you see, the snippet contains declarations of variables and the so-called placeholders (Tensorflow’s way of reserving a place for “something to come”) together with a few operations to be executed on them. From sess = tf.Session() onward, we tell it what to do with this “situation” (graph) through the session. Note that we use sess.run(W) and sess.run(B) to evaluate the trained parameters. Calling W and B directly, would return object that belongs to the session.

### Exporting the weights

Now, when we wish to store the variables, we need to use the tf.train.Saver() object.

Here, we choose to pass toSave dictionary to the Saver()  object. Although we could skip this argument and hence let Saver() export all of our variables by default, it can help us to indicate what we actually want to store from the model.

### Importing the weights

Basic importing of the weights can be done the following way:

Again, we use the dictionary to indicate the variables of interest. Note that toSave and toLoad must be consistent. One more thing in addition to the Saver() object is the necessity of declaring W and B again. Although, it barely matters what we set as their initial values, the Saver() needs to be “aware” of their existence. Otherwise it will flag an error, ironically complaining that it has nothing to save.

## Keras

As you can see, doing something that seems relatively simple in Tensorflow requires quite much more work. You may as yourself, what is the point in going the "hard way" at all? Well, Tensorflow was actually designed to be computationally efficient, which is a real plus when working with complex models. Yet, whenever working on a new project, one of the hardest challenges is to balance program efficiency against programmer's efficiency. In other words, what if you can make a really good model, but the time it will take to get there it is long or, what's worse, hard to even estimate? On the other hand, what if you choose the opposite?

Fortunately, there is a middle path here: Keras , which lets us use the power of Tensorflow (or Theano), but provide convenient APIs that make the work really easy. Let's revisit the problem once again and see how we can go about exporting and importing of the model using this library.

### Exporting

First of all, Keras defines every model using the concept of layers. Starting off with input layer that defines the feature vector coming in to the model. Later, solving each task can usually be divided into four steps:

• Defining the model architecture.
• Compiling of this model using a specific optimizer.
• Training it, by providing input-output pairs (examples).
• Finally, using it to predict new data, which can be used to evaluate the model.

Now, the object model is going to be our trained model. There is a bunch of operations we can perform. For example, we may convert its architecture to JSON by calling model.to_json() and extract its weights using  model.get_weights(). We can also save the object "as is" and use it later.

 model.save('simple_model.h5')  That’s it. 'simple_model.h5' will contain everything.

### Importing

To reuse our trained model, all we need to do is:

Y is now going to contain new prediction points.

## Summary

In this post, we have seen examples of how importing and exporting of models can be realized using three, probably most popular, machine learning libraries. Given a very easy example, we saw differences in complexity of the task from user perspective. Comparing the three, it seems that Keras is probably the best way to start on machine learning, as it combines the simplicty of use with power coming from Tensorflow.