标签:create nal isa protect ret 控制 not put case
TensorFlow 提供了一些操作和类,您可以使用它们来控制操作的执行,并向图表添加条件依赖项。tf.identity
tf.tuple
tf.group
tf.no_op
tf.count_up_to
tf.cond
tf.case
tf.while_loop
tf.identity
?
tf.identity(
? ? input,
? ? name=None
)
返回和输入大小与内容一致的tensor
参数:
input: 一个Tensor.
name: 操作名字(可选)
这个方法的源码:
def identity(input, name=None): # pylint: disable=redefined-builtin
r"""Return a tensor with the same shape and contents as input.
Args:
input: A `Tensor`.
name: A name for the operation (optional).
Returns:
A `Tensor`. Has the same type as `input`.
"""
if context.executing_eagerly():
input = ops.convert_to_tensor(input)
in_device = input.device
# TODO(ashankar): Does ‘identity‘ need to invoke execution callbacks?
context_device = context.context().device_name
if not context_device:
context_device = "/job:localhost/replica:0/task:0/device:CPU:0"
if context_device != in_device:
return input._copy() # pylint: disable=protected-access
return input
else:
return gen_array_ops.identity(input, name=name)
从源码可看出,input的device会与context.context()相比较,如果相同,则直接返回input,如果不相同则返回disable=protected-access权限的input的copy。
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Mon Aug 27 11:16:32 2018
@author: myhaspl
"""
import tensorflow as tf
x1 = tf.constant(2.)
x2 = [1,2,3]
y1=tf.identity(x1,name="my_y1")
y2=tf.identity(x2,name="my_y")
sess=tf.Session()
with sess:
print sess.run(y1)
print sess.run(y2)
2.0
[1 2 3]
标签:create nal isa protect ret 控制 not put case
原文地址:http://blog.51cto.com/13959448/2325222