tf.reshape (API r1.3)

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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章