TensorFlow教程之API DOC 6.3.4. CONTROL FLOW OPS

简介:

本文档为TensorFlow参考文档,本转载已得到TensorFlow中文社区授权。


Control Flow

Note: Functions taking Tensor arguments can also take anything accepted by tf.convert_to_tensor.

Contents

Control Flow

Control Flow Operations

TensorFlow provides several operations and classes that you can use to control the execution of operations and add conditional dependencies to your graph.


tf.identity(input, name=None)

Return a tensor with the same shape and contents as the input tensor or value.

Args:
  • input: A Tensor.
  • name: A name for the operation (optional).
Returns:

Tensor. Has the same type as input.


tf.tuple(tensors, name=None, control_inputs=None)

Group tensors together.

This creates a tuple of tensors with the same values as the tensors argument, except that the value of each tensor is only returned after the values of all tensors have been computed.

control_inputs contains additional ops that have to finish before this op finishes, but whose outputs are not returned.

This can be used as a "join" mechanism for parallel computations: all the argument tensors can be computed in parallel, but the values of any tensor returned by tuple are only available after all the parallel computations are done.

See also group and with_dependencies.

Args:
  • tensors: A list of Tensors or IndexedSlices, some entries can be None.
  • name: (optional) A name to use as a name_scope for the operation.
  • control_inputs: List of additional ops to finish before returning.
Returns:

Same as tensors.

Raises:
  • ValueError: If tensors does not contain any Tensor or IndexedSlices.

tf.group(*inputs, **kwargs)

Create an op that groups multiple operations.

When this op finishes, all ops in input have finished. This op has no output.

See also tuple and with_dependencies.

Args:
  • *inputs: One or more tensors to group.
  • **kwargs: Optional parameters to pass when constructing the NodeDef.
  • name: A name for this operation (optional).
Returns:

An Operation that executes all its inputs.

Raises:
  • ValueError: If an unknown keyword argument is provided, or if there are
           no inputs.
    

tf.no_op(name=None)

Does nothing. Only useful as a placeholder for control edges.

Args:
  • name: A name for the operation (optional).
Returns:

The created Operation.


tf.count_up_to(ref, limit, name=None)

Increments 'ref' until it reaches 'limit'.

This operation outputs "ref" after the update is done. This makes it easier to chain operations that need to use the updated value.

Args:
  • ref: A mutable Tensor. Must be one of the following types: int32int64. Should be from a scalar Variable node.
  • limit: An int. If incrementing ref would bring it above limit, instead generates an 'OutOfRange' error.
  • name: A name for the operation (optional).
Returns:

Tensor. Has the same type as ref. A copy of the input before increment. If nothing else modifies the input, the values produced will all be distinct.

Logical Operators

TensorFlow provides several operations that you can use to add logical operators to your graph.


tf.logical_and(x, y, name=None)

Returns the truth value of x AND y element-wise.

Args:
  • x: A Tensor of type bool.
  • y: A Tensor of type bool.
  • name: A name for the operation (optional).
Returns:

Tensor of type bool.


tf.logical_not(x, name=None)

Returns the truth value of NOT x element-wise.

Args:
  • x: A Tensor of type bool.
  • name: A name for the operation (optional).
Returns:

Tensor of type bool.


tf.logical_or(x, y, name=None)

Returns the truth value of x OR y element-wise.

Args:
  • x: A Tensor of type bool.
  • y: A Tensor of type bool.
  • name: A name for the operation (optional).
Returns:

Tensor of type bool.


tf.logical_xor(x, y, name='LogicalXor')

x ^ y = (x | y) & ~(x & y).

Comparison Operators

TensorFlow provides several operations that you can use to add comparison operators to your graph.


tf.equal(x, y, name=None)

Returns the truth value of (x == y) element-wise.

Args:
  • x: A Tensor. Must be one of the following types: float32float64int32int64complex64quint8qint8qint32.
  • y: A Tensor. Must have the same type as x.
  • name: A name for the operation (optional).
Returns:

Tensor of type bool.


tf.not_equal(x, y, name=None)

Returns the truth value of (x != y) element-wise.

Args:
  • x: A Tensor. Must be one of the following types: float32float64int32int64complex64quint8qint8qint32.
  • y: A Tensor. Must have the same type as x.
  • name: A name for the operation (optional).
Returns:

Tensor of type bool.


tf.less(x, y, name=None)

Returns the truth value of (x < y) element-wise.

Args:
  • x: A Tensor. Must be one of the following types: float32float64int32int64.
  • y: A Tensor. Must have the same type as x.
  • name: A name for the operation (optional).
Returns:

Tensor of type bool.


tf.less_equal(x, y, name=None)

Returns the truth value of (x <= y) element-wise.

Args:
  • x: A Tensor. Must be one of the following types: float32float64int32int64.
  • y: A Tensor. Must have the same type as x.
  • name: A name for the operation (optional).
Returns:

Tensor of type bool.


tf.greater(x, y, name=None)

Returns the truth value of (x > y) element-wise.

Args:
  • x: A Tensor. Must be one of the following types: float32float64int32int64.
  • y: A Tensor. Must have the same type as x.
  • name: A name for the operation (optional).
Returns:

Tensor of type bool.


tf.greater_equal(x, y, name=None)

Returns the truth value of (x >= y) element-wise.

Args:
  • x: A Tensor. Must be one of the following types: float32float64int32int64.
  • y: A Tensor. Must have the same type as x.
  • name: A name for the operation (optional).
Returns:

Tensor of type bool.


tf.select(condition, t, e, name=None)

Selects elements from t or e, depending on condition.

The conditiont, and e tensors must all have the same shape, and the output will also have that shape. The condition tensor acts as an element-wise mask that chooses, based on the value at each element, whether the corresponding element in the output should be taken from t (if true) or e (if false). For example:

For example:

# 'condition' tensor is [[True, False]
#                        [True, False]]
# 't' is [[1, 1],
#         [1, 1]]
# 'e' is [[2, 2],
#         [2, 2]]
select(condition, t, e) ==> [[1, 2],
                             [1, 2]]
Args:
  • condition: A Tensor of type bool.
  • t: A Tensor with the same shape as condition.
  • e: A Tensor with the same type and shape as t.
  • name: A name for the operation (optional).
Returns:

Tensor with the same type and shape as t and e.


tf.where(input, name=None)

Returns locations of true values in a boolean tensor.

This operation returns the coordinates of true elements in input. The coordinates are returned in a 2-D tensor where the first dimension (rows) represents the number of true elements, and the second dimension (columns) represents the coordinates of the true elements. Keep in mind, the shape of the output tensor can vary depending on how many true values there are in input. Indices are output in row-major order.

For example:

# 'input' tensor is [[True, False]
#                    [True, False]]
# 'input' has two true values, so output has two coordinates.
# 'input' has rank of 2, so coordinates have two indices.
where(input) ==> [[0, 0],
                  [1, 0]]

# `input` tensor is [[[True, False]
#                     [True, False]]
#                    [[False, True]
#                     [False, True]]
#                    [[False, False]
#                     [False, True]]]
# 'input' has 5 true values, so output has 5 coordinates.
# 'input' has rank of 3, so coordinates have three indices.
where(input) ==> [[0, 0, 0],
                  [0, 1, 0],
                  [1, 0, 1],
                  [1, 1, 1],
                  [2, 1, 1]]
Args:
  • input: A Tensor of type bool.
  • name: A name for the operation (optional).
Returns:

Tensor of type int64.

Debugging Operations

TensorFlow provides several operations that you can use to validate values and debug your graph.


tf.is_finite(x, name=None)

Returns which elements of x are finite.

Args:
  • x: A Tensor. Must be one of the following types: float32float64.
  • name: A name for the operation (optional).
Returns:

Tensor of type bool.


tf.is_inf(x, name=None)

Returns which elements of x are Inf.

Args:
  • x: A Tensor. Must be one of the following types: float32float64.
  • name: A name for the operation (optional).
Returns:

Tensor of type bool.


tf.is_nan(x, name=None)

Returns which elements of x are NaN.

Args:
  • x: A Tensor. Must be one of the following types: float32float64.
  • name: A name for the operation (optional).
Returns:

Tensor of type bool.


tf.verify_tensor_all_finite(t, msg, name=None)

Assert that the tensor does not contain any NaN's or Inf's.

Args:
  • t: Tensor to check.
  • msg: Message to log on failure.
  • name: A name for this operation (optional).
Returns:

Same tensor as t.


tf.check_numerics(tensor, message, name=None)

Checks a tensor for NaN and Inf values.

When run, reports an InvalidArgument error if tensor has any values that are not a number (NaN) or infinity (Inf). Otherwise, passes tensor as-is.

Args:
  • tensor: A Tensor. Must be one of the following types: float32float64.
  • message: A string. Prefix of the error message.
  • name: A name for the operation (optional).
Returns:

Tensor. Has the same type as tensor.


tf.add_check_numerics_ops()

Connect a check_numerics to every floating point tensor.

check_numerics operations themselves are added for each float or double tensor in the graph. For all ops in the graph, the check_numerics op for all of its (float or double) inputs is guaranteed to run before the check_numerics op on any of its outputs.

Returns:

group op depending on all check_numerics ops added.


tf.Assert(condition, data, summarize=None, name=None)

Asserts that the given condition is true.

If condition evaluates to false, print the list of tensors in datasummarize determines how many entries of the tensors to print.

Args:
  • condition: The condition to evaluate.
  • data: The tensors to print out when condition is false.
  • summarize: Print this many entries of each tensor.
  • name: A name for this operation (optional).

tf.Print(input_, data, message=None, first_n=None, summarize=None, name=None)

Prints a list of tensors.

This is an identity op with the side effect of printing data when evaluating.

Args:
  • input_: A tensor passed through this op.
  • data: A list of tensors to print out when op is evaluated.
  • message: A string, prefix of the error message.
  • first_n: Only log first_n number of times. Negative numbers log always;
        this is the default.
    
  • summarize: Only print this many entries of each tensor.
  • name: A name for the operation (optional).
Returns:

Same tensor as input_.

相关文章
|
2月前
|
数据采集 监控 安全
各种业务场景调用API代理的API接口教程
API代理的API接口在各种业务场景中具有广泛的应用,本文将介绍哪些业务场景可以使用API代理的API接口,并提供详细的调用教程和代码演示,同时,我们还将讨论在不同场景下使用API代理的API接口所带来的好处。
|
5月前
|
机器学习/深度学习 JSON JavaScript
图文讲解 Stable Diffusion API 调用教程
Stable Diffusion 是一个先进的深度学习模型,用于创造和修改图像。这个模型能够基于文本描述来生成图像,让机器理解和实现用户的创意。使用这项技术的关键在于掌握其 API,通过编程来操控图像生成的过程。
|
26天前
|
机器学习/深度学习 API TensorFlow
TensorFlow的高级API:tf.keras深度解析
【4月更文挑战第17天】本文深入解析了TensorFlow的高级API `tf.keras`,包括顺序模型和函数式API的模型构建,以及模型编译、训练、评估和预测的步骤。`tf.keras`结合了Keras的易用性和TensorFlow的性能,支持回调函数、模型保存与加载等高级特性,助力提升深度学习开发效率。
|
2月前
|
安全 API 数据安全/隐私保护
email api接口配置教程步骤详解
Email API是用于程序化访问邮件服务的工具,让开发者能集成邮件功能到应用中。配置Email API包括选择供应商(如SendGrid、Mailgun、AokSend),注册获取API密钥,配置API参数,及测试邮件发送。使用Email API能提升邮件发送的可靠性和效率,便于邮件管理及营销活动。AokSend支持大量验证码发送,适合高效邮件运营。
|
3月前
|
存储 负载均衡 API
部署大模型API的实战教程
部署大模型API的实战教程可以分为以下步骤:
|
3月前
|
机器学习/深度学习 人工智能 API
人工智能应用工程师技能提升系列2、——TensorFlow2——keras高级API训练神经网络模型
人工智能应用工程师技能提升系列2、——TensorFlow2——keras高级API训练神经网络模型
38 0
|
3天前
|
安全 API 开发者
智能体-Agent能力升级!新增Assistant API & Tools API服务接口
ModelScope-Agent是一个交互式创作空间,它支持LLM(Language Model)的扩展能力,例如工具调用(function calling)和知识检索(knowledge retrieval)。它已经对相关接口进行了开源,以提供更原子化的应用LLM能力。用户可以通过Modelscope-Agent上的不同代理(agent),结合自定义的LLM配置和消息,调用这些能力。
|
7天前
|
JSON 搜索推荐 数据挖掘
电商数据分析的利器:电商关键词搜索API接口(标题丨图片丨价格丨链接)
淘宝关键词搜索接口为电商领域的数据分析提供了丰富的数据源。通过有效利用这一接口,企业和研究人员可以更深入地洞察市场动态,优化营销策略,并提升用户体验。随着电商平台技术的不断进步,未来的API将更加智能和个性化,为电商行业带来更多的可能性。
|
14天前
|
存储 缓存 运维
DataWorks操作报错合集之DataWorks根据api,调用查询文件列表接口报错如何解决
DataWorks是阿里云提供的一站式大数据开发与治理平台,支持数据集成、数据开发、数据服务、数据质量管理、数据安全管理等全流程数据处理。在使用DataWorks过程中,可能会遇到各种操作报错。以下是一些常见的报错情况及其可能的原因和解决方法。
23 1
|
15天前
|
SQL 数据管理 API
数据管理DMS产品使用合集之阿里云DMS提供API接口来进行数据导出功能吗
阿里云数据管理DMS提供了全面的数据管理、数据库运维、数据安全、数据迁移与同步等功能,助力企业高效、安全地进行数据库管理和运维工作。以下是DMS产品使用合集的详细介绍。