<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Dr. Wolfgang Steitz on Medium]]></title>
        <description><![CDATA[Stories by Dr. Wolfgang Steitz on Medium]]></description>
        <link>https://medium.com/@wsteitz?source=rss-729c0d41467a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*EANXRmoR8fC8_XVRHIFxAQ.jpeg</url>
            <title>Stories by Dr. Wolfgang Steitz on Medium</title>
            <link>https://medium.com/@wsteitz?source=rss-729c0d41467a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 14:51:56 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@wsteitz/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Training TensorFlow models in Python and serving with Go]]></title>
            <link>https://medium.com/travelaudience/training-tensorflow-models-in-python-and-serving-with-go-1b2a9386b0ff?source=rss-729c0d41467a------2</link>
            <guid isPermaLink="false">https://medium.com/p/1b2a9386b0ff</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[tensorflow]]></category>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[ai]]></category>
            <dc:creator><![CDATA[Dr. Wolfgang Steitz]]></dc:creator>
            <pubDate>Mon, 05 Mar 2018 13:52:31 GMT</pubDate>
            <atom:updated>2018-03-05T16:54:13.740Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qGsNiyUwc1ezOXYQ2MnCRA.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/photos/d0CasEMHDQs?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">David Clode</a> on <a href="https://unsplash.com/?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p><a href="https://travelaudience.com/">travel audience</a> is a digital advertising platform and as a data-driven company, we apply machine learning in several cases: selecting a creative we want to show to a particular user,or deciding if we want to participate in an auction are only two of many applications.</p><p>One of our more important models is used for click through rate (CTR) prediction, since the likelihood of a click on an advertising banner is an important ingredient for many decisions.</p><p>To be able to cope with the real-time requirements of programmatic advertising, our real-time system is implemented in <a href="https://golang.org/">Go</a>. However, for us Data Scientists, Python is the programming language we usually use to setup, train and evaluate our machine learning models. And this makes perfect sense, as Python became the go-to language for machine learning in recent years.</p><p>In our current stack, all model development and training happens outside of the real-time system and is implemented in Python. The parameters and coefficients of the trained models are exported periodically and then imported in Go to be used in our real-time system.</p><p>While this setup served us well in the past, it has several drawbacks:</p><ol><li>some parts, like feature pre-processing and prediction, need to be implemented and maintained twice: first in Python and then in Go.</li><li>any change in the algorithms or our feature engineering requires re-implementing huge parts.</li><li>long feedback cycles when live testing new models, as many parties are involved.</li><li>Inconsistencies between Go &amp; Python implementations are hard to detect and can decrease the performance</li></ol><p>Overall, we were stuck with the current algorithms and we moved slower than we would have liked.</p><p>To overcome these issues, we started looking at <a href="https://www.tensorflow.org/">TensorFlow</a>, which is a great, open-source machine learning framework developed by Google. TensorFlow is famous for neural networks and deep learning, however it also supports simpler models like logistic regression. Most importantly, it has an implementation of the “Follow the regularized leader” optimizer, which is what we currently use for CTR prediction. Besides being an incredible machine learning library, TensorFlow has APIs for different programming languages, with Python and Go among them, bingo!</p><p>Unfortunately, TensorFlow’s Go API isn’t documented as well as the Python version and there are no tutorials that matched our use case. So here is some code that demonstrates how to first setup and train a model in Python and in the second part, how to import that model in Go and start predicting.</p><h3>Model setup and training with Python</h3><p>Let’s create a basic Logistic Regression model to predict the likelihood of a click on an advertising banner. To keep it simple, we will only use three input features: the domain where the ad is shown, the device type (desktop vs mobile vs tablet) and the hour of the day. The first step is setting up our input feature columns. (Make sure to check <a href="https://developers.googleblog.com/2017/11/introducing-tensorflow-feature-columns.html">this excellent introduction</a> to TensorFlow’s feature columns.)</p><pre>domain = tf.feature_column.categorical_column_with_hash_bucket(<br> “domain”, 100000, dtype=tf.string)<br>hour = tf.feature_column.categorical_column_with_identity(“hour”, 24)<br>device_type = tf.feature_column.categorical_column_with_vocabulary_list(<br> “device_type”, vocabulary_list=[“desktop”, “mobile”, “tablet”],<br> default_value=0)</pre><pre>feature_columns = [domain, hour, device_type]</pre><p>Now we can setup our actual model using TensorFlow’s high-level Estimator interface. We are using the <a href="https://research.google.com/pubs/pub41159.html">Follow The Regularized Leader</a> (FTRL) optimizer, which is an algorithm that provides adaptive learning rates and is particularly well-suited for large data sets with extremely high dimensionality and multi-level categorical features. It was developed with click-prediction in mind and perfectly fits our use case.</p><pre>ftrl = tf.train.FtrlOptimizer(<br>    learning_rate=0.1,<br>    learning_rate_power=-0.5,<br>    l1_regularization_strength=0.001,<br>    l2_regularization_strength=0.0<br>    )</pre><pre>model_dir = &quot;.&quot;<br>estimator = tf.estimator.LinearClassifier(<br>    feature_columns=feature_columns,<br>    optimizer=ftrl,<br>    model_dir=model_dir<br>    )</pre><p>To be able to train and evaluate our classifier, we need to provide an input function that defines how to load data and pipe it to the model. Because we are dealing with huge datasets, we cannot just read everything into memory but need to stream the data. Here is an input function that works with CSV files, it accepts a list of file paths and allows to iterate over the samples. The data files contain the raw input feature plus our target value is_click that indicates if that particular ad impression resulted in a click event. Note that our input_fn function returns a callable.</p><pre>def input_fn(paths):<br>    names = [&quot;domain&quot;, &quot;hour&quot;, &quot;device_type&quot;, &quot;is_click&quot;]<br>    record_defaults = [[&quot;&quot;], [0], [&quot;desktop&quot;], [0]]</pre><pre>    def _parse_csv(rows_string_tensor):<br>        columns = tf.decode_csv(rows_string_tensor, record_defaults)<br>        features = dict(zip(names, columns[:-1]))<br>        labels = columns[-1]<br>        return features, labels</pre><pre>    def _input_fn():<br>        dataset = tf.data.TextLineDataset(paths)<br>        dataset = dataset.map(_parse_csv)<br>        dataset = dataset.batch(100)<br>        iterator = dataset.make_one_shot_iterator()<br>        features, labels = iterator.get_next()<br>        return features, labels</pre><pre>    return _input_fn</pre><p>Now everything is setup and we can finally train our classifier.</p><pre>paths = [&lt;some CSVs to train our model on&gt;]<br>estimator.train(input_fn=input_fn(paths), steps=None)</pre><p>To export our trained model, we are using TensorFlow’s own SavedModel format. It’s important to give names to tensors and operations, to be able to address the specific parts during model inference.</p><pre>columns = [(&#39;hour&#39;, tf.int64),<br>           (&#39;domain&#39;, tf.string),<br>           (&#39;device_type&#39;, tf.string)]<br>feature_placeholders = {<br> name: tf.placeholder(dtype, [1], name=name + &quot;_placeholder&quot;)<br> for name, dtype in columns<br>}<br>export_input_fn = tf.estimator.export.build_raw_serving_input_receiver_fn(<br>    feature_placeholders)<br>estimator.export_savedmodel(model_dir, export_input_fn)</pre><h3>Inspecting the exported model</h3><p>The TensorFlow installation contains a tool called <a href="https://www.tensorflow.org/programmers_guide/saved_model#cli_to_inspect_and_execute_savedmodel">saved_model_cli</a> to inspect a model. Let’s take a look:</p><pre>$ saved_model_cli show --dir &lt;model_export_dir&gt; --all</pre><pre>MetaGraphDef with tag-set: &#39;<strong>serve</strong>&#39; contains the following SignatureDefs:</pre><pre>signature_def[&#39;predict&#39;]:<br>The given SavedModel SignatureDef contains the following input(s):<br>inputs[&#39;device_type&#39;] tensor_info:<br>    dtype: DT_STRING<br>    shape: (-1)<br>    name: <strong>device_type_placeholder</strong>:0<br>inputs[&#39;domain&#39;] tensor_info:<br>    dtype: DT_STRING<br>    shape: (-1)<br>    name: <strong>domain_placeholder</strong>:0<br>inputs[&#39;hour&#39;] tensor_info:<br>    dtype: DT_INT64<br>    shape: (-1)<br>    name: <strong>hour_placeholder</strong>:0<br>The given SavedModel SignatureDef contains the following output(s):<br>outputs[&#39;class_ids&#39;] tensor_info:<br>    dtype: DT_INT64<br>    shape: (-1, 1)<br>    name: linear/head/predictions/ExpandDims:0<br>outputs[&#39;classes&#39;] tensor_info:<br>    dtype: DT_STRING<br>    shape: (-1, 1)<br>    name: linear/head/predictions/str_classes:0<br>outputs[&#39;logistic&#39;] tensor_info:<br>    dtype: DT_FLOAT<br>    shape: (-1, 1)<br>    name: linear/head/predictions/logistic:0<br>outputs[&#39;logits&#39;] tensor_info:<br>    dtype: DT_FLOAT<br>    shape: (-1, 1)<br>    name: linear/linear_model/weighted_sum:0<br>outputs[&#39;probabilities&#39;] tensor_info:<br>    dtype: DT_FLOAT<br>    shape: (-1, 2)<br>    name: <strong>linear/head/predictions/probabilities</strong>:0<br>Method name is: tensorflow/serving/predict</pre><p>The important part is that this tells us how the input &amp; output nodes are named, which features are part of the model and what data types are expected. For example, the output node providing probabilities of the current input data leading to a click, can be accessed by the name “linear/head/predictions/probabilities”. We will need this information for our Go implementation.</p><h3>Switching to Go</h3><p>Unfortunately, TensorFlow’s Go bindings are hard to use and the documentation is rather sparse. We rely on the <a href="https://github.com/galeone/tfgo#tfgo-tensorflow-in-go">tfgo</a> package to make our life a bit easier. First, we load the exported model. Then we create some sample input data and trigger the actual prediction. The placeholder names we use in the function Exec are taken from saved_model_cli tool output above.</p><pre>// Import model.<br>model := tg.LoadModel(&quot;EXPORT&quot;, []string{&quot;serve&quot;}, &amp;tf.SessionOptions{})</pre><pre>// Create input tensors.<br>deviceInput, _ := tf.NewTensor([]string{&quot;desktop&quot;})<br>hourInput, _ := tf.NewTensor([]int64{19})<br>domainInput, _ := tf.NewTensor([]string{&quot;example.com&quot;})</pre><pre>// Predict.<br>results := model.Exec(<br> []tf.Output{<br>   model.Op(&quot;linear/head/predictions/probabilities&quot;, 0),<br> }, map[tf.Output]*tf.Tensor{<br>   model.Op(&quot;device_type_placeholder&quot;, 0): deviceInput,<br>   model.Op(&quot;hour_placeholder&quot;, 0): hourInput,<br>   model.Op(&quot;domain_placeholder&quot;, 0): domainInput,<br>  ,<br> )<br>predictions := results[0].Value().([][]float32)</pre><p>That’s it! We have successfully imported our trained model and started predicting. In our example, we just passed one sample (someone visiting “example.com” on a desktop browser at 7pm), but we could also pass multiple samples in one go and do multiple predictions at the same time, which turned out quite useful in our case.</p><h3>Conclusions</h3><p>Moving to TensorFlow allows us to improve our technical architecture as well as our machine learning capabilities. The provided example code shows you how to setup, train and export a machine learning model in Python. And then how to import and apply that model in Go. The full, executable source code is available at: <a href="https://github.com/travelaudience/tensorflow-export-import-example">https://github.com/travelaudience/tensorflow-export-import-example</a></p><p>The advantages of the setup with TensorFlow</p><ul><li>Eliminate duplicate code and potential inconsistencies between Python and Go implementation</li><li>The general export/import flow is independent of the used machine learning algorithm and the used features. This gives us Data Scientists much more freedom to explore new algorithms and go crazy with our modelling. At the same time it shortens our development cycle and allows moving models to production faster.</li><li>We can take advantage of all current and future things available in the TensorFlow ecosystem.</li></ul><p>There are also some minor disadvantages</p><ul><li>Tensorflow’s Go binding are <em>not</em> covered by the TensorFlow API stability guarantees and may see huge changes or disappear completely.</li><li>With TensorFlow, doing predictions is slower compared to a similar model using our old approach. We are still working on this.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1b2a9386b0ff" width="1" height="1" alt=""><hr><p><a href="https://medium.com/travelaudience/training-tensorflow-models-in-python-and-serving-with-go-1b2a9386b0ff">Training TensorFlow models in Python and serving with Go</a> was originally published in <a href="https://medium.com/travelaudience">travel audience</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>