**Neural Network Tutorials - Herong's Tutorial Examples** - 1.20, by Dr. Herong Yang

TensorFlow Session Class and run() Function

This section provides a tutorial example on how to create a TensoFlow session object and run any given nodes (tensor operations) in a tensor flow graph. You need to run a special operation, variables_initializer, to load the initial value to a variable node to avoid 'uninitialized value' error.

Once a tensor expression as a tensor flow graph, we need to find a way to evaluate the tension expression to get the final output tensor.

This is done by creating a TensorFlow Session object and calling its run() method using the following syntax. Note that if the graph has variable node, you need run a special operation to initialize it with its initial value.

# creating a session object, s. s = tf.Session() # initialize "variable" node, v. x = tf.variables_initializer(v) s.run(x) # evaluate the target node, n, # with a list of parameter tensors, one for each "placeholder" node. o = s.run(n, p) # o is the output tensor, not a node (operation)

Let's try this on our first tensor flow graph created earlier for the tensor operation of [a] = ([b]+[c])*([c]+[2]). Here is the my version of the complete script:

#- first_tensor_graph.py #- Copyright (c) HerongYang.com. All Rights Reserved. # import tensorflow as tf # create 2 variable nodes with initial values prepared, # but not loaded b = tf.Variable([[4,4,4],[4,4,4],[4,4,4]]) c = tf.Variable([[3,3,3],[3,3,3],[3,3,3]]) # create 1 constant node with a fixed value s = tf.constant([[2,2,2],[2,2,2],[2,2,2]]) # create 2 intermediate nodes t1 = tf.add(b,c) t2 = tf.add(c,s) # create the final node - the last operation a = tf.multiply(t1,t2) # create a TensorFlow session object to run the graph ss = tf.Session() # create special nodes for initialization operations bi = tf.variables_initializer([b]) ci = tf.variables_initializer([c]) # run initialization operations - loading initial values ss.run(bi) ss.run(ci) # run the last operation out = ss.run(a) # print the output print(out)

If you run the above script, you will get the following output, which is correct. You can verify this by performing the calculation yourself.

herong$ python3 first_tensor_graph.py [[35 35 35] [35 35 35] [35 35 35]]

You can actually call ses.run() on any nodes to see their outputs, since they are all tensor operations. Try it yourself.

Table of Contents

Deep Playground for Classical Neural Networks

Building Neural Networks with Python

Simple Example of Neural Networks

►TensorFlow - Machine Learning Platform

"tensorflow" - TensorFlow Python Library

"tensorflow" Interactive Test Web Page

►TensorFlow Session Class and run() Function

TensorFlow Variable Class and load() Function

Linear Regression with TensorFlow

tensorflow.examples.tutorials.mnist Module

Simple TensorFlow Model on MNIST Database

Commonly Used TensorFlow Funcitons

PyTorch - Machine Learning Platform

CNN (Convolutional Neural Network)