SlideShare una empresa de Scribd logo
1 de 19
Introduction to TensorFlow
Portland Data Science Group
Created by Andrew Ferlitsch
Community Outreach
June, 2017
What is it?
An Open Source Machine Learning Library released by Google in 2015
• Built on top of Google’s Inception v3
• Google’s most advanced image recognition system
• Convolution Neural Network (CNN)
• Available as a Python (or C++) Library
Basics – What is a Vector?
Vector = [ number, number, … ]
• A vector is an array of numbers.
• In machine learning, a vector holds the feature
values (variables) for a sample.
24 16 5 120000Vector =
Age Education
Level
Years of
Experience
Income
Basics – What is a Matrix?
Matrix = [ n ][ m ]
• A matrix is a 2-dimensional array of numbers.
• In machine learning, a matrix holds the feature
values [columns] for samples [rows] in a dataset.
24 16 5 120000
27 12 8 70000
28 18 2 135000
Matrix =
Age Education
Level
Years of
Experience
Income
What is a Tensor?
Tensor = [ n1 ][ n2 ] … [ nx ]
• A tensor is a high dimensional array.
• A tensor consists of features, where each feature
is a vector or multi-dimensional array (e.g., such
as in embedding).
A Sample
1
2
3
4
5
6
7
8
9
Features
Design & Run
• TensorFlow uses a Design & Run methodology.
Design
(symbolic representation)
Run
(bind the data and execute)
Construct training model
symbolically.
Once designed, bind (connect)
the data and execute the
model.
Data
Output
Symbolic Representation as Graph
• Tensors are inputs to tensor operations, which
output new tensors.
Variable
(tensor)
Variable
(tensor)
Op
Output
(tensor)
User constructs a symbolic representation (graph)
of how tensors flow through the network.
A Simple TensorFlow Graph
Variable
(matrix)
Variable
(matrix)
MatMul
Output
(matrix)
Two matrices
are defined as
input.
The two matrices
are dot multiplied.
The output is the dot
product of the two matrices.
TensorFlow with Python - Constants
import tensorflow as tf # import the Tensorflow library
x1 = tf.constant( 1.0 ) # define some constants
x2 = tf.constant( 2.0 )
By default, values are
floating point numbers
sess = tf.session() # connect session for execution
sess.run( [x1, x2] ) # run the graph
• Design the Graph
• Run the Graph
[ 1.0, 2.0 ]
TensorFlow with Python - Operations
import tensorflow as tf # import the tensorflow library
x1 = tf.constant( 1.0 ) # define some constants
x2 = tf.constant( 2.0 )
x3 = tf.add( x1, x2 ) # add inputs x1 and x2
sess = tf.session() # connect session for execution
sess.run( x3 ) # run the graph
• Design the Graph
• Run the Graph
3.0
When node x3 is executed, it takes the inputs x1 and x2
and applies the tensor add operation. Since these are scalar
values, they are added together to output a new scalar value.
TensorFlow with Python - Placeholders
import tensorflow as tf # import the Tensorflow library
x1 = tf.placeholder( tf.float ) # define some placeholders
x2 = tf.placeholder( tf.float )
x3 = tf.add( x1, x2 ) # add inputs x1 and x2
sess = tf.session() # connect session for execution
sess.run( x3, {x1: 1.0, x2: 2.0} ) # run the graph
• Design the Graph
• Run the Graph
3.0
When node x3 is executed, it binds the values to the inputs x1 and x2
and applies the tensor add operation. Since these are scalar
values, they are added together to output a new scalar value.
The data is bound at run time (parameterized variables).
TensorFlow with Python - Binding
import tensorflow as tf # import the Tensorflow library
x1 = tf.placeholder( tf.float, shape=[2] ) # define some placeholders
x2 = tf.placeholder( tf.float, shape=[2] )
x3 = tf.add( x1, x2 ) # add inputs x1 and x2
sess = tf.session() # connect session for execution
sess.run( x3, {x1: [1.0,2.0], x2: [3.0,4.0]} ) # run the graph
• Design the Graph
• Run the Graph
[ 4.0, 6.0 ]
Bound an array instead of a scalar value
Matrix addition
Multiple Operation TensorFlow Graph
Variable
(matrix)
Variable
(matrix)
MatAdd MatMul
The two matrices
are added.
The output is the dot
product (weight) of the addition of
the two matrices.
Output
(matrix)
Constant
(Scalar)
Weight applied to
operation.
TensorFlow with Python – Multi-Op
import tensorflow as tf # import the Tensorflow library
x1 = tf.placeholder( tf.float ) # define some placeholders
x2 = tf.placeholder( tf.float )
x3 = tf.add( x1, x2 ) # add inputs x1 and x2
x4 = tf.muliply( x3, 3.0 ) # multiply the result by 3
sess = tf.session() # connect session for execution
sess.run( x4, {x1: [1.0,2.0], x2: [3.0,4.0]} ) # run the graph
• Design the Graph
• Run the Graph
[ 12.0, 18.0 ]
Matrix multiply (weight) applied to Matrix addition
TensorFlow with Python – Operators
import tensorflow as tf # import the Tensorflow library
x1 = tf.placeholder( tf.float, shape=[2] ) # define some variables
x2 = tf.placeholder( tf.float, shape=[2] )
x3 = x1 + x2 # add inputs x1 and x2
x4 = x3 * 3.0 # multiply the result by 3
sess = tf.session() # connect session for execution
sess.run( x4, {x1: [1.0,2.0], x2: [3.0,4.0]} ) # run the graph
• Design the Graph
• Run the Graph
[ 12.0, 18.0 ]
Shortcut (or inline) operator
TensorFlow with Python – Variables
import tensorflow as tf # import the Tensorflow library
a = tf.Variable( tf.float ) # define some variables
b = tf.Variable( tf.float )
x = tf.placeholder( tf.float, shape=[4] )
yhat = a + b * x # simple linear regression
sess = tf.session() # connect session for execution
init = tf_global_variables_initializer() # initialize global variables
sess.run(init)
sess.run( yhat, {x: [1,2,3,4]} ) # run the graph
• Design the Graph
• Run the Graph
[ 0, 0.30000001, 0.60000001, 0.90000001 ]
Variables must be explicitly initialized
prior to running the graph
Run simple linear regression for x = 1, 2, 3 and 4
Example from tensorflow.org
TensorFlow with Python – Loss Function
y = tf.placeholder( tf.float ) # labels (actual values)
squares = tf.square( yhat – y ) # square of error
loss = tf.reduce_sum( squares ) # summation of squared errors
sess = tf.session() # connect session for execution
init = tf_global_variables_initializer() # initialize global variables
sess.run(init)
sess.run( loss, {x: [1,2,3,4], y:[0,-1,-2,-3]} ) # execute the graph
• Design the Graph
• Run the Graph
23.66
Sum up the squared difference of the actual
and predicted values.
Run simple linear regression for x = 1, 2, 3 and 4
with actual values 0, -1, -2 and -3.
Example from tensorflow.org
TensorFlow with Python – tf.train
optimizer = tf.train.GradientDescentOptimizer( 0.1 )
train = optimizer.minimize( loss ) # training model
sess.run(init)
for i in range(0,1000): # train over 1000 iterations
sess.run( train, {x: [1,2,3,4], y:[0,-1,-2,-3]} ) # train the model
sess.run([ b,w ] )
• Design the Graph
• Run the Graph
[0.99999082, -0.9999969]
Learning rate
Train the simple linear regression over 1000 iterations
to minimize the loss function.
Example from tensorflow.org
TensorFlow and tf.contrib.learn
• Not Covered in this tutorial
• Is a high-level wrapper built on top of Tensorflow
• For Developers – hides low-level implementation
tf.contrib.learn is a high-level TensorFlow library that
simplifies the mechanics of machine learning, including the
following:
• running training loops
• running evaluation loops
• managing data sets
• managing feeding
From tensorflow.org

Más contenido relacionado

La actualidad más candente

Machine Learning Tutorial Part - 1 | Machine Learning Tutorial For Beginners ...
Machine Learning Tutorial Part - 1 | Machine Learning Tutorial For Beginners ...Machine Learning Tutorial Part - 1 | Machine Learning Tutorial For Beginners ...
Machine Learning Tutorial Part - 1 | Machine Learning Tutorial For Beginners ...
Simplilearn
 
Decision Tree Algorithm With Example | Decision Tree In Machine Learning | Da...
Decision Tree Algorithm With Example | Decision Tree In Machine Learning | Da...Decision Tree Algorithm With Example | Decision Tree In Machine Learning | Da...
Decision Tree Algorithm With Example | Decision Tree In Machine Learning | Da...
Simplilearn
 
Support Vector Machine - How Support Vector Machine works | SVM in Machine Le...
Support Vector Machine - How Support Vector Machine works | SVM in Machine Le...Support Vector Machine - How Support Vector Machine works | SVM in Machine Le...
Support Vector Machine - How Support Vector Machine works | SVM in Machine Le...
Simplilearn
 

La actualidad más candente (20)

Multi-armed Bandits
Multi-armed BanditsMulti-armed Bandits
Multi-armed Bandits
 
Data Science - Part XII - Ridge Regression, LASSO, and Elastic Nets
Data Science - Part XII - Ridge Regression, LASSO, and Elastic NetsData Science - Part XII - Ridge Regression, LASSO, and Elastic Nets
Data Science - Part XII - Ridge Regression, LASSO, and Elastic Nets
 
Analysis Of Attribute Revelance
Analysis Of Attribute RevelanceAnalysis Of Attribute Revelance
Analysis Of Attribute Revelance
 
Machine Learning Tutorial Part - 1 | Machine Learning Tutorial For Beginners ...
Machine Learning Tutorial Part - 1 | Machine Learning Tutorial For Beginners ...Machine Learning Tutorial Part - 1 | Machine Learning Tutorial For Beginners ...
Machine Learning Tutorial Part - 1 | Machine Learning Tutorial For Beginners ...
 
Variational Autoencoder
Variational AutoencoderVariational Autoencoder
Variational Autoencoder
 
Neural collaborative filtering-발표
Neural collaborative filtering-발표Neural collaborative filtering-발표
Neural collaborative filtering-발표
 
Machine learning with ADA Boost
Machine learning with ADA BoostMachine learning with ADA Boost
Machine learning with ADA Boost
 
L2. Evaluating Machine Learning Algorithms I
L2. Evaluating Machine Learning Algorithms IL2. Evaluating Machine Learning Algorithms I
L2. Evaluating Machine Learning Algorithms I
 
Regularization in deep learning
Regularization in deep learningRegularization in deep learning
Regularization in deep learning
 
Decision Tree Algorithm With Example | Decision Tree In Machine Learning | Da...
Decision Tree Algorithm With Example | Decision Tree In Machine Learning | Da...Decision Tree Algorithm With Example | Decision Tree In Machine Learning | Da...
Decision Tree Algorithm With Example | Decision Tree In Machine Learning | Da...
 
Support vector machines (svm)
Support vector machines (svm)Support vector machines (svm)
Support vector machines (svm)
 
Logistic regression
Logistic regressionLogistic regression
Logistic regression
 
Data Science - Part IX - Support Vector Machine
Data Science - Part IX -  Support Vector MachineData Science - Part IX -  Support Vector Machine
Data Science - Part IX - Support Vector Machine
 
Linear models for classification
Linear models for classificationLinear models for classification
Linear models for classification
 
Support Vector Machine - How Support Vector Machine works | SVM in Machine Le...
Support Vector Machine - How Support Vector Machine works | SVM in Machine Le...Support Vector Machine - How Support Vector Machine works | SVM in Machine Le...
Support Vector Machine - How Support Vector Machine works | SVM in Machine Le...
 
Optimization/Gradient Descent
Optimization/Gradient DescentOptimization/Gradient Descent
Optimization/Gradient Descent
 
Credit card fraud detection
Credit card fraud detectionCredit card fraud detection
Credit card fraud detection
 
Ensemble methods in machine learning
Ensemble methods in machine learningEnsemble methods in machine learning
Ensemble methods in machine learning
 
K means clustering
K means clusteringK means clustering
K means clustering
 
Attention Mechanism in Language Understanding and its Applications
Attention Mechanism in Language Understanding and its ApplicationsAttention Mechanism in Language Understanding and its Applications
Attention Mechanism in Language Understanding and its Applications
 

Similar a Machine Learning - Introduction to Tensorflow

Similar a Machine Learning - Introduction to Tensorflow (20)

Advanced Spark and TensorFlow Meetup May 26, 2016
Advanced Spark and TensorFlow Meetup May 26, 2016Advanced Spark and TensorFlow Meetup May 26, 2016
Advanced Spark and TensorFlow Meetup May 26, 2016
 
TensorFlow Tutorial.pdf
TensorFlow Tutorial.pdfTensorFlow Tutorial.pdf
TensorFlow Tutorial.pdf
 
Introduction to TensorFlow 2
Introduction to TensorFlow 2Introduction to TensorFlow 2
Introduction to TensorFlow 2
 
Introduction to TensorFlow 2 and Keras
Introduction to TensorFlow 2 and KerasIntroduction to TensorFlow 2 and Keras
Introduction to TensorFlow 2 and Keras
 
TensorFlow for IITians
TensorFlow for IITiansTensorFlow for IITians
TensorFlow for IITians
 
TensorFlow example for AI Ukraine2016
TensorFlow example  for AI Ukraine2016TensorFlow example  for AI Ukraine2016
TensorFlow example for AI Ukraine2016
 
Google TensorFlow Tutorial
Google TensorFlow TutorialGoogle TensorFlow Tutorial
Google TensorFlow Tutorial
 
Deep Learning and TensorFlow
Deep Learning and TensorFlowDeep Learning and TensorFlow
Deep Learning and TensorFlow
 
Introduction to TensorFlow
Introduction to TensorFlowIntroduction to TensorFlow
Introduction to TensorFlow
 
Deep Learning, Scala, and Spark
Deep Learning, Scala, and SparkDeep Learning, Scala, and Spark
Deep Learning, Scala, and Spark
 
Introduction To Using TensorFlow & Deep Learning
Introduction To Using TensorFlow & Deep LearningIntroduction To Using TensorFlow & Deep Learning
Introduction To Using TensorFlow & Deep Learning
 
Tensorflow - Intro (2017)
Tensorflow - Intro (2017)Tensorflow - Intro (2017)
Tensorflow - Intro (2017)
 
Working with tf.data (TF 2)
Working with tf.data (TF 2)Working with tf.data (TF 2)
Working with tf.data (TF 2)
 
NTU ML TENSORFLOW
NTU ML TENSORFLOWNTU ML TENSORFLOW
NTU ML TENSORFLOW
 
Meetup tensorframes
Meetup tensorframesMeetup tensorframes
Meetup tensorframes
 
A Tour of Tensorflow's APIs
A Tour of Tensorflow's APIsA Tour of Tensorflow's APIs
A Tour of Tensorflow's APIs
 
NTC_Tensor flow 深度學習快速上手班_Part1 -機器學習
NTC_Tensor flow 深度學習快速上手班_Part1 -機器學習NTC_Tensor flow 深度學習快速上手班_Part1 -機器學習
NTC_Tensor flow 深度學習快速上手班_Part1 -機器學習
 
TensorFlow 深度學習快速上手班--機器學習
TensorFlow 深度學習快速上手班--機器學習TensorFlow 深度學習快速上手班--機器學習
TensorFlow 深度學習快速上手班--機器學習
 
Theano vs TensorFlow | Edureka
Theano vs TensorFlow | EdurekaTheano vs TensorFlow | Edureka
Theano vs TensorFlow | Edureka
 
TensorFlow in Your Browser
TensorFlow in Your BrowserTensorFlow in Your Browser
TensorFlow in Your Browser
 

Más de Andrew Ferlitsch

Más de Andrew Ferlitsch (20)

AI - Intelligent Agents
AI - Intelligent AgentsAI - Intelligent Agents
AI - Intelligent Agents
 
Pareto Principle Applied to QA
Pareto Principle Applied to QAPareto Principle Applied to QA
Pareto Principle Applied to QA
 
Whiteboarding Coding Challenges in Python
Whiteboarding Coding Challenges in PythonWhiteboarding Coding Challenges in Python
Whiteboarding Coding Challenges in Python
 
Object Oriented Programming Principles
Object Oriented Programming PrinciplesObject Oriented Programming Principles
Object Oriented Programming Principles
 
Python - OOP Programming
Python - OOP ProgrammingPython - OOP Programming
Python - OOP Programming
 
Python - Installing and Using Python and Jupyter Notepad
Python - Installing and Using Python and Jupyter NotepadPython - Installing and Using Python and Jupyter Notepad
Python - Installing and Using Python and Jupyter Notepad
 
Natural Language Processing - Groupings (Associations) Generation
Natural Language Processing - Groupings (Associations) GenerationNatural Language Processing - Groupings (Associations) Generation
Natural Language Processing - Groupings (Associations) Generation
 
Natural Language Provessing - Handling Narrarive Fields in Datasets for Class...
Natural Language Provessing - Handling Narrarive Fields in Datasets for Class...Natural Language Provessing - Handling Narrarive Fields in Datasets for Class...
Natural Language Provessing - Handling Narrarive Fields in Datasets for Class...
 
Machine Learning - Introduction to Recurrent Neural Networks
Machine Learning - Introduction to Recurrent Neural NetworksMachine Learning - Introduction to Recurrent Neural Networks
Machine Learning - Introduction to Recurrent Neural Networks
 
Machine Learning - Introduction to Convolutional Neural Networks
Machine Learning - Introduction to Convolutional Neural NetworksMachine Learning - Introduction to Convolutional Neural Networks
Machine Learning - Introduction to Convolutional Neural Networks
 
Machine Learning - Introduction to Neural Networks
Machine Learning - Introduction to Neural NetworksMachine Learning - Introduction to Neural Networks
Machine Learning - Introduction to Neural Networks
 
Python - Numpy/Pandas/Matplot Machine Learning Libraries
Python - Numpy/Pandas/Matplot Machine Learning LibrariesPython - Numpy/Pandas/Matplot Machine Learning Libraries
Python - Numpy/Pandas/Matplot Machine Learning Libraries
 
Machine Learning - Accuracy and Confusion Matrix
Machine Learning - Accuracy and Confusion MatrixMachine Learning - Accuracy and Confusion Matrix
Machine Learning - Accuracy and Confusion Matrix
 
Machine Learning - Ensemble Methods
Machine Learning - Ensemble MethodsMachine Learning - Ensemble Methods
Machine Learning - Ensemble Methods
 
ML - Multiple Linear Regression
ML - Multiple Linear RegressionML - Multiple Linear Regression
ML - Multiple Linear Regression
 
ML - Simple Linear Regression
ML - Simple Linear RegressionML - Simple Linear Regression
ML - Simple Linear Regression
 
Machine Learning - Dummy Variable Conversion
Machine Learning - Dummy Variable ConversionMachine Learning - Dummy Variable Conversion
Machine Learning - Dummy Variable Conversion
 
Machine Learning - Splitting Datasets
Machine Learning - Splitting DatasetsMachine Learning - Splitting Datasets
Machine Learning - Splitting Datasets
 
Machine Learning - Dataset Preparation
Machine Learning - Dataset PreparationMachine Learning - Dataset Preparation
Machine Learning - Dataset Preparation
 
Introduction to Machine Learning
Introduction to Machine LearningIntroduction to Machine Learning
Introduction to Machine Learning
 

Último

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 

Último (20)

DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 

Machine Learning - Introduction to Tensorflow

  • 1. Introduction to TensorFlow Portland Data Science Group Created by Andrew Ferlitsch Community Outreach June, 2017
  • 2. What is it? An Open Source Machine Learning Library released by Google in 2015 • Built on top of Google’s Inception v3 • Google’s most advanced image recognition system • Convolution Neural Network (CNN) • Available as a Python (or C++) Library
  • 3. Basics – What is a Vector? Vector = [ number, number, … ] • A vector is an array of numbers. • In machine learning, a vector holds the feature values (variables) for a sample. 24 16 5 120000Vector = Age Education Level Years of Experience Income
  • 4. Basics – What is a Matrix? Matrix = [ n ][ m ] • A matrix is a 2-dimensional array of numbers. • In machine learning, a matrix holds the feature values [columns] for samples [rows] in a dataset. 24 16 5 120000 27 12 8 70000 28 18 2 135000 Matrix = Age Education Level Years of Experience Income
  • 5. What is a Tensor? Tensor = [ n1 ][ n2 ] … [ nx ] • A tensor is a high dimensional array. • A tensor consists of features, where each feature is a vector or multi-dimensional array (e.g., such as in embedding). A Sample 1 2 3 4 5 6 7 8 9 Features
  • 6. Design & Run • TensorFlow uses a Design & Run methodology. Design (symbolic representation) Run (bind the data and execute) Construct training model symbolically. Once designed, bind (connect) the data and execute the model. Data Output
  • 7. Symbolic Representation as Graph • Tensors are inputs to tensor operations, which output new tensors. Variable (tensor) Variable (tensor) Op Output (tensor) User constructs a symbolic representation (graph) of how tensors flow through the network.
  • 8. A Simple TensorFlow Graph Variable (matrix) Variable (matrix) MatMul Output (matrix) Two matrices are defined as input. The two matrices are dot multiplied. The output is the dot product of the two matrices.
  • 9. TensorFlow with Python - Constants import tensorflow as tf # import the Tensorflow library x1 = tf.constant( 1.0 ) # define some constants x2 = tf.constant( 2.0 ) By default, values are floating point numbers sess = tf.session() # connect session for execution sess.run( [x1, x2] ) # run the graph • Design the Graph • Run the Graph [ 1.0, 2.0 ]
  • 10. TensorFlow with Python - Operations import tensorflow as tf # import the tensorflow library x1 = tf.constant( 1.0 ) # define some constants x2 = tf.constant( 2.0 ) x3 = tf.add( x1, x2 ) # add inputs x1 and x2 sess = tf.session() # connect session for execution sess.run( x3 ) # run the graph • Design the Graph • Run the Graph 3.0 When node x3 is executed, it takes the inputs x1 and x2 and applies the tensor add operation. Since these are scalar values, they are added together to output a new scalar value.
  • 11. TensorFlow with Python - Placeholders import tensorflow as tf # import the Tensorflow library x1 = tf.placeholder( tf.float ) # define some placeholders x2 = tf.placeholder( tf.float ) x3 = tf.add( x1, x2 ) # add inputs x1 and x2 sess = tf.session() # connect session for execution sess.run( x3, {x1: 1.0, x2: 2.0} ) # run the graph • Design the Graph • Run the Graph 3.0 When node x3 is executed, it binds the values to the inputs x1 and x2 and applies the tensor add operation. Since these are scalar values, they are added together to output a new scalar value. The data is bound at run time (parameterized variables).
  • 12. TensorFlow with Python - Binding import tensorflow as tf # import the Tensorflow library x1 = tf.placeholder( tf.float, shape=[2] ) # define some placeholders x2 = tf.placeholder( tf.float, shape=[2] ) x3 = tf.add( x1, x2 ) # add inputs x1 and x2 sess = tf.session() # connect session for execution sess.run( x3, {x1: [1.0,2.0], x2: [3.0,4.0]} ) # run the graph • Design the Graph • Run the Graph [ 4.0, 6.0 ] Bound an array instead of a scalar value Matrix addition
  • 13. Multiple Operation TensorFlow Graph Variable (matrix) Variable (matrix) MatAdd MatMul The two matrices are added. The output is the dot product (weight) of the addition of the two matrices. Output (matrix) Constant (Scalar) Weight applied to operation.
  • 14. TensorFlow with Python – Multi-Op import tensorflow as tf # import the Tensorflow library x1 = tf.placeholder( tf.float ) # define some placeholders x2 = tf.placeholder( tf.float ) x3 = tf.add( x1, x2 ) # add inputs x1 and x2 x4 = tf.muliply( x3, 3.0 ) # multiply the result by 3 sess = tf.session() # connect session for execution sess.run( x4, {x1: [1.0,2.0], x2: [3.0,4.0]} ) # run the graph • Design the Graph • Run the Graph [ 12.0, 18.0 ] Matrix multiply (weight) applied to Matrix addition
  • 15. TensorFlow with Python – Operators import tensorflow as tf # import the Tensorflow library x1 = tf.placeholder( tf.float, shape=[2] ) # define some variables x2 = tf.placeholder( tf.float, shape=[2] ) x3 = x1 + x2 # add inputs x1 and x2 x4 = x3 * 3.0 # multiply the result by 3 sess = tf.session() # connect session for execution sess.run( x4, {x1: [1.0,2.0], x2: [3.0,4.0]} ) # run the graph • Design the Graph • Run the Graph [ 12.0, 18.0 ] Shortcut (or inline) operator
  • 16. TensorFlow with Python – Variables import tensorflow as tf # import the Tensorflow library a = tf.Variable( tf.float ) # define some variables b = tf.Variable( tf.float ) x = tf.placeholder( tf.float, shape=[4] ) yhat = a + b * x # simple linear regression sess = tf.session() # connect session for execution init = tf_global_variables_initializer() # initialize global variables sess.run(init) sess.run( yhat, {x: [1,2,3,4]} ) # run the graph • Design the Graph • Run the Graph [ 0, 0.30000001, 0.60000001, 0.90000001 ] Variables must be explicitly initialized prior to running the graph Run simple linear regression for x = 1, 2, 3 and 4 Example from tensorflow.org
  • 17. TensorFlow with Python – Loss Function y = tf.placeholder( tf.float ) # labels (actual values) squares = tf.square( yhat – y ) # square of error loss = tf.reduce_sum( squares ) # summation of squared errors sess = tf.session() # connect session for execution init = tf_global_variables_initializer() # initialize global variables sess.run(init) sess.run( loss, {x: [1,2,3,4], y:[0,-1,-2,-3]} ) # execute the graph • Design the Graph • Run the Graph 23.66 Sum up the squared difference of the actual and predicted values. Run simple linear regression for x = 1, 2, 3 and 4 with actual values 0, -1, -2 and -3. Example from tensorflow.org
  • 18. TensorFlow with Python – tf.train optimizer = tf.train.GradientDescentOptimizer( 0.1 ) train = optimizer.minimize( loss ) # training model sess.run(init) for i in range(0,1000): # train over 1000 iterations sess.run( train, {x: [1,2,3,4], y:[0,-1,-2,-3]} ) # train the model sess.run([ b,w ] ) • Design the Graph • Run the Graph [0.99999082, -0.9999969] Learning rate Train the simple linear regression over 1000 iterations to minimize the loss function. Example from tensorflow.org
  • 19. TensorFlow and tf.contrib.learn • Not Covered in this tutorial • Is a high-level wrapper built on top of Tensorflow • For Developers – hides low-level implementation tf.contrib.learn is a high-level TensorFlow library that simplifies the mechanics of machine learning, including the following: • running training loops • running evaluation loops • managing data sets • managing feeding From tensorflow.org