Rimeng Society
2.3 Sessions, Tensors, Variables OP
Learning Objectives
- target
- applicationSess.runOr eval runs the diagram program and gets the tensor value
- Apply feed_dict mechanism to populate data at runtime
- Applying placeholder implementation to create placeholders
- Knowing the common TensorFlow creation tensors
- Knowing common tensor mathematical operations
- Explain the identity of numpy's array and tensor
- Explain the two shape-changing characteristics of a tensor
- Apply set_Shapes andTf.reshapeImplement modification of tensor shape
- applicationTf.matmulModification of Matrix Operations to Implement Tensors
- applicationTf.castType of implementation tensor
- Explain the special role of variable op
- Explains the role of the trainable parameter of the variable op
- Apply global_variables_initializer implements the initialization of variable op
- application
- nothing
- Content Preview
2.3.1 Sessions
A class that runs TensorFlow operation.Sessions can be opened in two ways
- tf.Session: For use in a complete program
- tf.InteractiveSession: TensorFlow in an interactive context, such as a shell
1 TensorFlow usesTf.SessionClasses represent connections between client programs (usually Python programs, but also provide similar interfaces in other languages) and the C++ runtime
2Tf.SessionObjects use the distributed TensorFlow runtime to provide access to devices on the local computer and remote devices.
2.3.1.1 __init__(target='', graph=None, config=None)
Resources that a session may have, such asTf.Variable,Tf.QueueBaseandTf.ReaderBase.Releasing these resources is important when they are no longer needed.Therefore, you need to callTf.Session.closeMethod in a session, or use the session as a context manager.The following two examples serve the same purpose:
def session_demo(): """ //Session Demo :return: """ a_t = tf.constant(10) b_t = tf.constant(20) # Direct use of this symbolic operator for calculation is not advocated # More commonly, tensorflow functions are used to calculate # c_t = a_t + b_t c_t = tf.add(a_t, b_t) print("tensorflow Implement addition operations:\n", c_t) # Open Session # Traditional session definitions # sess = tf.Session() # sum_t = sess.run(c_t) # print("sum_t:\n", sum_t) # sess.close() # Open Session with tf.Session() as sess: # sum_t = sess.run(c_t) # Want to execute multiple tensor s simultaneously print(sess.run([a_t, b_t, c_t])) # A convenient way to get tensor values # print("sum_in sess"T:\n', c_T.eval()) # Diagram Properties of Sessions print("Diagram properties of a session:\n", sess.graph) return None
- target: If this parameter is left blank (the default setting), the session will only use devices on the local computer.You can specify the grpc://web address to specify the address of the TensorFlow server, which allows the session to access all devices on the computer controlled by that server.
- graph: by default, NEWTf.SessionBind to the current default diagram.
- config: This parameter allows you to specify aTf.ConfigProtoIn order to control the behavior of the session.For example, the ConfigProto protocol is used to print device usage information
# Run session and print device information sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True, log_device_placement=True))
Sessions can allocate different resources to run on different devices.
/job:worker/replica:0/task:0/device:CPU:0
device_type: type of device (e.g. CPU, GPU, TPU)
run() of a 2.3.1.2 session
-
run(fetches,feed_dict=None, options=None, run_metadata=None)
- through the use ofSess.run() to run the operation
- fetches: a single operation, or a list, tuple (other types that are not tensorflow do not work)
- feed_dict: The parameter allows the caller to override the value of the tensor in the graph and assign values at runtime
- andTf.placeholderWhen used together, the shape of the value is checked for compatibility with the placeholder.
UseTf.operation.eval() Operations can also be run, but need to be run in a session
# Create a graph a = tf.constant(5.0) b = tf.constant(6.0) c = a * b # Create Session sess = tf.Session() # Calculate C value print(sess.run(c)) print(c.eval(session=sess))
2.3.1.3 feed operation
- placeholder provides placeholders, run through feed_dict Specifies Parameter
def session_run_demo(): """ //run methods for sessions :return: """ # Define placeholders a = tf.placeholder(tf.float32) b = tf.placeholder(tf.float32) sum_ab = tf.add(a, b) print("sum_ab:\n", sum_ab) # Open Session with tf.Session() as sess: print("Results of placeholders:\n", sess.run(sum_ab, feed_dict={a: 3.0, b: 4.0})) return None
Note the error reported at runtime:
RuntimeError: If this Session is in an invalid state (e.g. closed). TypeError: If fetches or feed_The dict key is of an inappropriate type. ValueError: If fetches or feed_dict key is invalid or refers to a key that Tensor does not exist.
When writing TensorFlow programs, the primary goal of program delivery and operation isTf.Tensor
2.3.2 Tensor
TensorFlow's tensor is an n-dimensional array of typeTf.Tensor.Tensor has two important properties
- Type:Data type
- shape: shape (order)
Types of 2.3.2.1 Tensors
Order of 2.3.2.2 Tensor
Shapes are of order 0, 1, 2....
tensor1 = tf.constant(4.0) tensor2 = tf.constant([1, 2, 3, 4]) linear_squares = tf.constant([[4], [9], [16], [25]], dtype=tf.int32) print(tensor1.shape) # 0 dimension: () 1 dimension: (10,) 2 dimension: (3, 4) 3 dimension: (3, 4, 5)
2.3.3 Instructions to Create Tensors
- Fixed Value Tensor
- Random Value Tensor
- Other Special Ops to create tensors
- tf.Variable
- tf.placeholder
Transformation of 2.3.4 Tensor
2.3.4.1 Type Change
2.3.4.2 Shape Change
TensorFlow's tensor has two shape transformations, dynamic and static
- tf.reshape
- tf.set_shape
Dynamic and static shapes must conform to the following rules
- Static Shape
- When converting a static shape, 1-D to 1-D, 2-D to 2-D, cannot change shape across orders
- Static shapes cannot be set again for tensors of static shapes that have already been fixed
- dynamic shape
- tf.reshape() When creating a new tensor dynamically, the number of elements of the tensor must match
def tensor_demo(): """ //Introduction to Tensors :return: """ a = tf.constant(value=30.0, dtype=tf.float32, name="a") b = tf.constant([[1, 2], [3, 4]], dtype=tf.int32, name="b") a2 = tf.constant(value=30.0, dtype=tf.float32, name="a2") c = tf.placeholder(dtype=tf.float32, shape=[2, 3, 4], name="c") sum = tf.add(a, a2, name="my_add") print(a, a2, b, c) print(sum) # Get Tensor Properties print("a Graph properties:\n", a.graph) print("b Name:\n", b.name) print("a2 Shape:\n", a2.shape) print("c Data type:\n", c.dtype) print("sum Of op:\n", sum.op) # Get static shape print("b The static shape of:\n", b.get_shape()) # Define placeholders a_p = tf.placeholder(dtype=tf.float32, shape=[None, None]) b_p = tf.placeholder(dtype=tf.float32, shape=[None, 10]) c_p = tf.placeholder(dtype=tf.float32, shape=[3, 2]) # Get static shape print("a_p The static shape of the is:\n", a_p.get_shape()) print("b_p The static shape of the is:\n", b_p.get_shape()) print("c_p The static shape of the is:\n", c_p.get_shape()) # Shape Update # a_p.set_shape([2, 3]) # Static shape cannot be modified once it has been fixed # b_p.set_shape([10, 3]) # c_p.set_shape([2, 3]) # The part of a static shape that is already fixed includes its order, and if it is fixed, the shape cannot be updated across the order. # Use dynamic shapes if you want to change shapes across orders # a_p.set_shape([1, 2, 3]) # Get static shape print("a_p The static shape of the is:\n", a_p.get_shape()) print("b_p The static shape of the is:\n", b_p.get_shape()) print("c_p The static shape of the is:\n", c_p.get_shape()) # dynamic shape # c_p_r = tf.reshape(c_p, [1, 2, 3]) c_p_r = tf.reshape(c_p, [2, 3]) # Dynamic shape, when changing, cannot change the total number of elements # c_p_r2 = tf.reshape(c_p, [3, 1]) print("Results of dynamic shapes:\n", c_p_r) # print("Result of dynamic shape 2:\n", c_p_r2) return None
Mathematical operations of 2.3.5 tensors
- Arithmetic Operators
- Basic Mathematical Functions
- Matrix operations
- reduce operation
- Sequence Index Operation
Refer to: https://www.tensorflow.org/versions/r1.8/api_guides/python/math_ops
These API s are used, as described in the documentation
2.3.6 Variables
The TensorFlow variable is the best way to represent a shared persistent state handled by a program.Variables pass throughTf.VariableOP class to operate on.Variable characteristics:
- Storage persistence
- Modifiable Value
- Can be assigned to be trained
2.3.6.1 Creating variables
- tf.Variable(initial_value=None,trainable=True,collections=None,name=None)
- initial_value:Initialized value
- trainable: Is it trained
- collections: The new variable will be added to the collection of listed graphs, defaulting to [GraphKeys.GLOBAL_VARIABLES], if the trainable is a True variable, is also added to the graphics collectionGraphKeys.TRAINABLE_VARIABLES
- Variables need to be explicitly initialized to run values
def variable_demo(): """ //Presentation of variables :return: """ # Define Variables a = tf.Variable(initial_value=30) b = tf.Variable(initial_value=40) sum = tf.add(a, b) # initialize variable init = tf.global_variables_initializer() # Open Session with tf.Session() as sess: # Variable Initialization sess.run(init) print("sum:\n", sess.run(sum)) return None
2.3.6.2 UseTf.variable_Scope() modifies the namespace of a variable
Namespace name is added before OP name
with tf.variable_scope("name"): var = tf.Variable(name='var', initial_value=[4], dtype=tf.float32) var_double = tf.Variable(name='var', initial_value=[4], dtype=tf.float32) <tf.Variable 'name/var:0' shape=() dtype=float32_ref> <tf.Variable 'name/var_1:0' shape=() dtype=float32_ref>