深入浅出TensorFlow2函数——tf.shape
分类目录:《深入浅出TensorFlow2函数》总目录
语法
tf.shape(input, out_type=tf.dtypes.int32, name=None)
参数
input:tf.Tensor或tf.SparseTensorout_type:[可选] 操作的指定输出类型(int32或int64)。默认为tf.int32name:[可选] 操作的名称
返回值
一个表示input的tf.Tensor,对于标量输入,返回tf.Tensor的形状为(0,),其值为空向量[]。
实例
输入:
tf.shape(tf.constant([1,2,3]))
输出:
<tf.Tensor: shape=(1,), dtype=int32, numpy=array([3])>
函数实现
@tf_export("shape", v1=[])
@dispatch.add_dispatch_support
def shape_v2(input, out_type=dtypes.int32, name=None):# pylint: disable=redefined-builtin"""Returns a tensor containing the shape of the input tensor.See also `tf.size`, `tf.rank`.`tf.shape` returns a 1-D integer tensor representing the shape of `input`.For a scalar input, the tensor returned has a shape of (0,) and its value isthe empty vector (i.e. []).For example:>>> tf.shape(1.)>>> t = tf.constant([[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]])>>> tf.shape(t)Note: When using symbolic tensors, such as when using the Keras API,tf.shape() will return the shape of the symbolic tensor.>>> a = tf.keras.layers.Input((None, 10))>>> tf.shape(a)<... shape=(3,) dtype=int32...>In these cases, using `tf.Tensor.shape` will return more informative results.>>> a.shapeTensorShape([None, None, 10])(The first `None` represents the as yet unknown batch size.)`tf.shape` and `Tensor.shape` should be identical in eager mode. Within`tf.function` or within a `compat.v1` context, not all dimensions may beknown until execution time. Hence, when defining custom layers and modelsfor graph mode, prefer the dynamic `tf.shape(x)` over the static `x.shape`.Args:input: A `Tensor` or `SparseTensor`.out_type: (Optional) The specified output type of the operation (`int32` or`int64`). Defaults to `tf.int32`.name: A name for the operation (optional).Returns:A `Tensor` of type `out_type`.""" return shape(input, name, out_type)@tf_export(v1=["shape"])
@dispatch.add_dispatch_support
def shape(input, name=None, out_type=dtypes.int32):return shape_internal(input, name, optimize=True, out_type=out_type)
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
