Learning Machines / Fall 2016

Week 8

Taught by Patrick Hebron at ITP, Fall 2016

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):

#!flask/bin/python

# 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):

#!flask/bin/python

# 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:
@app.route('/')
@app.route('/index')
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='0.0.0.0', 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):

<html>
    <head>
        <title>Basic Flask Tutorial</title>
    </head>
    <body>
        <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" />
                  <br/>
                  <input type="submit" />
                </form>
            </div>
        </div>
    </body>
</html>

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):

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

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:

http://0.0.0.0:8080

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:

  • Form groups with classmates who are interested in a similar problem domain.
  • With your group, discuss the current landscape of this domain. Here are a few possible considerations:
    • Who has this sort of problem or uses tools in this domain?
    • What tools already exist in this space?
    • What are the strengths and weaknesses of these tools?
    • Which of these weaknesses are the most challenging to overcome and which are the least?
  • Individually, make up a list of assumptions about this problem domain and its necessary or essential elements.
  • Get back together with your group and discuss your points of view, assumptions and premises. Here are a few possible considerations:
    • Which assumptions seem universal or inescapable?
    • Which seem contextual, cultural, temporary or like "piano tops?"
  • Individually, consider how this group discussion challenges or strengthens your earlier assumptions:
    • What do you feel more certain of?
    • What do you feel less certain of?
    • What tests, prototypes or experiments might help you to further investigate your assumptions?
    • What were the unique elements of your perspective on this problem domain?
    • What tests, prototypes or experiments could you produce to showcase a unique element of your perspective?

Homework

Assignment:

  • As a continuation of the From Intuition to Exploration exercise, please perform one (or a few) such experiments.
    • This can be done through code or any other means that seems appropriate to your inquiry.
    • It may be helpful to start with a paper prototype of an interface for your project.
    • To simulate the interactions between the user and the machine learning system, ask a friend to play the role of the user as you play the role of the machine. As you do so, try to imagine what knowledge you're drawing upon in order to respond to the user and consider how a machine could gain this kind of knowledge. Keep in mind that you are capable of far more complex thoughts than any existing machine learning system. Nevertheless, this process may be useful in helping to determine the kinds of interactions you're looking to achieve in your project as well as some of the underlying requirements of the machine learning system that will make these interactions possible. We will continue to refine the details over the coming weeks.

Readings:

Videos: