CoreTensorFlow
LakLakshmanan
Learnhowto... Writelazyevaluationand imperativeprograms
Learnhowto... Writelazyevaluationand imperativeprograms Workwithgraphs,sessions, andvariables
Learnhowto... Writelazyevaluationand imperativeprograms Workwithgraphs,sessions, andvariables VisualizeTensorFlowgraphs
Learnhowto... Writelazyevaluationand imperativeprograms Workwithgraphs,sessions, andvariables VisualizeTensorFlowgraphs DebugTensorFlowprograms
WhatisTensorFlow
LakLakshmanan
TensorFlowisan open-source high-performancelibrary high-performance library fornumerical for numerical computationthatuses computation thatuses directedgraphs
Nodesrepresent mathematicaloperations
Nodesrepresent mathematicaloperations
Nodesrepresent mathematicaloperations
Edgesrepresent arraysofdata
Nodesrepresent mathematicaloperations
Nodesrepresent mathematicaloperations
Nodesrepresent mathematicaloperations
Edgesrepresent arraysofdata
AtensorisanN-dimensionalarrayofdata
Rank0 Tensor scalar
AtensorisanN-dimensionalarrayofdata
Rank0 Tensor scalar
Rank1 Tensor vector
AtensorisanN-dimensionalarrayofdata
Rank0 Tensor scalar
Rank1 Tensor vector
Rank2 Tensor matrix
AtensorisanN-dimensionalarrayofdata
Rank0 Tensor scalar
Rank1 Tensor vector
Rank2 Tensor matrix
Rank3 Tensor
Rank4 Tensor
BenefitsofaDirectedGraph
LakLakshmanan
TensorFlowgraphsareportablebetween differentdevices
CPUs
GPUs
TensorFlowLiteprovideson-deviceinferenceofML modelsonmobiledevicesandisavailablefora varietyofhardware
Trainoncloud
TensorFlowLiteprovideson-deviceinferenceofML modelsonmobiledevicesandisavailablefora varietyofhardware
Trainoncloud
RuninferenceoniOS, Android,RaspberryPi,etc.
TensorFlowevensupports federatedlearning
https://research.googleblog.com/2017/04/federated-lear ning-collaborative.html
TensorFlowevensupports federatedlearning
https://research.googleblog.com/2017/04/federated-lear ning-collaborative.html
TensorFlowevensupports federatedlearning
https://research.googleblog.com/2017/04/federated-lear ning-collaborative.html
TensorFlowevensupports federatedlearning
https://research.googleblog.com/2017/04/federated-lear ning-collaborative.html
TensorFlowispopularamongbothdeeplearning researchersandmachinelearningengineers
TensorFlowispopularamongbothdeeplearning researchersandmachinelearningengineers
#1repositoryfor“machinelearning”categoryonGitHub
TensorFlowispopularamongbothdeeplearning researchersandmachinelearningengineers c e r u s o e n p o
#1repositoryfor“machinelearning”categoryonGitHub
TensorFlowAPIhierarchy
LakLakshmanan
TensorFlowtoolkithierarchy RunTFat scalewith CMLE
tf.estimator
High-levelAPIfordistributed training
tf.layers,tf.losses,tf.metrics
Componentsusefulwhen buildingcustomNNmodels
CoreTensorFlow(Python)
PythonAPIgivesyoufull control
CoreTensorFlow(C++)
C++APIisquitelowlevel
CPU
GPU
TPU
Android
TFrunsondifferenthardware
e n i g n E L M d u o l C
TensorFlowtoolkithierarchy
tf.estimator
High-levelAPIfordistributed training
tf.layers,tf.losses,tf.metrics
Componentsusefulwhen buildingcustomNNmodels
CoreTensorFlow(Python)
PythonAPIgivesyoufull control
CoreTensorFlow(C++)
C++APIisquitelowlevel
CPU
GPU
TPU
Android
TFrunsondifferenthardware
e n i g n E L M d u o l C
TensorFlowtoolkithierarchy
tf.estimator
High-levelAPIfordistributed training
tf.layers,tf.losses,tf.metrics
Componentsusefulwhen buildingcustomNNmodels
CoreTensorFlow(Python)
PythonAPIgivesyoufull control
CoreTensorFlow(C++)
C++APIisquitelowlevel
CPU
GPU
TPU
Android
TFrunsondifferenthardware
e n i g n E L M d u o l C
TensorFlowtoolkithierarchy
tf.estimator
High-levelAPIfordistributed training
tf.layers,tf.losses,tf.metrics
Componentsusefulwhen buildingcustomNNmodels
CoreTensorFlow(Python)
PythonAPIgivesyoufull control
CoreTensorFlow(C++)
C++APIisquitelowlevel
CPU
GPU
TPU
Android
TFrunsondifferenthardware
e n i g n E L M d u o l C
TensorFlowtoolkithierarchy
tf.estimator
High-levelAPIfordistributed training
tf.layers,tf.losses,tf.metrics
Componentsusefulwhen buildingcustomNNmodels
CoreTensorFlow(Python)
PythonAPIgivesyoufull control
CoreTensorFlow(C++)
C++APIisquitelowlevel
CPU
GPU
TPU
Android
TFrunsondifferenthardware
e n i g n E L M d u o l C
TensorFlowtoolkithierarchy
tf.estimator
High-levelAPIfordistributed training
tf.layers,tf.losses,tf.metrics
Componentsusefulwhen buildingcustomNNmodels
CoreTensorFlow(Python)
PythonAPIgivesyoufull control
CoreTensorFlow(C++)
C++APIisquitelowlevel
CPU
GPU
TPU
Android
TFrunsondifferenthardware
e n i g n E L M d u o l C
TensorFlowtoolkithierarchy RunTFat scalewith CMLE
tf.estimator
High-levelAPIfordistributed training
tf.layers,tf.losses,tf.metrics
Componentsusefulwhen buildingcustomNNmodels
CoreTensorFlow(Python)
PythonAPIgivesyoufull control
CoreTensorFlow(C++)
C++APIisquitelowlevel
CPU
GPU
TPU
Android
TFrunsondifferenthardware
e n i g n E L M d u o l C
ThePythonAPIletsyou buildandrunDirected Graphs
... c = tf.add(a, b)
Build
ThePythonAPIletsyoubuildandrunDirectedGraphs a
... c = tf.add(a, b)
Build
b
add c
Tensors
Operations
ThePythonAPIletsyoubuildandrunDirectedGraphs a
... c = tf.add(a, b)
Build
b
add
Run session = tf.Session() numpy_c = session.run(c, feed_dict= ….)
c
Tensors
Operations
ThePythonAPIletsyoubuildandrunDirectedGraphs a
... c = tf.add(a, b)
Build
b
add
Run session = tf.Session() numpy_c = session.run(c, feed_dict= ….)
c
Tensors
Operations
ThePythonAPIletsyoubuildandrunDirectedGraphs a
... c = tf.add(a, b)
Build
b
add
Run session = tf.Session() numpy_c = session.run(c, feed_dict= ….)
c
Tensors
Operations
ThePythonAPIletsyoubuildandrunDirectedGraphs a
... c = tf.add(a, b)
Build
b
add
Run session = tf.Session() numpy_c = session.run(c, feed_dict= ….)
c
Tensors
Operations
TensorFlowdoeslazyevaluation: youneedtorunthegraphtoget results* Build
a = tf.constant([5, 3, 8]) b = tf.constant([3, -1, 2]) c = tf.add(a, b) print c
Tensor("Add_7:0", shape=(3,), dtype=int32) with tf.Session() as sess: result = sess.run(c) print result [8 2 10]
Run
*tf.eager, however,allows youtoexecute operations imperativelylink
TensorFlowdoeslazyevaluation:youneedtorunthe graphtogetresults* numpy a = np.array([5, 3, 8]) b = np.array([3, -1, 2]) c = np.add(a, b) print c [8 2 10]
*tf.eager,however,allows youtoexecuteoperations imperativelylink
TensorFlow Build
a = tf.constant([5, 3, 8]) b = tf.constant([3, -1, 2]) c = tf.add(a, b) print c
Tensor("Add_7:0", shape=(3,), dtype=int32) with tf.Session() as sess: result = sess.run(c) print result [8 2 10]
Run
TensorFlowdoeslazyevaluation:youneedtorunthe graphtogetresults* numpy a = np.array([5, 3, 8]) b = np.array([3, -1, 2]) c = np.add(a, b) print c [8 2 10]
*tf.eager,however,allows youtoexecuteoperations imperativelylink
TensorFlow Build
a = tf.constant([5, 3, 8]) b = tf.constant([3, -1, 2]) c = tf.add(a, b) print c
Tensor("Add_7:0", shape=(3,), dtype=int32) with tf.Session() as sess: result = sess.run(c) print result [8 2 10]
Run
TensorFlowdoeslazyevaluation:youneedtorunthe graphtogetresults* numpy a = np.array([5, 3, 8]) b = np.array([3, -1, 2]) c = np.add(a, b) print c [8 2 10]
*tf.eager,however,allows youtoexecuteoperations imperativelylink
TensorFlow Build
a = tf.constant([5, 3, 8]) b = tf.constant([3, -1, 2]) c = tf.add(a, b) print c
Tensor("Add_7:0", shape=(3,), dtype=int32) with tf.Session() as sess: result = sess.run(c) print result [8 2 10]
Run
TensorFlowdoeslazyevaluation:youneedtorunthe graphtogetresults* numpy a = np.array([5, 3, 8]) b = np.array([3, -1, 2]) c = np.add(a, b) print c [8 2 10]
*tf.eager,however,allows youtoexecuteoperations imperativelylink
TensorFlow Build
a = tf.constant([5, 3, 8]) b = tf.constant([3, -1, 2]) c = tf.add(a, b) print c
Tensor("Add_7:0", shape=(3,), dtype=int32) with tf.Session() as sess: result = sess.run(c) print result [8 2 10]
Run
GraphandSession
LakLakshmanan
Graphscanbeprocessed,compiled,remotely executed,andassignedtodevices Data:tensors(N-dimensionalarrays) Nodes:operations (ops)ontensors
biases
Add
weights MatMul examples
Relu
Graphscanbeprocessed,compiled,remotely executed,andassignedtodevices
biases Add
weights MatMul examples
send
recv
Graphscanbeprocessed,compiled,remotely executed,andassignedtodevices
Inadditiontocomputation(e.g.,add, matmul),constants,variables,logging,and controlflowarealsoOps
biases
Add
weights MatMul examples
send
recv
SessionallowsTensorFlowtocacheanddistribute computation Python front end
...
Session
CoreTensorFlowExecutionSystem Ops
add
mul
Print
reshape
...
Kernels
CPU
GPU
Android
iOS
...
ExecuteTensorFlow graphsbycallingrun()on atf.Session import tensorflow as tf x = tf.constant([3, 5, 7]) y = tf.constant([1, 2, 3]) z = tf.add(x, y) with tf.Session() as sess: print sess.run(z) [4 7 10]
ExecuteTensorFlowgraphsbycallingrun() onatf.Session import tensorflow as tf x = tf.constant([3, 5, 7]) y = tf.constant([1, 2, 3]) z = tf.add(x, y) with tf.Session() as sess: print sess.run(z) [4 7 10] [4 7 10]
x
y
ExecuteTensorFlowgraphsbycallingrun() onatf.Session import tensorflow as tf x = tf.constant([3, 5, 7]) y = tf.constant([1, 2, 3]) z = tf.add(x, y) with tf.Session() as sess: print sess.run(z) [4 7 10] [4 7 10]
x
y
tf.add
z
ExecuteTensorFlowgraphsbycallingrun() onatf.Session import tensorflow as tf x = tf.constant([3, 5, 7]) y = tf.constant([1, 2, 3]) z = tf.add(x, y) with tf.Session() as sess: print sess.run(z) [4 7 10] [4 7 10]
x
y
tf.add
z
Evaluatingatensorisa shortcuttocallingrun()on thegraph’sdefaultsession import tensorflow as tf x = tf.constant([3, 5, 7]) y = tf.constant([1, 2, 3]) z = tf.add(x, y) with tf.Session() as sess: print z.eval() [4 7 10] [4 10]
Itispossibletoevaluatea listoftensors Shortcuts tocommon arithmetic operators
run() acceptsa listof tensors
Itispossibletoevaluatea listoftensors Shortcuts tocommon arithmetic operators
run() acceptsa listof tensors
TensorFlowinEagermodemakesiteasiertotryout things,butisnotrecommendedforproductioncode Importtf.eager Call exactly once
Notethevalue beingprinted
TensorFlowinEagermodemakesiteasiertotryout things,butisnotrecommendedforproductioncode Importtf.eager Call exactly once
Notethevalue beingprinted
TensorFlowinEagermodemakesiteasiertotryout things,butisnotrecommendedforproductioncode Importtf.eager Call exactly once
TensorFlowinEagermodemakesiteasiertotryout things,butisnotrecommendedforproductioncode Importtf.eager Call exactly once
Notethevalue beingprinted
Youcanwritethegraphoutusing tf.summary.FileWriter Namethe tensors andthe operations Writethe session graphtoa summary directory
Youcanwritethegraphoutusing tf.summary.FileWriter Namethe tensors andthe operations Writethe session graphtoa summary directory
Thegraphcanbe visualizedinTensorBoard
Thegraphcanbevisualizedin TensorBoard
Youcanalsowritetogs:// andstartTensorBoard fromCloudShell
1
Runthefollowingcommandin CloudShelltostart TensorBoard: tensorboard--port8080--logdir gs://${BUCKET}/${SUMMARY_DIR}
Youcanalsowritetogs:// andstartTensorBoard fromCloudShell
2
Toopenanewbrowser window,selectPreviewon port8080fromtheWeb previewmenuinthetop-right corneroftheCloudShell toolbar. Inthenewwindow,youcanuse TensorBoardtoseethetraining summaryandthevisualized networkgraph.
Youcanalsowritetogs:// andstartTensorBoard fromCloudShell
3
PressControl+Ctostop TensorBoardinCloudShell. https://cloud.google.com/ml-eng ine/docs/distributed-tensorflowmnist-cloud-datalab
TensorandVariable
LakLakshmanan
AtensorisanN-dimensionalarrayofdata Common name
Rank (Dimension)
Example
Shape of example
Scalar
0
x = tf.constant(3)
()
Vector
1
x = tf.constant([3, 5, 7])
(3,)
Matrix
2
x = tf.constant([[3, 5, 7], [4, 6, 8]])
(2,3)
3D Tensor
3
tf.constant([[[3, 5, 7],[4, 6, 8]], [[1, 2, 3],[4, 5, 6]] ])
(2,2,3)
nD Tensor
n
x1 x2 x3 x4
(3,) (2,3) (4,2,3) (2,4,2,3)
= = = =
tf.constant([2, 3, 4]) tf.stack([x1, x1]) tf.stack([x2, x2, x2, x2]) tf.stack([x3, x3])
AtensorisanN-dimensionalarrayofdata Common name
Rank (Dimension)
Example
Shape of example
Scalar
0
x = tf.constant(3)
()
Vector
1
x = tf.constant([3, 5, 7])
(3,)
Matrix
2
x = tf.constant([[3, 5, 7], [4, 6, 8]])
(2,3)
3D Tensor
3
tf.constant([[[3, 5, 7],[4, 6, 8]], [[1, 2, 3],[4, 5, 6]] ])
(2,2,3)
nD Tensor
n
x1 x2 x3 x4
(3,) (2,3) (4,2,3) (2,4,2,3)
= = = =
tf.constant([2, 3, 4]) tf.stack([x1, x1]) tf.stack([x2, x2, x2, x2]) tf.stack([x3, x3])
AtensorisanN-dimensionalarrayofdata Common name
Rank (Dimension)
Example
Shape of example
Scalar
0
x = tf.constant(3)
()
Vector
1
x = tf.constant([3, 5, 7])
(3,)
Matrix
2
x = tf.constant([[3, 5, 7], [4, 6, 8]])
(2,3)
3D Tensor
3
tf.constant([[[3, 5, 7],[4, 6, 8]], [[1, 2, 3],[4, 5, 6]] ])
(2,2,3)
nD Tensor
n
x1 x2 x3 x4
(3,) (2,3) (4,2,3) (2,4,2,3)
= = = =
tf.constant([2, 3, 4]) tf.stack([x1, x1]) tf.stack([x2, x2, x2, x2]) tf.stack([x3, x3])
AtensorisanN-dimensionalarrayofdata Common name
Rank (Dimension)
Example
Shape of example
Scalar
0
x = tf.constant(3)
()
Vector
1
x = tf.constant([3, 5, 7])
(3,)
Matrix
2
x = tf.constant([[3, 5, 7], [4, 6, 8]])
(2,3)
3D Tensor
3
tf.constant([[[3, 5, 7],[4, 6, 8]], [[1, 2, 3],[4, 5, 6]] ])
(2,2,3)
nD Tensor
n
x1 x2 x3 x4
(3,) (2,3) (4,2,3) (2,4,2,3)
= = = =
tf.constant([2, 3, 4]) tf.stack([x1, x1]) tf.stack([x2, x2, x2, x2]) tf.stack([x3, x3])
AtensorisanN-dimensionalarrayofdata Common name
Rank (Dimension)
Example
Shape of example
Scalar
0
x = tf.constant(3)
()
Vector
1
x = tf.constant([3, 5, 7])
(3,)
Matrix
2
x = tf.constant([[3, 5, 7], [4, 6, 8]])
(2,3)
3D Tensor
3
tf.constant([[[3, 5, 7],[4, 6, 8]], [[1, 2, 3],[4, 5, 6]] ])
(2,2,3)
nD Tensor
n
x1 x2 x3 x4
(3,) (2,3) (4,2,3) (2,4,2,3)
= = = =
tf.constant([2, 3, 4]) tf.stack([x1, x1]) tf.stack([x2, x2, x2, x2]) tf.stack([x3, x3])
AtensorisanN-dimensionalarrayofdata Common name
Rank (Dimension)
Example
Shape of example
Scalar
0
x = tf.constant(3)
()
Vector
1
x = tf.constant([3, 5, 7])
(3,)
Matrix
2
x = tf.constant([[3, 5, 7], [4, 6, 8]])
(2,3)
3D Tensor
3
tf.constant([[[3, 5, 7],[4, 6, 8]], [[1, 2, 3],[4, 5, 6]] ])
(2,2,3)
nD Tensor
n
x1 x2 x3 x4
(3,) (2,3) (4,2,3) (2,4,2,3)
= = = =
tf.constant([2, 3, 4]) tf.stack([x1, x1]) tf.stack([x2, x2, x2, x2]) tf.stack([x3, x3])
Tensorscanbesliced
Tensorscanbesliced
Tensorscanbesliced
Tensorscanbesliced
Quiz: (1) What would x[1, :] do?
(2) How about
x[1, 0:2]?
Tensorscanbesliced
Quiz: (1) What would x[1, :] do? [4,6,8] (2) How about [4,6,8]
x[1, 0:2]?
Tensorscanbereshaped
Tensorscanbereshaped
Tensorscanbereshaped
Tensorscanbereshaped
Tensorscanbereshaped
Tensorscanbereshaped
Tensorscanbereshaped
Avariableisatensorwhosevalueisinitializedandthen typicallychangedastheprogramruns
Createvariable, specifyinghowto initandwhether itcanbetuned “Trainingloop”of 5updatesto weights
Avariableisatensorwhosevalueisinitializedandthen typicallychangedastheprogramruns
Createvariable, specifyinghowto initandwhether itcanbetuned “Trainingloop”of 5updatesto weights
Avariableisatensorwhosevalueisinitializedandthen typicallychangedastheprogramruns
Createvariable, specifyinghowto initandwhether itcanbetuned “Trainingloop”of 5updatesto weights
Avariableisatensorwhosevalueisinitializedandthen typicallychangedastheprogramruns
Createvariable, specifyinghowto initandwhether itcanbetuned “Trainingloop”of 5updatesto weights
Avariableisatensorwhosevalueisinitializedandthen typicallychangedastheprogramruns
Createvariable, specifyinghowto initandwhether itcanbetuned “Trainingloop”of 5updatesto weights
Avariableisatensorwhosevalueisinitializedandthen typicallychangedastheprogramruns
Createvariable, specifyinghowto initandwhether itcanbetuned “Trainingloop”of 5updatesto weights
Avariableisatensorwhosevalueisinitializedandthen typicallychangedastheprogramruns
Createvariable, specifyinghowto initandwhether itcanbetuned “Trainingloop”of 5updatesto weights
Avariableisatensorwhosevalueisinitializedandthen typicallychangedastheprogramruns
Createvariable, specifyinghowto initandwhether itcanbetuned “Trainingloop”of 5updatesto weights
Avariableisatensorwhosevalueisinitializedandthen typicallychangedastheprogramruns Multiplying[1,2] x[2,3]yieldsa [1,3]matrix Initializeall variables Print[1,3] matrixateach ofthe5 iterations
Avariableisatensorwhosevalueisinitializedandthen typicallychangedastheprogramruns Multiplying[1,2] x[2,3]yieldsa [1,3]matrix Initializeall variables Print[1,3] matrixateach ofthe5 iterations
Avariableisatensorwhosevalueisinitializedandthen typicallychangedastheprogramruns Multiplying[1,2] x[2,3]yieldsa [1,3]matrix Initializeall variables Print[1,3] matrixateach ofthe5 iterations
Avariableisatensorwhosevalueisinitializedandthen typicallychangedastheprogramruns
Createvariable, specifyinghowtoinitand whetheritcanbetuned
“Trainingloop”of5 updatestoweights
Multiplying[1,2]x[2,3] yieldsa[1,3]matrix Initializeallvariables
Avariableisatensorwhosevalueisinitializedandthen typicallychangedastheprogramruns
Createvariable, specifyinghowtoinitand whetheritcanbetuned
“Trainingloop”of5 updatestoweights
Multiplying[1,2]x[2,3] yieldsa[1,3]matrix Initializeallvariables
Avariableisatensorwhosevalueisinitializedandthen typicallychangedastheprogramruns
Createvariable, specifyinghowtoinitand whetheritcanbetuned
“Trainingloop”of5 updatestoweights
Multiplying[1,2]x[2,3] yieldsa[1,3]matrix Initializeallvariables
Avariableisatensorwhosevalueisinitializedandthen typicallychangedastheprogramruns
Createvariable, specifyinghowtoinitand whetheritcanbetuned
“Trainingloop”of5 updatestoweights
Multiplying[1,2]x[2,3] yieldsa[1,3]matrix Initializeallvariables
Avariableisatensorwhosevalueisinitializedandthen typicallychangedastheprogramruns
Createvariable, specifyinghowtoinitand whetheritcanbetuned
“Trainingloop”of5 updatestoweights
Multiplying[1,2]x[2,3] yieldsa[1,3]matrix Initializeallvariables
Avariableisatensorwhosevalueisinitializedandthen typicallychangedastheprogramruns
Createvariable, specifyinghowtoinitand whetheritcanbetuned
“Trainingloop”of5 updatestoweights
Multiplying[1,2]x[2,3] yieldsa[1,3]matrix Initializeallvariables
Placeholdersallowyoutofeedinvalues,suchasby readingfromatextfile
Placeholdersallowyoutofeedinvalues,suchasby readingfromatextfile
Tensor("Placeholder :0", dtype=float32)
Placeholdersallowyoutofeedinvalues,suchasby readingfromatextfile
Tensor("Placeholder :0", dtype=float32)
Placeholdersallowyoutofeedinvalues,suchasby readingfromatextfile
Tensor("Placeholder :0", dtype=float32)
[
4.
8.
12.]
LAB Writinglow-level TensorFlowprograms
LakLakshmanan
Lab:Writinglow-levelTensorFlowprograms Inthislab,youwilllearnhowtheTensorFlowPythonAPIworks 1.
Building a graph
2.
Running a graph
3.
Feeding values into a graph
4.
Findingtheareaofatrianglebyusing TensorFlow
DebuggingTensorFlow programs
LakLakshmanan
DebuggingTensorFlowprogramsissimilarto debugginganypieceofsoftware 1
2
ReadError Messages
Isolatethe methodin question
3
Send made-up dataintothe method
4
Knowhow tosolve common problems
Readerrormessagesto understandtheproblem 1
in some_method(data) 2 a = data[:,0:2] 3 c = data[:,1] ----> 4 s = (a + c) 5 return tf.sqrt(tf.matmul( tf.sqrt(tf.matmul(s, s, tf.transpose(s)))
Readerrormessagesto understandtheproblem 1
output: ValueError: Dimensions must be equal, but are 2 and 4 for 'add' (op: 'Add') with input shapes: [4,2], [4].
Isolatethemethodin question 2
def some_method(data): a = data[:,0:2] c = data[:,1] s = (a + c) return tf.sqrt(tf.matmul(s, tf.transpose(s)))
Calltheproblematic methodwithfakedata 3
def some_method(data): a = data[:,0:2] print a.get_shape() c = data[:,1] print c.get_shape() s = (a + c) return tf.sqrt(tf.matmul(s, tf.transpose(s))) with tf.Session() as sess: fake_data = tf.constant([ [5.0, 3.0, 7.1], [2.3, 4.1, 4.8], [2.8, 4.2, 5.6], [2.9, 8.3, 7.3] ]) print sess.run(some_method(fake_data))
Calltheproblematic methodwithfakedata 3
def some_method(data): a = data[:,0:2] print a.get_shape() c = data[:,1] print c.get_shape() s = (a + c) return tf.sqrt(tf.matmul(s, tf.transpose(s))) with tf.Session() as sess: fake_data = tf.constant([ [5.0, 3.0, 7.1], [2.3, 4.1, 4.8], [2.8, 4.2, 5.6], [2.9, 8.3, 7.3] ]) print sess.run(some_method(fake_data))
Calltheproblematic methodwithfakedata 3
def some_method(data): a = data[:,0:2] (4, 2) print a.get_shape() Whydoesthe c = data[:,1] additionfail? print c.get_shape() (4,) s = (a + c) return tf.sqrt(tf.matmul(s, tf.transpose(s))) with tf.Session() as sess: fake_data = tf.constant([ [5.0, 3.0, 7.1], [2.3, 4.1, 4.8], [2.8, 4.2, 5.6], [2.9, 8.3, 7.3] ]) print sess.run(some_method(fake_data))
Knowhowtosolve commonproblems 4
Tensorshape Scalar-vectormismatch Datatypemismatch
Themostcommonproblemtendstobetensorshape def some_method(data): a = data[:,0:2] print a.get_shape() (4, 2) c = data[:,1] Whydoestheadditionfail? (4,) print c.get_shape() s = (a + c) return tf.sqrt(tf.matmul(s, tf.transpose(s))) with tf.Session() as sess: fake_data = tf.constant([ [5.0, 3.0, 7.1], [2.3, 4.1, 4.8], [2.8, 4.2, 5.6], [2.9, 8.3, 7.3] ]) print sess.run(some_method(fake_data))
Themostcommonproblemtendstobetensorshape c = data[:,1:3]
output:
(4, 2) [[ 12.88448715 [ 11.87813091 [ 12.44909573 [ 15.72132301
Add an assert
11.87813091 10.96220779 11.48999596 14.50930595
12.44909573 11.48999596 12.04325485 15.20789242
15.72132301] 14.50930595] 15.20789242] 19.20416641]]
Shapeproblemsalsohappenbecauseofbatchsizeor becauseyouhaveascalarwhenavectorisneeded(or viceversa) Tensor("Placeholde r_4:0", shape=(?, 3), dtype=float32)
ValueError: Cannot feed value of shape (3,)
output
Tensor("Placeholder_4:0", shape=(?, 3), dtype=float32)
Shapeproblemsalsohappenbecauseofbatchsizeor becauseyouhaveascalarwhenavectorisneeded(or viceversa) Tensor("Placeholde r_4:0", shape=(?, 3), dtype=float32)
ValueError: Cannot feed value of shape (3,)
output
Tensor("Placeholder_4:0", shape=(?, 3), dtype=float32)
Shapeproblemsalsohappenbecauseofbatchsizeor becauseyouhaveascalarwhenavectorisneeded(or viceversa) Tensor("Placeholde r_4:0", shape=(?, 3), dtype=float32)
ValueError: Cannot feed value of shape (3,)
output
Tensor("Placeholder_4:0", shape=(?, 3), dtype=float32)
Shapeproblemsalsohappenbecauseofbatchsizeor becauseyouhaveascalarwhenavectorisneeded(or viceversa) Tensor("Placeholde r_4:0", shape=(?, 3), dtype=float32)
ValueError: Cannot feed value of shape (3,)
output
Tensor("Placeholder_4:0", shape=(?, 3), dtype=float32)
Shapeproblemsalsohappenbecauseofbatchsizeor becauseyouhaveascalarwhenavectorisneeded(or viceversa) Tensor("Placeholde r_4:0", shape=(?, 3), dtype=float32)
ValueError: Cannot feed value of shape (3,)
output
Tensor("Placeholder_4:0", shape=(?, 3), dtype=float32)
Shapeproblemscanoften befixedusing... 1. tf.reshape() 2. tf.expand_dims() 3. tf.slice() 4. tf.squeeze()
tf.expand_dimsinsertsadimensionof1intoa tensor’sshape tf.expand_dims() output x = tf.constant([[3, 2], [4, 5], [6, 7]]) print "x.shape", x.shape expanded = tf.expand_dims(x, 1) print "expanded.shape", expanded.shape
x.shape (3, 2) expanded.shape (3, 1, 2)
with tf.Session() as sess: print "expanded:\n", expanded.eval()
[[4 5]]
expanded: [[[3 2]]
[[6 7]]]
tf.expand_dimsinsertsadimensionof1intoa tensor’sshape tf.expand_dims() output x = tf.constant([[3, 2], [4, 5], [6, 7]]) print "x.shape", x.shape expanded = tf.expand_dims(x, 1) print "expanded.shape", expanded.shape
x.shape (3, 2) expanded.shape (3, 1, 2)
with tf.Session() as sess: print "expanded:\n", expanded.eval()
[[4 5]]
expanded: [[[3 2]]
[[6 7]]]
tf.expand_dimsinsertsadimensionof1intoa tensor’sshape tf.expand_dims() output x = tf.constant([[3, 2], [4, 5], [6, 7]]) print "x.shape", x.shape expanded = tf.expand_dims(x, 1) print "expanded.shape", expanded.shape
x.shape (3, 2) expanded.shape (3, 1, 2)
with tf.Session() as sess: print "expanded:\n", expanded.eval()
[[4 5]]
expanded: [[[3 2]]
[[6 7]]]
tf.expand_dimsinsertsadimensionof1intoa tensor’sshape tf.expand_dims() output x = tf.constant([[3, 2], [4, 5], [6, 7]]) print "x.shape", x.shape expanded = tf.expand_dims(x, 1) print "expanded.shape", expanded.shape
x.shape (3, 2) expanded.shape (3, 1, 2)
with tf.Session() as sess: print "expanded:\n", expanded.eval()
[[4 5]]
expanded: [[[3 2]]
[[6 7]]]
tf.sliceextractsaslicefromatensor tf.slice()output x = tf.constant([[3, 2],
x.shape (3, 2)
[4, 5],
sliced.shape (2, 1)
[6, 7]])
sliced:
print "x.shape", x.shape sliced = tf.slice(x, [0, 1], [2, 1]) print "sliced.shape", sliced.shape with tf.Session() as sess: print "sliced:\n", sliced.eval()
[[2] [5]]
tf.sliceextractsaslicefromatensor tf.slice()output x = tf.constant([[3, 2],
x.shape (3, 2)
[4, 5],
sliced.shape (2, 1)
[6, 7]])
sliced:
print "x.shape", x.shape sliced = tf.slice(x, [0, 1], [2, 1]) print "sliced.shape", sliced.shape with tf.Session() as sess: print "sliced:\n", sliced.eval()
[[2] [5]]
tf.sliceextractsaslicefromatensor tf.slice()output x = tf.constant([[3, 2],
x.shape (3, 2)
[4, 5],
sliced.shape (2, 1)
[6, 7]])
sliced:
print "x.shape", x.shape sliced = tf.slice(x, [0, 1], [2, 1]) print "sliced.shape", sliced.shape with tf.Session() as sess: print "sliced:\n", sliced.eval()
[[2] [5]]
tf.squeezeremovesdimensionsofsize1fromthe shapeofatensor t [[[1] [2] [3] [4]]
output
[[5] [6] [7] [8]]] t squeezed [[1 2 3 4] [5 6 7 8]]
tf.squeezeremovesdimensionsofsize1fromthe shapeofatensor t [[[1] [2] [3] [4]]
output
[[5] [6] [7] [8]]] t squeezed [[1 2 3 4] [5 6 7 8]]
Anothercommonproblem isdatatype
ValueError: Tensor conversion requested dtype float32 for Tensor with dtype int32: 'Tensor("Const_34:0", shape=(2, 3), dtype=int32)'
Thereasonisbecausewearemixingtypes def some_method(a, b): s = (a + b) return tf.sqrt(tf.matmul(s, tf.transpose(s)))
with tf.Session() as sess: fake_a = tf.constant([ [5.0, 3.0, 7.1], [2.3, 4.1, 4.8], ]) fake_b = tf.constant([ [2, 4, 5], [2, 8, 7] ]) print sess.run(some_method(fake_a, fake_b))
Addinga tensoroffloats toatensorof intswon’twork
Thereasonisbecausewearemixingtypes def some_method(a, b): s = (a + b) return tf.sqrt(tf.matmul(s, tf.transpose(s)))
with tf.Session() as sess: fake_a = tf.constant([ [5.0, 3.0, 7.1], [2.3, 4.1, 4.8], ]) fake_b = tf.constant([ [2, 4, 5], [2, 8, 7] ]) print sess.run(some_method(fake_a, fake_b))
Addinga tensoroffloats toatensorof intswon’twork
Onesolutionistodoacast withtf.cast() b = tf.cast(b,tf.float32)
Onesolutionistodoacast withtf.cast() output [[ 15.63361835 [ 16.04929924
16.04929924] 17.43960953]]
Todebugfull-blown programs,therearethree methods
Todebugfull-blown programs,therearethree methods tf.Print()
Todebugfull-blown programs,therearethree methods tf.Print()
tfdbg
Todebugfull-blown programs,therearethree methods tf.Print()
tfdbg
TensorBoard
Changelogginglevelfrom WARN
tf.Print()canbeusedtologspecifictensorvalues
%bash python xyz.py
Output: [[ [ https://www.tensorflow.org/programmers_guide/debugger
nan nan
nan] 1.43365264]]
tf.Print()canbeusedtologspecifictensorvalues
%bash python xyz.py
Output: [[ [ https://www.tensorflow.org/programmers_guide/debugger
nan nan
nan] 1.43365264]]
tf.Print()canbeusedtologspecifictensorvalues
%bash python xyz.py
Output: [[ [ https://www.tensorflow.org/programmers_guide/debugger
nan nan
nan] 1.43365264]]
TensorFlowhasadynamic,interactivedebugger(no easywaytouseitfromDatalabcurrently)
# in a Terminal window python xyz.py --debug
https://www.tensorflow.org/programmers_guide/debugger
UseTensorFlowdebuggertostepthroughcode
UseTensorFlowdebuggertostepthroughcode