tensorflow 控制流操作,条件判断和循环操作
来源:互联网 发布:易经软件 知乎 编辑:程序博客网 时间:2024/05/29 17:06
Control flow operations: conditionals and loops
When building complex models such as recurrent neural networks you may need to control the flow of operations through conditionals and loops. In this section we introduce a number of commonly used control flow ops.
Let’s assume you want to decide whether to multiply to or add two given tensors based on a predicate. This can be simply implemented with tf.cond which acts as a python “if” function:
a = tf.constant(1)b = tf.constant(2)p = tf.constant(True)x = tf.cond(p, lambda: a + b, lambda: a * b)print(tf.Session().run(x))
Since the predicate is True in this case, the output would be the result of the addition, which is 3.
Most of the times when using TensorFlow you are using large tensors and want to perform operations in batch. A related conditional operation is tf.where, which like tf.cond takes a predicate, but selects the output based on the condition in batch.
a = tf.constant([1, 1])b = tf.constant([2, 2])p = tf.constant([True, False])x = tf.where(p, a + b, a * b)print(tf.Session().run(x))
This will return [3, 2].
Another widely used control flow operation is tf.while_loop. It allows building dynamic loops in TensorFlow that operate on sequences of variable length. Let’s see how we can generate Fibonacci sequence with tf.while_loops:
n = tf.constant(5)def cond(i, a, b): return i < ndef body(i, a, b): return i + 1, b, a + bi, a, b = tf.while_loop(cond, body, (2, 1, 1))print(tf.Session().run(b))
This will print 5. tf.while_loops takes a condition function, and a loop body function, in addition to initial values for loop variables. These loop variables are then updated by multiple calls to the body function until the condition returns false.
Now imagine we want to keep the whole series of Fibonacci sequence. We may update our body to keep a record of the history of current values:
n = tf.constant(5)def cond(i, a, b, c): return i < ndef body(i, a, b, c): return i + 1, b, a + b, tf.concat([c, [a + b]], 0)i, a, b, c = tf.while_loop(cond, body, (2, 1, 1, tf.constant([1, 1])))print(tf.Session().run(c))
Now if you try running this, TensorFlow will complain that the shape of the the fourth loop variable is changing. So you must make that explicit that it’s intentional:
i, a, b, c = tf.while_loop( cond, body, (2, 1, 1, tf.constant([1, 1])), shape_invariants=(tf.TensorShape([]), tf.TensorShape([]), tf.TensorShape([]), tf.TensorShape([None])))
This is not only getting ugly, but is also somewhat inefficient. Note that we are building a lot of intermediary tensors that we don’t use. TensorFlow has a better solution for this kind of growing arrays. Meet tf.TensorArray. Let’s do the same thing this time with tensor arrays:
n = tf.constant(5)c = tf.TensorArray(tf.int32, n)c = c.write(0, 1)c = c.write(1, 1)def cond(i, a, b, c): return i < ndef body(i, a, b, c): c = c.write(i, a + b) return i + 1, b, a + b, ci, a, b, c = tf.while_loop(cond, body, (2, 1, 1, c))c = c.stack()print(tf.Session().run(c))
TensorFlow while loops and tensor arrays are essential tools for building complex recurrent neural networks. As an exercise try implementing beam search using tf.while_loops. Can you make it more efficient with tensor arrays?
更多教程:http://www.tensorflownews.com/
- tensorflow 控制流操作,条件判断和循环操作
- PL/SQL中操作符、条件和循环控制
- Effective TensorFlow Chapter 8: 在TensorFlow中的控制流:条件语句和循环
- Swift中的条件操作和循环
- Swift学习之条件操作和循环
- 条件判断和循环
- 条件判断和循环
- 条件判断和循环
- 条件判断和循环
- Python入门笔记(4)_ 控制流:条件判断和循环语句
- Python_条件判断和循环
- python10:条件判断和循环
- shell循环和条件判断
- 3.条件判断和循环
- PowerShell 条件判断和循环
- python 条件判断和循环
- Python(条件判断和循环)
- Python-条件判断和循环
- Dropwizard与Spring Boot比较
- Linux下利用phpize安装memcashe的php源码扩展包
- 需求文档到底谁来写
- Java动态代理的两种实现方法
- java运行时数据区划分
- tensorflow 控制流操作,条件判断和循环操作
- 详解this指针--为什么空的对象指针可以调用成员函数
- QT HTTP 服务 代理服务器没有响应
- 通过栈实现算术表达式的计算
- ES6 — 新增关键字let、const
- 剑指offer-15:单链表中倒数第k个结点
- UVA 11419 SAM I AM 网络流
- 微服务架构设计
- PHP 时间/日期/星期的方法及解决....