tf.reshape (API r1.3)
https://github.com/tensorflow/docs/tree/r1.3/site/en/api_docs/api_docs/python/tf
site/en/api_docs/api_docs/python/tf/reshape.md
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.
給定 tensor,這個操作返回一個張量,它與帶有形狀 shape 的 tensor 具有相同的值。
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.
如果 shape
的一個分量是特殊值 -1,則計算該維度的大小,以使總大小保持不變。特別地情況爲,一個 [-1] 維的 shape
整個矩陣平鋪成 1 維。至多能有一個 shape
的分量可以是 -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
.
如果 shape
是 1-D 或更高,則操作返回形狀爲 shape
的張量,其填充爲 tensor
的值。在這種情況下,隱含的 shape
元素數量必須與 tensor
元素數量相同。
1. 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). (操作的名稱 (可選)。)
2. Returns
A Tensor. Has the same type as tensor.
該操作返回一個 Tensor。與 tensor 具有相同的類型。
shape 是一個張量,其中的一個元素可以是 -1。-1 表示不指定這一維度的大小,函數自動計算,但列表中只能存在一個 -1。
reshape 變換矩陣按照最簡單的理解就是:
reshape(M, shape) == reshape(M, [-1]) => reshape(M, shape)
先將矩陣 M 變爲一維矩陣,然後再對一維矩陣的形式根據 shape 進行構造。
3. Example
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import print_function
from __future__ import division
import os
import sys
import numpy as np
import tensorflow as tf
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
current_directory = os.path.dirname(os.path.abspath(__file__))
print(16 * "++--")
print("current_directory:", current_directory)
print(16 * "++--")
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:", input_x.shape)
print('\n')
output_reshape = sess.run(y_reshape)
print("output_reshape.shape:", output_reshape.shape)
print("output_reshape:", output_reshape)
print('\n')
output_reshape_slice = sess.run(y_reshape_slice)
print("output_reshape_slice.shape:", output_reshape_slice.shape)
print("output_reshape_slice:", output_reshape_slice)
/usr/bin/python2.7 /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow/yongqiang.py
++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--
current_directory: /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow
++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--
2019-08-15 19:09:35.722959: I tensorflow/core/platform/cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX AVX2 FMA
2019-08-15 19:09:35.804714: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:892] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2019-08-15 19:09:35.804993: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1030] Found device 0 with properties:
name: GeForce GTX 1080 major: 6 minor: 1 memoryClockRate(GHz): 1.7335
pciBusID: 0000:01:00.0
totalMemory: 7.92GiB freeMemory: 7.43GiB
2019-08-15 19:09:35.805004: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1120] Creating TensorFlow device (/device:GPU:0) -> (device: 0, name: GeForce GTX 1080, pci bus id: 0000:01:00.0, compute capability: 6.1)
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