您的位置:首页 > 其它

tf.reshape (API r1.3)

2017-11-10 20:59 267 查看

tf.reshape (API r1.3)

1. tf.reshape

reshape(
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. 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: