Basic operations on TensorFlow graphs

An example description
This example demonstrates the use of 3 ways to create a graph, and demonstrates the operation of obtaining graph-related content.
A TensorFlow program creates a graph by default. In addition to the automatic graph creation by the system, it can also be created manually and do some other operations.

two code
import numpy as np
import tensorflow as tf
#1 Method of creating a graph
'''
The following code demonstrates the process of using the tf.Graph function to create a graph, using the tf.get_default_graph function to obtain the graph, and using reset_default_graph to reset the graph
'''
c = tf.constant(0.0)
g = tf.Graph()
with g.as_default():
  c1 = tf.constant(0.0)
  print(c1.graph) #<tensorflow.python.framework.ops.Graph object at 0x000001DAEDB25908> Obtain the graph through .graph
  print(g) #<tensorflow.python.framework.ops.Graph object at 0x000001DAEDB25908> The graph created by tf.Graph
  print(c.graph) #<tensorflow.python.framework.ops.Graph object at 0x000001DAEDB25160>, the default graph was created at the beginning
g2 =  tf.get_default_graph()
print(g2) #<tensorflow.python.framework.ops.Graph object at 0x000001DAEDB25160>, get the default graph at the beginning
tf.reset_default_graph()
g3 =  tf.get_default_graph()
print(g3) #<tensorflow.python.framework.ops.Graph object at 0x000001DAEDB25668> Rebuild and replace the original default graph with a graph

# 2. Get tensor
'''
In the graph, you can get the corresponding element by name, such as get_tensor_by_name to get the tensor in the graph
'''
print(c1.name) #Const:0 c1 is established in subgraph g
t = g.get_tensor_by_name(name = "Const:0") #Access the tensor in the graph, put the name of c1 in get_tensor_by_name to get its tensor in reverse
print(t)  #Tensor("Const:0", shape=(), dtype=float32)

# 3 Get op
'''
The method of getting node operation op is very similar to the method of getting tensor, the method used is get_operation_by_name
'''
a = tf.constant([[1.0, 2.0]])
b = tf.constant([[1.0], [3.0]])
tensor1 = tf.matmul(a, b, name='exampleop') #Not OP, but tensor. OP is actually describing the operation relationship in the tensor
print(tensor1.name,tensor1) #Print the name of the tensor and the tensor
test = g3.get_tensor_by_name("exampleop:0") #test is the same as tensor1
print(test)
print(tensor1.op.name)
testop = g3.get_operation_by_name("exampleop")
print(testop)
with tf.Session() as sess:
    test = sess.run (test)
    print(test)
    test = tf.get_default_graph().get_tensor_by_name("exampleop:0")
    print (test)

#4 Get all lists
#Return the list of operation nodes in the graph
tt2 = g.get_operations()
print(tt2) #[<tf.Operation 'Const' type=Const>] Since there is only one constant in g, all print a message

#5 Get Objects
'''
Use the as_graph_element function, that is, pass in an object and return a tensor or an OP. This function has validation and conversion functions and is occasionally used in multi-threading.
'''
tt3 = g.as_graph_element(c1)
print(tt3)
print("________________________\n")
Three running results
<tensorflow.python.framework.ops.Graph object at 0x000001DAEDB25908>
<tensorflow.python.framework.ops.Graph object at 0x000001DAEDB25908>
<tensorflow.python.framework.ops.Graph object at 0x000001DAEDB25160>
<tensorflow.python.framework.ops.Graph object at 0x000001DAEDB25160>
<tensorflow.python.framework.ops.Graph object at 0x000001DAEDB25668>
Const:0
Tensor("Const:0", shape=(), dtype=float32)
exampleop:0 Tensor("exampleop:0", shape=(1, 1), dtype=float32)
Tensor("exampleop:0", shape=(1, 1), dtype=float32)
exampleop
name: "exampleop"
op: "MatMul"
input: "Const"
input: "Const_1"
attr {
  key: "T"
  value {
    type: DT_FLOAT
  }
}
attr {
  key: "transpose_a"
  value {
    b: false
  }
}
attr {
  key: "transpose_b"
  value {
    b: false
  }
}
[[ 7.]]
Tensor("exampleop:0", shape=(1, 1), dtype=float32)
[<tf.Operation 'Const' type=Const>]
Tensor("Const:0", shape=(), dtype=float32)

Four instructions
This example embodies the operations of creating a graph, getting tensors, getting nodes, getting a list of elements, and getting objects.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324726295&siteId=291194637