分类目录:《深入浅出TensorFlow2函数》总目录


语法

tf.reshape(tensor, shape, name=None
)

参数

返回值

返回一个新的形状为shapetf.Tensor且具有与tensor以同样的顺序和相同的值。

实例

输入:

t1 = [[1, 2, 3],[4, 5, 6]]
print(tf.shape(t1).numpy())        # [2 3]
t2 = tf.reshape(t1, [6])
t2        #<tf.Tensor: shape=(6,), dtype=int32, numpy=array([1, 2, 3, 4, 5, 6], dtype=int32)>
tf.reshape(t2, [3, 2])        # <tf.Tensor: shape=(3, 2), dtype=int32, numpy= array([[1, 2], [3, 4], [5, 6]], dtype=int32)>

如果shape的一个参数为是-1,则计算该维度的大小,使总大小保持不变。特别是,若shape[-1],则将tensor展平为一维。shape的参数最多只能有一个-1。此外,若t为仅含一个元素的tensor,则tf.reshape(t, [])t转变为一个标量。

函数实现

@tf_export("reshape", v1=["reshape", "manip.reshape"])
@dispatch.add_dispatch_support
def reshape(tensor, shape, name=None):  # pylint: disable=redefined-outer-namer"""Reshapes a tensor.Given `tensor`, this operation returns a new `tf.Tensor` that has the samevalues as `tensor` in the same order, except with a new shape given by`shape`.>>> t1 = [[1, 2, 3],...       [4, 5, 6]]>>> print(tf.shape(t1).numpy())[2 3]>>> t2 = tf.reshape(t1, [6])>>> t2<tf.Tensor: shape=(6,), dtype=int32,numpy=array([1, 2, 3, 4, 5, 6], dtype=int32)>>>> tf.reshape(t2, [3, 2])<tf.Tensor: shape=(3, 2), dtype=int32, numpy=array([[1, 2],[3, 4],[5, 6]], dtype=int32)>The `tf.reshape` does not change the order of or the total number of elementsin the tensor, and so it can reuse the underlying data buffer. This makes ita fast operation independent of how big of a tensor it is operating on.>>> tf.reshape([1, 2, 3], [2, 2])Traceback (most recent call last):...InvalidArgumentError: Input to reshape is a tensor with 3 values, but therequested shape has 4To instead reorder the data to rearrange the dimensions of a tensor, see`tf.transpose`.>>> t = [[1, 2, 3],...      [4, 5, 6]]>>> tf.reshape(t, [3, 2]).numpy()array([[1, 2],[3, 4],[5, 6]], dtype=int32)>>> tf.transpose(t, perm=[1, 0]).numpy()array([[1, 4],[2, 5],[3, 6]], dtype=int32)If one component of `shape` is the special value -1, the size of thatdimension is computed so that the total size remains constant.  In particular,a `shape` of `[-1]` flattens into 1-D.  At most one component of `shape` canbe -1.>>> t = [[1, 2, 3],...      [4, 5, 6]]>>> tf.reshape(t, [-1])<tf.Tensor: shape=(6,), dtype=int32,numpy=array([1, 2, 3, 4, 5, 6], dtype=int32)>>>> tf.reshape(t, [3, -1])<tf.Tensor: shape=(3, 2), dtype=int32, numpy=array([[1, 2],[3, 4],[5, 6]], dtype=int32)>>>> tf.reshape(t, [-1, 2])<tf.Tensor: shape=(3, 2), dtype=int32, numpy=array([[1, 2],[3, 4],[5, 6]], dtype=int32)>`tf.reshape(t, [])` reshapes a tensor `t` with one element to a scalar.>>> tf.reshape([7], []).numpy()7More examples:>>> t = [1, 2, 3, 4, 5, 6, 7, 8, 9]>>> print(tf.shape(t).numpy())[9]>>> tf.reshape(t, [3, 3])<tf.Tensor: shape=(3, 3), dtype=int32, numpy=array([[1, 2, 3],[4, 5, 6],[7, 8, 9]], dtype=int32)>>>> t = [[[1, 1], [2, 2]],...      [[3, 3], [4, 4]]]>>> print(tf.shape(t).numpy())[2 2 2]>>> tf.reshape(t, [2, 4])<tf.Tensor: shape=(2, 4), dtype=int32, numpy=array([[1, 1, 2, 2],[3, 3, 4, 4]], dtype=int32)>>>> t = [[[1, 1, 1],...       [2, 2, 2]],...      [[3, 3, 3],...       [4, 4, 4]],...      [[5, 5, 5],...       [6, 6, 6]]]>>> print(tf.shape(t).numpy())[3 2 3]>>> # Pass '[-1]' to flatten 't'.>>> tf.reshape(t, [-1])<tf.Tensor: shape=(18,), dtype=int32,numpy=array([1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6],dtype=int32)>>>> # -- Using -1 to infer the shape -->>> # Here -1 is inferred to be 9:>>> tf.reshape(t, [2, -1])<tf.Tensor: shape=(2, 9), dtype=int32, numpy=array([[1, 1, 1, 2, 2, 2, 3, 3, 3],[4, 4, 4, 5, 5, 5, 6, 6, 6]], dtype=int32)>>>> # -1 is inferred to be 2:>>> tf.reshape(t, [-1, 9])<tf.Tensor: shape=(2, 9), dtype=int32, numpy=array([[1, 1, 1, 2, 2, 2, 3, 3, 3],[4, 4, 4, 5, 5, 5, 6, 6, 6]], dtype=int32)>>>> # -1 is inferred to be 3:>>> tf.reshape(t, [ 2, -1, 3])<tf.Tensor: shape=(2, 3, 3), dtype=int32, numpy=array([[[1, 1, 1],[2, 2, 2],[3, 3, 3]],[[4, 4, 4],[5, 5, 5],[6, 6, 6]]], dtype=int32)>Args:tensor: A `Tensor`.shape: A `Tensor`. Must be one of the following types: `int32`, `int64`.Defines the shape of the output tensor.name: Optional string. A name for the operation.Returns:A `Tensor`. Has the same type as `tensor`."""result = gen_array_ops.reshape(tensor, shape, name)tensor_util.maybe_set_static_shape(result, shape)return result

深入浅出TensorFlow2函数——tf.reshape相关推荐

  1. 深入浅出TensorFlow2函数——tf.constant

    分类目录:<深入浅出TensorFlow2函数>总目录 相关文章: · 深入浅出TensorFlow2函数--tf.constant · 深入浅出TensorFlow2函数--tf.Ten ...

  2. 深入浅出TensorFlow2函数——tf.keras.layers.Dense

    分类目录:<深入浅出TensorFlow2函数>总目录 tf.keras.layers.Dense实现操作:output = activation(dot(input, kernel) + ...

  3. 深入浅出TensorFlow2函数——tf.data.Dataset.batch

    分类目录:<深入浅出TensorFlow2函数>总目录 语法 batch(batch_size, drop_remainder=False, num_parallel_calls=None ...

  4. 深入浅出TensorFlow2函数——tf.keras.layers.Embedding

    分类目录:<深入浅出TensorFlow2函数>总目录 语法 tf.keras.layers.Embedding(input_dim, output_dim, embeddings_ini ...

  5. 深入浅出PaddlePaddle函数——paddle.Tensor

    分类目录:<深入浅出PaddlePaddle函数>总目录 相关文章: · 深入浅出TensorFlow2函数--tf.Tensor · 深入浅出Pytorch函数--torch.Tenso ...

  6. 深入浅出Pytorch函数——torch.arange

    分类目录:<深入浅出Pytorch函数>总目录 相关文章: · 深入浅出TensorFlow2函数--tf.range · 深入浅出Pytorch函数--torch.arange · 深入 ...

  7. 深入浅出Pytorch函数——torch.exp

    分类目录:<深入浅出Pytorch函数>总目录 相关文章: · 深入浅出TensorFlow2函数--tf.exp · 深入浅出TensorFlow2函数--tf.math.exp · 深 ...

  8. 深入浅出PaddlePaddle函数——paddle.arange

    分类目录:<深入浅出PaddlePaddle函数>总目录 相关文章: · 深入浅出TensorFlow2函数--tf.range · 深入浅出Pytorch函数--torch.arange ...

  9. 深入浅出PaddlePaddle函数——paddle.numel

    分类目录:<深入浅出PaddlePaddle函数>总目录 相关文章: · 深入浅出TensorFlow2函数--tf.size · 深入浅出Pytorch函数--torch.numel · ...

最新文章

  1. 介绍两个好玩的和Github相关的Chrome扩展
  2. UVA - 10603 Fill(BFS求最小值问题)
  3. 我的机器学习入门之路(下)——知识图谱、推荐、广告
  4. win32汇编创建线程简单Demo
  5. Spring Boot-使用Spring Initializer快速创建Spring Boot项目
  6. Java实现各种排序算法
  7. 电脑任务管理器快捷键_电脑知识小常识
  8. 如何查看Linux系统的带宽流量
  9. python和java哪个好-Python和JAVA的就业前景哪个好点?
  10. 剑指offer——面试题14:调整数组顺序使奇数位于偶数前面
  11. 《统计会犯错——如何避免数据分析中的统计陷阱》—第1章构建置信区间
  12. Lightweight OpenPose
  13. 【MOOC测试】数学模型
  14. gif图片解析与生成(GIF+文字动效)
  15. dp主机_miniDP转DP和type-C转DP连接线上机简单测评
  16. 码code | 拒绝996,不用服务器也能高效开发小游戏
  17. 调用百度地图API实现动态走航路线图
  18. 交流异步电机的Modelica模型
  19. 抓取百度地图瓦片(离线GIS)
  20. .net 微信开发

热门文章

  1. POJ1704_Georgia and Bob_Nim游戏变型
  2. Angular中弹窗内如何分页及limit性能优化的思路
  3. 【C语言训练】委派任务
  4. 深度优先遍历(DFS)和广度优先遍历(BFS)
  5. 业务中立_反对网络中立性威胁开源社区的生存
  6. 【老生谈算法】matlab编写PSO算法及实例——PSO算法
  7. linux内核是否支持cgroup,如何检查我的Linux主机上是否有cgroup可用?
  8. WinDbg基础(3)Adplus参数设置
  9. 助力数字政府建设,中科三方构建域名安全保障体系
  10. 田忌赛马java lms_471.洲际赛上的田忌赛马