Learning Machines

Taught by Patrick Hebron at ITP, Fall 2016

Week 8:

Technical Questions:

We will start today's class by answering any questions that arose from your initial explorations of TensorFlow, LaunchBot and Docker.

Practical Considerations for Application Development:

Creating a Web Frontend for a Python Project:

In most cases, you'll want to present your machine learning project in a more user-friendly form than a bunch of python scripts or a Jupyter notebook.

Perhaps the most likely outlet for your project would be a front-end website.

There are several libraries for building web applications with Python.

Below, we will look at Flask, a micro web framework written in Python and based on the Werkzeug toolkit and Jinja2 template engine.

To get started, please take a look at the Flask Installation Instructions.

In the example below, we won't use any actual machine learning code.

Instead, we will create a simple web page that takes user input via a form, passes the user input data to an "external library" that will double for a machine learning system and then render the results back into a web view.

First, let's create our "external library." In our case, this will be comprised by a simple function that takes a string input and returns the length of that string. This function is meant to simulate an external machine learning system in the simplest way possible. We can imagine replacing this function with something along the lines of Char-RNN, which might take a string as input and return a string that predicts what text should follow after the input string. The point here is to show how our Flask application can send data to an external library for processing and then inject the results back into a web view.

External Python Library (MyExternalLibrary.py):


# This function is nothing special.
# It is meant to simulate an external function we might call from our Flask app.
def getStringLength(input):
    return len( input )

Next, we will create the Flask application itself:

Flask Application (demo.py):


# Import Flask:
from flask import Flask, render_template, request, url_for

# Import the example "external library":
import MyExternalLibrary

# Initialize Flask app:
app = Flask(__name__)

# Define default route:
def index():
    # Render index page:
    return render_template( 'index.html' )

# Define form submission route:
@app.route('/hello', methods=['POST'])
def hello():
    # Get user's name from submitted form data:
    name_string = request.form[ 'user_name' ]
    # Call the "external function" on the user-submitted data:
    name_length = MyExternalLibrary.getStringLength( name_string )
    # Render hello page (with variables injected):
    return render_template( 'hello.html', name_str = name_string, name_len = name_length )

# Run app:
if __name__ == '__main__':
    app.run( host='', port=8080, debug=False )

In the Flask application code above, you will notice references to two HTML templates.

The index.html template will contain the user-input form. When the user submits the form, the Flask application defined above will capture the form input fields and use them to process the output.

Index Template (templates/index.html):

        <title>Basic Flask Tutorial</title>
        <div id="container">
            <h1>Basic Flask Tutorial</h1>
            <div id="content">
                <form method="post" action="{{ url_for('hello') }}">
                  <label for="user_name">Please enter your name:</label>
                  <input type="text" name="user_name" />
                  <input type="submit" />

Once the user input data has been processed by our "external library," we will want to inject the results back into a web view. The hello.html template below defines this resulting view. Notice that the code below contains placeholder variables for the name_str and name_len data points that will be injected by the Flask application.

Hello Template (templates/hello.html):

       <title>Basic Flask Tutorial</title>
       <div id="container">
           <h1>Basic Flask Tutorial</h1>
           <div id="content">
             Hello, <b>{{name_str}}</b>!<br/>
             Your name contains {{name_len}} characters.

We can run this Flask application from the command-line by calling:

python demo.py

We can then view our application by pointing a browser to the url:

We can also deploy this application to a cloud service:

Integrating TensorFlow into Native Applications:

It is possible to build and/or load TensorFlow graphs in languages other than Python.

In particular, the C and C++ TensorFlow APIs enable integration with native applications.

It should be noted, however, that this approach is significantly more complicated than working with TensorFlow's Python API.

I only recommend this approach if you have significant C/C++ development experience.

I have done a fair amount of work with the TensorFlow C and C++ APIs and can help anyone interested in pursuing this path during office hours.

Here are some helpful links:

From Intuition to Exploration:

"If you are in a shipwreck and all the boats are gone, a piano top buoyant enough to keep you afloat that comes along makes a fortuitous life preserver. But this is not to say that the best way to design a life preserver is in the form of a piano top. I think that we are clinging to a great many piano tops in accepting yesterday’s fortuitous contrivings as constituting the only means for solving a given problem."

- Buckminster Fuller, Operating Manual for Spaceship Earth

Project Development Exercise: