tf.reshape (API r1.3)
2017-11-10 20:59
267 查看
tf.reshape (API r1.3)
1. tf.reshapereshape( tensor, shape, name=None )
Defined in tensorflow/python/ops/gen_array_ops.py.
See the guide: Tensor Transformations > Shapes and Shaping
Reshapes a tensor.
Given tensor, this operation returns a tensor that has the same values as tensor with shape shape.
If one component of shape is the special value -1, the size of that dimension 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 can be -1.
If shape is 1-D or higher, then the operation returns a tensor with shape shape filled with the values of tensor. In this case, the number of elements implied by shape must be the same as the number of elements in tensor.
For example:
# tensor 't' is [1, 2, 3, 4, 5, 6, 7, 8, 9] # tensor 't' has shape [9] reshape(t, [3, 3]) ==> [[1, 2, 3], [4, 5, 6], [7, 8, 9]] # tensor 't' is [[[1, 1], [2, 2]], # [[3, 3], [4, 4]]] # tensor 't' has shape [2, 2, 2] reshape(t, [2, 4]) ==> [[1, 1, 2, 2], [3, 3, 4, 4]] # tensor 't' is [[[1, 1, 1], # [2, 2, 2]], # [[3, 3, 3], # [4, 4, 4]], # [[5, 5, 5], # [6, 6, 6]]] # tensor 't' has shape [3, 2, 3] # pass '[-1]' to flatten 't' reshape(t, [-1]) ==> [1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6] # -1 can also be used to infer the shape # -1 is inferred to be 9: reshape(t, [2, -1]) ==> [[1, 1, 1, 2, 2, 2, 3, 3, 3], [4, 4, 4, 5, 5, 5, 6, 6, 6]] # -1 is inferred to be 2: reshape(t, [-1, 9]) ==> [[1, 1, 1, 2, 2, 2, 3, 3, 3], [4, 4, 4, 5, 5, 5, 6, 6, 6]] # -1 is inferred to be 3: reshape(t, [ 2, -1, 3]) ==> [[[1, 1, 1], [2, 2, 2], [3, 3, 3]], [[4, 4, 4], [5, 5, 5], [6, 6, 6]]] # tensor 't' is [7] # shape `[]` reshapes to a scalar reshape(t, []) ==> 7
Args:
tensor: A Tensor.
shape: A Tensor. Must be one of the following types: int32, int64. Defines the shape of the output tensor.
name: A name for the operation (optional).
Returns:
A Tensor. Has the same type as tensor.
2. example 1
import tensorflow as tf imp 4000 ort numpy as np x_anchor = tf.constant([[[0, 1], [2, 3], [4, 5], [6, 7], [8, 9], [10, 11]]], dtype=np.float32) y_anchor102 = tf.transpose(x_anchor, perm=[1, 0, 2]) y_anchor_reshape = tf.reshape(y_anchor102, [6 * 1, 2]) with tf.Session() as sess: input_anchor = sess.run(x_anchor) print("input_anchor.shape:") print(input_anchor.shape) print('\n') output_anchor102 = sess.run(y_anchor102) print("output_anchor102.shape:") print(output_anchor102.shape) print('\n') print("output_anchor102:") print(output_anchor102) print('\n') output_y_anchor_reshape = sess.run(y_anchor_reshape) print("output_y_anchor_reshape.shape:") print(output_y_anchor_reshape.shape) print('\n') print("output_y_anchor_reshape:") print(output_y_anchor_reshape)
output:
input_anchor.shape: (1, 6, 2) output_anchor102.shape: (6, 1, 2) output_anchor102: [[[ 0. 1.]] [[ 2. 3.]] [[ 4. 5.]] [[ 6. 7.]] [[ 8. 9.]] [[ 10. 11.]]] output_y_anchor_reshape.shape: (6, 2) output_y_anchor_reshape: [[ 0. 1.] [ 2. 3.] [ 4. 5.] [ 6. 7.] [ 8. 9.] [ 10. 11.]] Process finished with exit code 0
shape是一个张量,其中的一个元素可以是-1。-1表示不指定这一维度的大小,函数自动计算,但列表中只能存在一个-1。
shape变换矩阵按照最简单的理解就是:
reshape(M, shape) => reshape(M, [-1]) => reshape(M, shape)
先将矩阵M变为一维矩阵,然后再对一维矩阵的形式根据shape进行构造。
3. example 2
import tensorflow as tf import numpy as np x = tf.constant([[[0, 1], [2, 3], [4, 5], [6, 7], [8, 9], [10, 11]]], dtype=np.float32) y_reshape = tf.reshape(x, [12]) y_reshape_slice = tf.reshape(x, [12])[0:3] with tf.Session() as sess: input_x = sess.run(x) print("input_x.shape:") print(input_x.shape) print('\n') output_reshape = sess.run(y_reshape) print("output_reshape.shape:") print(output_reshape.shape) print('\n') print("output_reshape:") print(output_reshape) print('\n') output_reshape_slice = sess.run(y_reshape_slice) print("output_reshape_slice.shape:") print(output_reshape_slice.shape) print('\n') print("output_reshape_slice:") print(output_reshape_slice)
output:
input_x.shape: (1, 6, 2) output_reshape.shape: (12,) output_reshape: [ 0. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11.] output_reshape_slice.shape: (3,) output_reshape_slice: [ 0. 1. 2.] Process finished with exit code 0
4.
参考文献
1. http://www.jianshu.com/p/689b5b5b46d8
2.
相关文章推荐
- tf.concat (API r1.3)
- tf.matmul (API r1.3)
- tf.cast (API r1.3)
- tf.split (API r1.3)
- tf.argmax (API r1.3)
- tf.reduce_sum (API r1.3)
- 第一阶段-入门详细图文讲解tensorflow1.4 API-tf.reshape
- TensorFlow Lite demo——就是为嵌入式设备而存在的,底层调用NDK神经网络API,注意其使用的tf model需要转换下,同时提供java和C++ API,无法使用tflite的见后
- 180224 tensorflow之tf.reshape()与tf.concat()应用举例
- 学习笔记TF048:TensorFlow 系统架构、设计理念、编程模型、API、作用域、批标准化、神经元函数优化
- pytorch的tf.reshape
- jQuery 1.3 API 在线参考文档中文版
- 深度学习笔记——深度学习框架TensorFlow(四)[高级API tf.contrib.learn]
- TensorFlow 学习(七) — 常用函数 api、tf.nn 库
- tensorflow_api_3:tf.placeholder( )
- tensorflow API: tf.unsorted_segment_sum
- easyUI 1.3 中文 API
- jQuery 1.3 API 参考文档中文版
- JSR 206: Java API for XML Processing (JAXP) 1.3
- TensorFlow的reshape操作 tf.reshape