Post on 13-Jan-2017
The TensorFlow dancecraze
Gabe Hamilton
TensorFlow is a Machine Learning library
What is Machine Learning?
usually: Statistical Pattern Matching
Using past measurements to predict something about a new piece of information
Does this picture contain a cat? Classification
How much is this home worth? Regression
Regression Talk https://docs.google.com/presentation/d/17FoojZ17DKDZqoByhB16PmtAXtnJtXSy1-feK9-A_fg/edit#slide=id.p10
There are lots of other ML libraries
Is it right for you?
Maybe Not, use Prediction APIhttp://www.slideshare.net/gabehamilton/intro-to-google-prediction-api-15167420
Or equivalent Amazon or Microsoft APIs
if you have a CSV of observed data.
But I’d like to get into the details or I am working on
a complex problem...
You do still have some choices.Some people prefer Keras on top of TensorFlow https://keras.io/ or tf-slim or pretty-tensor
So why use TensorFlow?
It’s not much different than the “easier” libraries.
You’ll want to understand more and more details anyway.
Because you like
It has the hottest dance moves!
Look out a Tensor!
Some Math and a Multi-Dimensional Array had a baby
For our purposesIt’s a multi-dimensional array.
Just remember that you are putting a mathematical construct in that array.
A tensor is a multi-dimensional array with certain transformation properties
You have some dataEven though it’s in a 2D spreadsheet it can describe a multidimensional space.
House Price Data 1 mile to park 2 bedrooms 1800 sq ft
So our Tensor is [1, 2, 1800]Square footage
Distance to park
# of bathrooms
Let’s build a Tensor Machine
= a Tensor
Comparing outputis our Output
But we are expecting
- = Loss
Iterate until we have a good model
DemoSimple Regression
https://github.com/gabehamilton/code-from-talks/tree/master/tensorflow_intro
Demo (run in Datalab)
Demo code: fitting a line to pointsDemo is of Datalab notebook# Create 100 phony x, y data points in NumPy, y = x * 0.1 + 0.3
tf.reset_default_graph()
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
x_measured = np.random.rand(100).astype(np.float32)
y_measured = x_measured * (0.1 + (0.05 *np.random.rand(100))) + 0.3
plt.scatter(x_measured, y_measured)
plt.xlabel('<- Bluer : Uniform Color : Redder ->')
plt.ylabel('Deaths per mission')
plt.title('Star Trek Uniform Color Mortality Incidence')
# plt.plot([0, 1], [0, 0.5], color='purple', lw=2)
# plt.plot([0, 1], [0.25, 0.5], color='purple', lw=2)
Measured Data
Demo code continuedimport tensorflow as tf
# Try to find values for slope and offset that match
# y_measured = slope * x_measured + offset
slope = tf.Variable(tf.random_uniform([1], -1.0, 1.0))
offset = tf.Variable(tf.zeros([1]))
y_predicted = slope * x_measured + offset
loss = tf.reduce_mean(tf.square(y_predicted - y_measured))
optimizer = tf.train.GradientDescentOptimizer(0.5)
train = optimizer.minimize(loss)
A test value for slope
Demo code continued# Launch the graph.
sess = tf.Session()
sess.run(tf.initialize_all_variables()) # and initialize variables
# Fit the line.
print 'step #\t', 'slope\t\t', 'offset'
for step in range(201):
sess.run(train)
if step % 20 == 0:
print step,'\t', sess.run(slope)[0], '\t', sess.run(offset)[0]
A test value for slope and offset
Q & APlus more slides for longer talks or
answering questions
Why TensorFlow is useful
Build your graph in a high level language, execute in fast implementations.
Distribute graph operations across processors.
Whole graph can be optimized.
4+ D Spaces
A given measurement all fits into a simple Tensor: [4, 4, 4, 3]
Some Tensors
Scalar 0.2Vector [1, 2, 1800]Matrix [2, 1, 1], [1, 2, 0], [0, 1, 0]
n-Tensors
Rank 2 Tensor
When a bunch of vectors hang out they make a Matrix
[2, 1, 1],[1, 2, 0],[0, 1, 0]