版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sunshinefcx/article/details/85397621
#所以需要注意的是 张量 不是 节点操作(OP)
#tensor1 = tf.matmul(a,b,name='exampleop')
#上面这个 定义的只是一个张量,是在一个静态的图(graph)中的
#张量在 定义完成之后是不会进行操作的,想要进行操作就必须使用 节点操作 也就是OP来运行,才能计算出ab矩阵的乘积
#OP其实是在描述 张量 中的运算关系
可以参照下面的这个例子,来理解tensorflow中 张量 与 节点操作 之间的关系
import tensorflow as tf
tf.reset_default_graph()
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)
g3 = tf.get_default_graph()
#注意 tensor的名字是定义的 name 再加上 :0,也就是 name:0
#把找到的tensor复制给这个变量,也就是说test和tensor1是一样的
test = g3.get_tensor_by_name('exampleop:0')
print(test)
print(tensor1.op.name)
#获取节点操作 OP
testop = g3.get_operation_by_name("exampleop")
print("a:",a.name)
print("b:",b.name)
print(testop)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
test = sess.run(test)
print(test)
test = tf.get_default_graph().get_tensor_by_name("exampleop:0")
print(test)