tensorflow编程: Control Flow

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

如果 xy都为 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]]

Debugging Operations



  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值