Introduction to Machine Learning with TensorFlow

Click here to load reader

Embed Size (px)

Transcript of Introduction to Machine Learning with TensorFlow

Introduction to Machine Learning with TensorFlow

Introduction to Machine Learning with TensorFlowPaolo Tomeo

[Replace the presenter name and social on this slide with your own]Hello, I'm [your name], and I'm not an expert in Machine Learning.That's what's so cool about this presentation.In fact, as long as you know how to use an API, you can get started using machine learning today.

Open source Machine Learning library

Especially useful for Deep Learning

For research and production

Apache 2.0 license

Next, Ill introduce you to TensorFlowTensorFlow is an open source library for machine intelligence. And its the most popular ML library on Github.Its used by researchers, data scientists, and developers. Its capable of large-scale ML, and production ready.If youre new to ML, you can use TensorFlow to explore (we encourage you do so, and dive in!)We also have a Udacity Course (search for Udacity Deep Learning) you can take to learn more

Machine LearningComputer algorithms for learning to do something- learning to complete a task- make accurate predictions- to behave intelligently

The focus is on automatic methods: learning without any human intervention

In the next couple slides, Ill show you several Google products that are powered by Machine Learning and/or TensorFlowThen, well look at code to recognize handwritten digits.This is often considered the hello world of computer visionWe can do this in only six lines of code, using TF.Learn: a high level machine learning library, on top of TensorFlow

Hello World

Image from

In the next couple slides, Ill show you several Google products that are powered by Machine Learning and/or TensorFlowThen, well look at code to recognize handwritten digits.This is often considered the hello world of computer visionWe can do this in only six lines of code, using TF.Learn: a high level machine learning library, on top of TensorFlow

What we see

What the computer sees

Vision feels easy to people. But for computers, its hardWhereas we see an apple, the computer sees array of pixelsYou wouldnt want to write if statements to analyze theseTheres too much complexity and variation in the worldWe can solve this easily using Machine Learning.

Complete codeimport tensorflow as tfmnist = tf.contrib.learn.datasets.load_dataset('mnist')classifier = tf.learn.LinearClassifier(n_classes=10), mnist.train.labels)score = metrics.accuracy_score(mnist.test.labels, classifier.predict(mnist.test.images))print('Accuracy: {0:f}'.format(score))

Here's the complete code!Be sure to check the website for the latest syntax, since TensorFlow Learn is actively being developed, and still very newVideo of installing TensorFlow and running this code here:

To recognize images, we use a deep neural networkThis presentation is not a crash course, but let me quickly introduce a few conceptsYou can think of machine learning as programming + dataInstead of writing rules to recognize images, we write algorithms that find patterns in dataDeep Learning is a branch of machine learning that focuses on neural networks.This network classifies images as cats or dogs.The input to the network is the raw pixelsThe output of the network is a prediction for whether the image is a cat or dog.The network is composed of layers of artificial neurons. These are loosely inspired by the way the brain works, but its more appropriate to think of them as simple mathematical functionsEach neuron has a weight. Initially, the weights are random, and the network wont accurately classify images.We adjust the weights using an algorithm called backpropagation. Want to learn more? Google has a Udacity class you can take. Search for Deep Learning Udacity

More notes:Deep Learning has achieved state of the art results in many domains, like image and speech recognitionAdvantages over traditional ML: when youre using Deep Learning, you dont need to do feature engineering. Just use the raw input.Disadvantages: requires lots of data, compute tim

Biologically Inspired Artificial Neural NetworkImage from

Deep Neural Network (DNN)

Iris Dataset

Deep Learning Classifier for Iris Dataset (1/3)Tutorial from import tensorflow as tfimport numpy as np# Data setsIRIS_TRAINING = "iris_training.csvIRIS_TEST = "iris_test.csv"# Load datasets.training_set = tf.contrib.learn.datasets.base.load_csv(filename=IRIS_TRAINING, = tf.contrib.learn.datasets.base.load_csv(filename=IRIS_TEST,

Deep Learning Classifier for Iris Dataset (2/3)Tutorial from # Specify that all features have real-value datafeature_columns = [tf.contrib.layers.real_valued_column("", dimension=4)]# Build 3 layer DNN with 10, 20, 10 units respectively.classifier = tf.contrib.learn.DNNClassifier(feature_columns=feature_columns, hidden_units=[10, 20, 10],n_classes=3,model_dir="/tmp/iris_model")# Fit,, steps=2000)

Deep Learning Classifier for Iris Dataset (3/3)Tutorial from # Evaluate accuracy.accuracy_score = classifier.evaluate(,["accuracy"]print('Accuracy: {0:f}'.format(accuracy_score))# Classify two new flower samples.new_samples = np.array([[6.4, 3.2, 4.5, 1.5], [5.8, 3.1, 5.0, 1.7]], dtype=float)y = classifier.predict(new_samples)print('Predictions: {}'.format(str(y)))

Getting Started Exercises

Now lets look at a couple getting started exercises you can do next

Lots of tutorials at

There are many great tutorials at tensorflow.orgCheck out MNIST for Beginners to see how to write lower level TF codeCheck out MNIST for Experts to see how to get 99% accuracyAnd try the TF.Learn Quickstart for something similar to what we did here.

Codelab - - for Poets

TensorFlow for Poets is a codelab that will walk you through training your own image recognizer, without having to write a single line of codeTo use TensorFlow for Poets, you start by taking pictures of what you want to recognizeFor example, to build the museum app - youd take about 50 photos of each piece you want to recognize, and place them in directories on your laptopThis is your training dataThe next ingredient is Inception. As I mentioned before, this is a image recognition model, open sourced by Google.Under the hood. TensorFlow for Poets uses a technique Transfer Learning is to retrain Inception to recognize your imagesYou only need about 50 images of each thing you want to recognize to get started

Mobile TensorFlow

TensorFlow also comes with samples for Android, iOS, and Raspberry PIOnce you train a model using TensorFlow for Poets, you can install it on your phone (this works best on Android at the moment, its hot off the press)Now you have an app that recognizes images, running on device, and completely open source

Claude Monet - Bouquet of SunflowersImages from the Metropolitan Museum of Art (with permission)Image by @random_forests

Heres an app that was built using TensorFlow for Poets and TensorFlow Mobile to identify paintings at a museum

A little more TensorFlow

Now lets look

A multidimensional array.

A graph of operations.

Whats a tensor? What does it mean for them to flow?Tensor: multidimensional array. This is a very general datatype.A scalar is a Tensor. A scalar is just a number, like 7. A vector (also called a list, or array) is also a Tensor. Likewise, a Matrix (or lists of lists) is a Tensor. So is a cube, and so on. You can think of a Tensor as a general datatype to hold numbers.

Data Flow GraphsComputation is defined as a directed acyclic graph (DAG) to optimize an objective functionGraph is defined in high-level language (Python)Graph is compiled and optimizedGraph is executed (in parts or fully) on available low level devices (CPU, GPU)Data (tensors) flow through the graphTensorFlow can compute gradients automatically

By flow, we mean the way we compute with tensors.Tensors flow through a graph of operations, like addition and multiplication.Building up a graph like this enables lots of optimizations about how its executed to be handled behind the scenes

ArchitectureCore in C++ Front ends: Python and C++ today, community may add moreCore TensorFlow Execution System

CPUGPUAndroidiOS...C++ front endPython front end...

C++ core for high performancePrimarily for Machine Learning, built on general purpose programming model that can be used otherwise. Makes it really flexible.Runs on Android and iOS as well as in the cloud - universal!GPU support Distributed training supportMultiple language frontends, Lots of Python users, C++ for high performance integration, others being added by community

tf.contrib.learnTensorFlows high-level machine learning APIEasy to configure, train, and evaluate a variety of machine learning modelsDatasets available in tf.contrib.learn.datasetsWarning: any code in tf.contr