tensorflow-计算图

版权声明:本博客所有文章版权归博主刘兴所有,转载请注意来源 https://blog.csdn.net/AI_LX/article/details/89465388
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Dec 23 11:26:04 2018


"""
import tensorflow as tf
#创建图
c=tf.constant(0.0)
g=tf.Graph()
with g.as_default():
    c1=tf.constant(0.1)
    print(c1.graph)
    print(g)
    print(c.graph)

<tensorflow.python.framework.ops.Graph object at 0xb1cd345c0>
<tensorflow.python.framework.ops.Graph object at 0xb1cd345c0>
<tensorflow.python.framework.ops.Graph object at 0x10d1f6160>
从输出可以看出
print(c1.graph)
print(g)
输出为同一个计算图,
而 print(c.graph)为另一个计算图

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Dec 23 11:26:04 2018


"""
import tensorflow as tf
#创建图
c=tf.constant(0.0)
g=tf.Graph()
with g.as_default():
    c1=tf.constant(0.1)
g2=tf.get_default_graph()
print(g2)
tf.reset_default_graph()
g3=tf.get_default_graph()
print(g3)
print(c1.name) 

<tensorflow.python.framework.ops.Graph object at 0xb19ce09b0>
<tensorflow.python.framework.ops.Graph object at 0xb19ce0e48>
Const:0
重置计算图以及获得tensor

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Dec 23 11:26:04 2018

@author: myhaspl
"""
import tensorflow as tf
#创建图
c=tf.constant(0.0)
g=tf.Graph()
with g.as_default():
    c1=tf.constant(0.1)
g2=tf.get_default_graph()
print(g2)
tf.reset_default_graph()
g3=tf.get_default_graph()
print(g3)
print(c1.name)
t=g.get_tensor_by_name(name="Const:0")
print(t)
a=tf.constant([[1.0,2.0]])
b=tf.constant([[1.0],[3.0]])
tensor1=tf.matmul(a,b,name="exampleop")
print(tensor1.name,tensor1)
test=g3.get_tensor_by_name("exampleop:0")
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)
tt2=g.get_operations()
print(tt2)

tt3=g.as_graph_element(c1)
print(tt3)

<tensorflow.python.framework.ops.Graph object at 0x7f8aaa8e5160>
<tensorflow.python.framework.ops.Graph object at 0x7f8a90a9a710>
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
}
}

2018-12-25 17:26:56.179157: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
[[7.]]
Tensor(“exampleop:0”, shape=(1, 1), dtype=float32)
[<tf.Operation ‘Const’ type=Const>]
Tensor(“Const:0”, shape=(), dtype=float32)
[

猜你喜欢

转载自blog.csdn.net/AI_LX/article/details/89465388