开发者学堂课程【深度学习框架TensorFlow入门:Operation的介绍】学习笔记,与课程紧密联系,让用户快速学习知识。
课程地址:https://developer.aliyun.com/learning/course/773/detail/13540
Operation的介绍
内容介绍
一.常见的 OP
二.指令名称
一.常见的 OP
(1)哪些是 OP
(2)操作函数与操作对象区分
操作函数 & 操作对象
tf.constant(Tensor 对象)
tf.constant(Tensor对象)
|
输入Tensor对象 -Consor-输出 Tensor对象 |
tf.add(Tensor对象1,Tensor对象2) |
输入Tensor对象1,Tensor对象2 – Add对象 – 输出Tensor对象3 |
一个操作对象 (Operation) 是 TensorFlow 图中的一个节点,可以接收0个或者多个输入 Tensor,井且可以输出0个或者多个 Tensor, Operation 对象是 通过 op 构函数(如 t.matmul0 ) 创建的。
例如: c = t.matmul(a, b) 创建了一个 Operation 对象,类型为 MatMul 类型,它将张量 a, b 作为输入,c 作为输出,并且输出数据,打印的时候也是打印的数据。其中 t.matmul() 是函数,在执行 matmul 函数的过程中 会通过 MatMul 类创建一个与之对应的对象
#实现一个加法运算
con_ a = tf.constant(3.0)
con _b =tf.constant(4.0)
sum_ c =tf.add(con_a, con_b)
print(“打印con_a:\n”,con_a)
print("打印con_b:\n", con_b)
print("打印con_c:\n", sum_c)
打印语句会生成:
打印 con_a:
Tensor("Const:0", shape=(), dtype=float32)
打印 con_ b:
Tensor("Const 1:0". shape=(), dtype=float32)
打印 sum_c:
Tensor("Add:0", shape=(), dtype=float32)
注意:打印出来的是张量值,可以理解成 OP 当中包含了这个值。并且每一个 OP 指令都对应一个唯一的名称,如上面的 Const:0,这个在TensorBoard 上面也可以显示。
tf.Tensor 对象以输出该张量的 tf.Operation 明确命名。张量名称的形式为"<OP_ NAME>:<i>",其中:
"<OP_ NAME>"是生成该张量的指令的名称
"<i>"是-一个整数,它表示该张量在指令的输出中的索引
二、指令名称
tf.Graph 对象为其包含的 tf.Operation 对象定义的一个命名空间
TensorFlow 会自动为图中的每个指令选择一个唯一名称, 用户也
可以指定描述性名称,使程序阅读起来更轻松。我们可以以以下方式改写指令名称
每个创建新的 tf.Operation 或返回新的 tf.Tensor 的 API 函数可以接受可选的 name 参数。
例如,tf.constant(42.0, name=" answer")创建了一个名为"answer"的新 tf.Operation 并返回一个名为"answer:0"的 tf.Tensor。如果默认图已包含名为 "answer"的指令,则 TensorFlow 会在名称上附加“1"、“2“等字符,以便让名称具有唯一性。
当修改好之后,我们在 Tensorboard 显示的名字也会被修改
a = tf.constant(3.0, name="a")
b = tf.constant(4.0, name="b" )