Control Flow Operations
tf.identity
复制tensor
tf.identity (input, name=None)
import tensorflow as tf
t = tf.constant(value=[[1, 1, 1], [2, 2, 2]], dtype=tf.int32)
a1 = t
a2 = tf.identity(t)
print a1
assert a1 == t
print a2
assert a2 == t
经验证,a1 = t
得到的是 t,a2 = tf.identity(t)
得到的不是 t ,只是 t 的副本。这样有利于用副本进行运算而不引起 原tensor
的数值变化。
Tensor("Const:0", shape=(2, 3), dtype=int32)
Traceback (most recent call last):
Tensor("Identity:0", shape=(2, 3), dtype=int32)
File "/home/user/Desktop/test/1.py", line 10, in <module>
assert a2 == t
AssertionError
tf.tuple
将多个tensor放入一个tuple中并返回。
tf.tuple (tensors, name=None, control_inputs=None)
import tensorflow as tf
t1 = tf.constant(value=[[1, 1], [2, 2]], dtype=tf.int32)
t2 = tf.constant(value=[[3, 3], [4, 4]], dtype=tf.int32)
t3 = tf.constant(value=[[5, 5], [6, 6]], dtype=tf.int32)
tuple_t = tf.tuple([t1, t2, t3])
group_t = tf.group(t1, t2, t3)
with tf.Session() as sess:
print tuple_t
print sess.run(tuple_t)
print '\n----------\n'
print group_t
print sess.run(group_t)
[<tf.Tensor 'tuple/control_dependency:0' shape=(2, 2) dtype=int32>, <tf.Tensor 'tuple/control_dependency_1:0' shape=(2, 2) dtype=int32>, <tf.Tensor 'tuple/control_dependency_2:0' shape=(2, 2) dtype=int32>]
[array([[1, 1],
[2, 2]], dtype=int32), array([[3, 3],
[4, 4]], dtype=int32), array([[5, 5],
[6, 6]], dtype=int32)]
----------
name: "group_deps"
op: "NoOp"
input: "^Const"
input: "^Const_1"
input: "^Const_2"
None
tf.group
将多个op放入同一个op中并返回该op。
# 代码见 tf.tuple
Logical Operators
tf.logical_and
逻辑与
tf.logical_and (x, y, name=None)
import tensorflow as tf
x = tf.convert_to_tensor([True, True, True, False, False])
y = tf.convert_to_tensor([True, False, True, False, True])
logical_and = tf.logical_and(x=x, y=y)
with tf.Session() as sess:
print logical_and
print sess.run(logical_and)
Tensor("LogicalAnd:0", shape=(5,), dtype=bool)
[ True False True False False]
tf.logical_not
逻辑非
tf.logical_not (x, name=None)
import tensorflow as tf
x = tf.convert_to_tensor([True, True, True, False, False])
logical_not = tf.logical_not(x=x)
with tf.Session() as sess:
print logical_not
print sess.run(logical_not)
Tensor("LogicalNot:0", shape=(5,), dtype=bool)
[False False False True True]
tf.logical_or
tf.logical_xor
Comparison Operators
tf.equal
只 比较 俩tensor 的 value部分 是否相等,返回 bool型 tensor。
tf.equal (x, y, name=None)
import tensorflow as tf
x = tf.convert_to_tensor([1, 2, 3])
y = tf.identity(x)
with tf.Session() as sess:
print tf.equal(x, y)
print sess.run(tf.equal(x, y))
Tensor("Equal:0", shape=(3,), dtype=bool)
[ True True True]
tf.not_equal
只 比较 俩tensor 的 value部分 是否不等,返回 bool型 tensor。
tf.not_equal (x, y, name=None)
tf.less
tensor x 是否小于 tensor y,返回 bool型 tensor。
tf.less (x, y, name=None)
tf.less_equal
tensor x 是否小于等于 tensor y,返回 bool型 tensor。
tf.less_equal (x, y, name=None)
tf.greater
tensor x 是否大于 tensor y,返回 bool型 tensor。
tf.greater (x, y, name=None)
tf.greater_equal
tensor x 是否大于等于 tensor y,返回 bool型 tensor。
tf.greater_equal (x, y, name=None)
tf.where
如果 x
和 y
都为 None
,则返回 tensor condition
中的 bool值
为 True
的坐标列表。
tf.where (condition, x=None, y=None, name=None)
import tensorflow as tf
x = tf.convert_to_tensor([[True, True, True],
[False, True, False],
[False, False, True]])
y = tf.where(x)
with tf.Session() as sess:
print y
print sess.run(y)
Tensor("Where:0", shape=(?, 2), dtype=int64)
# 从返回结果可知,第0行(的0、1、2)、第1行(的1)、第2行(的2)bool值为True
[[0 0]
[0 1]
[0 2]
[1 1]
[2 2]]
import tensorflow as tf
x = tf.convert_to_tensor([[1.0, 1.1],
[0.0, 0.9]])
with tf.Session() as sess:
print(sess.run(tf.where(tf.greater(x, 0.9))))
[[0 0]
[0 1]]