SlideShare a Scribd company logo
TENSORFLOW
Building a Graph
■ Start with ops that do not need any input (source ops), Constant
import tensorflow as tf
matrix1 = tf.constant([[3,3]])
matrix2 = tf.constant([[6],[6]])
product=tf.matmul(matrix1,matrix2)
print(product) # O/pTensor("MatMul_1:0", shape=(1, 1), dtype=int32)
sess=tf.Session()
result=sess.run(product)
print(result) #O/P [[36]]
sess.close()
■ You will see difference in o/p in both the print statement. First statement just tells us about the Product
tensor property . Second statement is under a session. Graph gets executed thus the print shows the
value of the tensor.
matrix1 matrix2
OP:
MatMul
result
Tensors
■ As we have seen how a basic tensorflow works, lets dig deeper onTensors
■ n-dimensional array
■ 0-d tensor: scalar (number) ,1-d tensor: vector 2-d, tensor: matrix etc etc.
■ tf.Variable is a class, but tf.constant is an op
Graphs
■ It represents computations.
import tensorflow as tf
a = tf.add(3, 5)
■ TF automatically names the nodes when you don’t explicitly name
them.
x = 3
y = 5
■ Edge: Tensors ie data
■ Node: operators, variables, and constants
■ If we simply print(a), output will be tensor object and not 8.To
fetch the value of it in a, we need to run the session.
■ The session will look at the graph, trying to think: how can I get
the value of a, then it computes all the nodes that leads to a.
x
y
Ad
d
x
y
a
Interpret
ation
3
5
Add
Session
x
y
a
3
5
Add
8
But why only graphs?
■ Save computation (only run subgraphs that lead to the values you want to fetch)
■ Break computation into small, differential pieces to facilitates auto-differentiation
■ Facilitate distributed computation, spread the work across multiple CPUs, GPUs, or
devices
■ Many common machine learning models are commonly taught and visualized as
directed graphs already
Sessions
■ A Session object encapsulates the environment in which
Operation objects are executed, andTensor objects are
evaluated.
x = 2
y = 3
add_op = tf.add(x, y)
mul_op = tf.mul(x, y)
useless = tf.mul(x, add_op)
pow_op = tf.pow(add_op, mul_op)
with tf.Session() as sess:
z = sess.run(pow_op)
Because we only want the value of pow_op and pow_op doesn’t
depend on useless, session won’t compute value of useless → save
computation
Operations
Its very similar to numpy. In terms of data type,TensorFlow integrates seamlessly with NumPy. If the
requested fetch is aTensor , then the output of will be a NumPy ndarray. Beware when using NumPy arrays
because NumPy andTensorFlow might become not so compatible in the future!
Tensor Board
■ Help a lot when you build complicated models
■ To useTensor Board
I. Go to terminal
II. run: $ python [yourprogram].py
III. $ tensorboard --logdir="./graphs" --port 6006
IV. Then open your browser and go to:
http://localhost:6006/
Example ofTensor Board
import tensorflow as tf
matrix1 = tf.constant([[3,3]])
matrix2 = tf.constant([[6],[6]])
product=tf.matmul(matrix1,matrix2)
print(product)
sess=tf.Session()
writer = tf.summary.FileWriter("./graphs", sess.graph)
result=sess.run(product)
print(result)
sess.close()
I don’t like const, How to explicitly name
them
import tensorflow as tf
matrix1 = tf.constant([[3,3]], name="a")
matrix2 = tf.constant([[6],[6]], name="b")
product=tf.matmul(matrix1,matrix2)
print(product)
sess=tf.Session()
writer = tf.summary.FileWriter("./graphs", sess.graph)
result=sess.run(product)
print(result)
sess.close()
Key Notes
■ Graph execution is done across all available compute resources, such as CPU or GPU. If
you have GPU,TensorFlow uses your first GPU
■ TensorFlow separates definition of computations from their execution
■ Only use constants for primitive types. Constants are stored in graph’s definition.This makes
loading graphs expensive when constants are big. Use variables or readers for more data that
requires more memory.
Test yourTensor flow knowledge
Graphs Sessions Tensors Variables Nodes

More Related Content

PDF
Your first TensorFlow programming with Jupyter
PDF
Use the Matplotlib, Luke @ PyCon Taiwan 2012
PDF
【AI実装4】TensorFlowのプログラムを読む2 非線形回帰
PDF
Machine Learning Basics for Web Application Developers
PPT
Dynamic allocation
PDF
R programming lab 1 - jupyter notebook
PDF
Py lecture5 python plots
PPTX
Lecture 3.2 bt
Your first TensorFlow programming with Jupyter
Use the Matplotlib, Luke @ PyCon Taiwan 2012
【AI実装4】TensorFlowのプログラムを読む2 非線形回帰
Machine Learning Basics for Web Application Developers
Dynamic allocation
R programming lab 1 - jupyter notebook
Py lecture5 python plots
Lecture 3.2 bt

What's hot (20)

PDF
Tensorflowで五目並べの思考アルゴリズムを作る
PDF
Matlab plotting
PDF
The TensorFlow dance craze
PDF
Matplotlib 簡介與使用
PDF
Demo1 use numpy
PPTX
#OOP_D_ITS - 3rd - Pointer And References
PDF
Theano vs TensorFlow | Edureka
PDF
Actividad 3 - Unidad 2 - Niveyro - Alarcon
PDF
機械学習によるデータ分析 実践編
PDF
The matplotlib Library
PPTX
Simone Scardapane - Bring your neural networks to the browser with TF.js! - C...
PPTX
Malloc() and calloc() in c
PPTX
Tensorflow windows installation
PDF
Introduction to programming class 11 exercises
PPT
computer notes - Data Structures - 38
PDF
Matlab integration
PPT
PPTX
Lecture 1 mte 407
PPTX
Lecture 1 mte 407
PPTX
Dynamic memory allocation
Tensorflowで五目並べの思考アルゴリズムを作る
Matlab plotting
The TensorFlow dance craze
Matplotlib 簡介與使用
Demo1 use numpy
#OOP_D_ITS - 3rd - Pointer And References
Theano vs TensorFlow | Edureka
Actividad 3 - Unidad 2 - Niveyro - Alarcon
機械学習によるデータ分析 実践編
The matplotlib Library
Simone Scardapane - Bring your neural networks to the browser with TF.js! - C...
Malloc() and calloc() in c
Tensorflow windows installation
Introduction to programming class 11 exercises
computer notes - Data Structures - 38
Matlab integration
Lecture 1 mte 407
Lecture 1 mte 407
Dynamic memory allocation
Ad

Similar to Tensorflow basics (20)

PPTX
Introduction To TensorFlow | Deep Learning Using TensorFlow | CloudxLab
PDF
Advanced Spark and TensorFlow Meetup May 26, 2016
PDF
Introduction to TensorFlow, by Machine Learning at Berkeley
PPTX
Machine Learning - Introduction to Tensorflow
PPTX
Tensorflow in practice by Engineer - donghwi cha
PDF
TensorFlow example for AI Ukraine2016
PDF
TensorFlow Tutorial.pdf
PDF
TensorFlow Tutorial | Deep Learning Using TensorFlow | TensorFlow Tutorial Py...
PPTX
Tensorflow 101 @ Machine Learning Innovation Summit SF June 6, 2017
PPTX
Introduction to Deep Learning and TensorFlow
PPTX
Intro to Deep Learning, TensorFlow, and tensorflow.js
PDF
Pytorch for tf_developers
PPTX
H2 o berkeleydltf
PPTX
TensorFlow in Your Browser
PPTX
Deep Learning and TensorFlow
PPTX
Deep Learning in Your Browser
PPTX
Introduction to Deep Learning, Keras, and Tensorflow
PDF
Introduction to Deep Learning, Keras, and TensorFlow
PDF
Introduction To TensorFlow | Deep Learning Using TensorFlow | TensorFlow Tuto...
PDF
From NumPy to PyTorch
Introduction To TensorFlow | Deep Learning Using TensorFlow | CloudxLab
Advanced Spark and TensorFlow Meetup May 26, 2016
Introduction to TensorFlow, by Machine Learning at Berkeley
Machine Learning - Introduction to Tensorflow
Tensorflow in practice by Engineer - donghwi cha
TensorFlow example for AI Ukraine2016
TensorFlow Tutorial.pdf
TensorFlow Tutorial | Deep Learning Using TensorFlow | TensorFlow Tutorial Py...
Tensorflow 101 @ Machine Learning Innovation Summit SF June 6, 2017
Introduction to Deep Learning and TensorFlow
Intro to Deep Learning, TensorFlow, and tensorflow.js
Pytorch for tf_developers
H2 o berkeleydltf
TensorFlow in Your Browser
Deep Learning and TensorFlow
Deep Learning in Your Browser
Introduction to Deep Learning, Keras, and Tensorflow
Introduction to Deep Learning, Keras, and TensorFlow
Introduction To TensorFlow | Deep Learning Using TensorFlow | TensorFlow Tuto...
From NumPy to PyTorch
Ad

Recently uploaded (20)

PDF
22.Patil - Early prediction of Alzheimer’s disease using convolutional neural...
PPTX
mbdjdhjjodule 5-1 rhfhhfjtjjhafbrhfnfbbfnb
PPTX
1_Introduction to advance data techniques.pptx
PPT
Miokarditis (Inflamasi pada Otot Jantung)
PPTX
The THESIS FINAL-DEFENSE-PRESENTATION.pptx
PPTX
Qualitative Qantitative and Mixed Methods.pptx
PPTX
Business Acumen Training GuidePresentation.pptx
PPTX
advance b rammar.pptxfdgdfgdfsgdfgsdgfdfgdfgsdfgdfgdfg
PDF
Recruitment and Placement PPT.pdfbjfibjdfbjfobj
PDF
Fluorescence-microscope_Botany_detailed content
PPT
Reliability_Chapter_ presentation 1221.5784
PDF
BF and FI - Blockchain, fintech and Financial Innovation Lesson 2.pdf
PDF
Clinical guidelines as a resource for EBP(1).pdf
PPTX
Introduction-to-Cloud-ComputingFinal.pptx
PPTX
DISORDERS OF THE LIVER, GALLBLADDER AND PANCREASE (1).pptx
PDF
Business Analytics and business intelligence.pdf
PDF
Foundation of Data Science unit number two notes
PDF
168300704-gasification-ppt.pdfhghhhsjsjhsuxush
PPTX
Computer network topology notes for revision
PPTX
Introduction to Firewall Analytics - Interfirewall and Transfirewall.pptx
22.Patil - Early prediction of Alzheimer’s disease using convolutional neural...
mbdjdhjjodule 5-1 rhfhhfjtjjhafbrhfnfbbfnb
1_Introduction to advance data techniques.pptx
Miokarditis (Inflamasi pada Otot Jantung)
The THESIS FINAL-DEFENSE-PRESENTATION.pptx
Qualitative Qantitative and Mixed Methods.pptx
Business Acumen Training GuidePresentation.pptx
advance b rammar.pptxfdgdfgdfsgdfgsdgfdfgdfgsdfgdfgdfg
Recruitment and Placement PPT.pdfbjfibjdfbjfobj
Fluorescence-microscope_Botany_detailed content
Reliability_Chapter_ presentation 1221.5784
BF and FI - Blockchain, fintech and Financial Innovation Lesson 2.pdf
Clinical guidelines as a resource for EBP(1).pdf
Introduction-to-Cloud-ComputingFinal.pptx
DISORDERS OF THE LIVER, GALLBLADDER AND PANCREASE (1).pptx
Business Analytics and business intelligence.pdf
Foundation of Data Science unit number two notes
168300704-gasification-ppt.pdfhghhhsjsjhsuxush
Computer network topology notes for revision
Introduction to Firewall Analytics - Interfirewall and Transfirewall.pptx

Tensorflow basics

  • 2. Building a Graph ■ Start with ops that do not need any input (source ops), Constant import tensorflow as tf matrix1 = tf.constant([[3,3]]) matrix2 = tf.constant([[6],[6]]) product=tf.matmul(matrix1,matrix2) print(product) # O/pTensor("MatMul_1:0", shape=(1, 1), dtype=int32) sess=tf.Session() result=sess.run(product) print(result) #O/P [[36]] sess.close() ■ You will see difference in o/p in both the print statement. First statement just tells us about the Product tensor property . Second statement is under a session. Graph gets executed thus the print shows the value of the tensor. matrix1 matrix2 OP: MatMul result
  • 3. Tensors ■ As we have seen how a basic tensorflow works, lets dig deeper onTensors ■ n-dimensional array ■ 0-d tensor: scalar (number) ,1-d tensor: vector 2-d, tensor: matrix etc etc. ■ tf.Variable is a class, but tf.constant is an op
  • 4. Graphs ■ It represents computations. import tensorflow as tf a = tf.add(3, 5) ■ TF automatically names the nodes when you don’t explicitly name them. x = 3 y = 5 ■ Edge: Tensors ie data ■ Node: operators, variables, and constants ■ If we simply print(a), output will be tensor object and not 8.To fetch the value of it in a, we need to run the session. ■ The session will look at the graph, trying to think: how can I get the value of a, then it computes all the nodes that leads to a. x y Ad d x y a Interpret ation 3 5 Add Session x y a 3 5 Add 8
  • 5. But why only graphs? ■ Save computation (only run subgraphs that lead to the values you want to fetch) ■ Break computation into small, differential pieces to facilitates auto-differentiation ■ Facilitate distributed computation, spread the work across multiple CPUs, GPUs, or devices ■ Many common machine learning models are commonly taught and visualized as directed graphs already
  • 6. Sessions ■ A Session object encapsulates the environment in which Operation objects are executed, andTensor objects are evaluated. x = 2 y = 3 add_op = tf.add(x, y) mul_op = tf.mul(x, y) useless = tf.mul(x, add_op) pow_op = tf.pow(add_op, mul_op) with tf.Session() as sess: z = sess.run(pow_op) Because we only want the value of pow_op and pow_op doesn’t depend on useless, session won’t compute value of useless → save computation
  • 7. Operations Its very similar to numpy. In terms of data type,TensorFlow integrates seamlessly with NumPy. If the requested fetch is aTensor , then the output of will be a NumPy ndarray. Beware when using NumPy arrays because NumPy andTensorFlow might become not so compatible in the future!
  • 8. Tensor Board ■ Help a lot when you build complicated models ■ To useTensor Board I. Go to terminal II. run: $ python [yourprogram].py III. $ tensorboard --logdir="./graphs" --port 6006 IV. Then open your browser and go to: http://localhost:6006/
  • 9. Example ofTensor Board import tensorflow as tf matrix1 = tf.constant([[3,3]]) matrix2 = tf.constant([[6],[6]]) product=tf.matmul(matrix1,matrix2) print(product) sess=tf.Session() writer = tf.summary.FileWriter("./graphs", sess.graph) result=sess.run(product) print(result) sess.close()
  • 10. I don’t like const, How to explicitly name them import tensorflow as tf matrix1 = tf.constant([[3,3]], name="a") matrix2 = tf.constant([[6],[6]], name="b") product=tf.matmul(matrix1,matrix2) print(product) sess=tf.Session() writer = tf.summary.FileWriter("./graphs", sess.graph) result=sess.run(product) print(result) sess.close()
  • 11. Key Notes ■ Graph execution is done across all available compute resources, such as CPU or GPU. If you have GPU,TensorFlow uses your first GPU ■ TensorFlow separates definition of computations from their execution
  • 12. ■ Only use constants for primitive types. Constants are stored in graph’s definition.This makes loading graphs expensive when constants are big. Use variables or readers for more data that requires more memory.
  • 13. Test yourTensor flow knowledge Graphs Sessions Tensors Variables Nodes