TensorFlow Stack and unstack detailed introduction __tensorflow

Source: Internet
Author: User
Turn from: https://www.toutiao.com/i6463423755296178702/

Tf.stack ()

Tf.stack (values, axis=0, name= ' stack ')
Converts a tensor array of one dimension R to a r+1 tensor with the specified axis axis. Increases a dimension by assigning a set of tensor to a specified axis.

Suppose the tensor array to be converted is of the length n, where the shape of each tensor is (A, B, C).
If the axis is axis=0, the shape of the transformed tensor is (N, A, B, C).
If the axis is Axis=1, the shape of the transformed tensor is (A, N, B, C).
If the axis is axis=2, the shape of the transformed tensor is (A, B, N, C). The other case is the analogy.

Examples are as follows:
' X ' is [1, 4], Shape is (2), dimension is 1
' Y ' is [2, 5], Shape is (2), dimension is 1
' Z ' is [3, 6], Shape is (2), dimension is 1
Stack ([x, Y, z]) => [[1, 4], [2, 5], [3, 6]] # Axis's value defaults to 0. The output shape is (3, 2)
Stack ([x, Y, z], Axis=1) => [[1, 2, 3], [4, 5, 6]] The value of axis is 1. The output shape is (2, 3)

' X ' is [[1,1,1,1],[2,2,2,2],[3,3,3,3]], the shape is (3,4), the dimension is 2
' Y ' is [[4,4,4,4],[5,5,5,5],[6,6,6,6]], the shape is (3,4), the dimension is 2
Stack ([x,y]) => [[[[1,1,1,1],[2,2,2,2],[3,3,3,3]]], [[4,4,4,4],[5,5,5,5],[6,6,6,6]]] # axis defaults to 0. The output shape is (2, 3, 4)
The value of stack ([X,y],axis=1) => [[[1,1,1,1],[4,4,4,4]],[[2,2,2,2],[5,5,5,5]],[[3,3,3,3],[6,6,6,6]]] # axis is 1. The output shape is (3, 2, 4)
Stack ([x,y],axis=2) => [[1,4],[1,4],[1,4],[1,4]],[[2,5],[2,5],[2,5],[2,5]],[[3,6],[3,6],[3,6],[3,6]]]# Axis has a value of 2. The output shape is (3, 4, 2)

Axis is able to understand that stack is a dimension that increases the tensor of a group of identical shapes. Axis is the tensor in which axis-specified dimensions are replaced with all of these tensor arrays. For example, the axis=2 is specified in the 2nd dimension, and the original element is replaced with the elements of the entire tensor array, from (A, B, c) to (A, B, N, C).

Parameters:
Values: An array of tensor arrays with the same shape and data type.
Axis: An integer that is centered around shaft axis. The default is the first dimension, axis=0. Supports negative numbers. Value range is [-(r+1), r+1)
Name: This operation is named (optional)
Returns: Tensor after being raised by one dimension
Exception: ValueError: If axis axis goes out of range [-(r+1), r+1). Tf.unstack ()

Tf.unstack (value, Num=none, axis=0, name= ' Unstack ')
Converts a tensor array of one dimension R to a R-1 tensor with the specified axis axis. Reduces a dimension by a set of tensor to a specified axis. Just as opposed to stack ().

Splits the tensor value into a number of num tensor arrays. If NUM is not specified, it is specified according to the shape of the tensor value. If Value.shape[axis] does not exist, the ValueError exception is thrown.


If the shape of a tensor is (A, B, C, D).
if Axis = = 0, the output tensor is value[i,:,:,:],i takes a value of [0,a), and the shape of the tensor for each output is (b,c,d).
if Axis = = 1, the output tensor is value[:, I,:,:],i takes a value of [0,b), and the shape of the tensor for each output is (a,c,d).
if Axis = = 2, the output tensor is value[:,:, I,:],i takes a value of [0,c), and the shape of the tensor for each output is (a,b,d). by analogy.

Examples are as follows:
' X ' is [[1,1,1,1],[2,2,2,2],[3,3,3,3]] # shape is (3,4) with a dimension of 2
Unstack (x,axis=0) => with the specified dimension 0 as an axis, to 3 shapes (4) tensor [1,1,1,1],[2,2,2,2],[3,3,3,3]
Unstack (X,axis=1) => with the specified dimension 1 as an axis, to 4 shapes (3) tensor [1,2,3],[1,2,3],[1,2,4],[1,2,3]

Axis can understand this: Unstack is an array of tensor that is to reduce one tensor to a lower dimension. Axis is the dimension specified by axis, which is replaced by the data in the same dimension as all of this tensor.

Parameter:
Value: A tensor that is greater than 0 for a dimension that will be reduced.
Num: Integer. The length of the specified dimension axis. If set to None (default), the value is automatically evaluated.
Axis: Integer. The dimension that is specified with Axis axis is the first dimension, axis=0, that is the default. Supports negative numbers. The value range is [R, R]
Name: The name of this action (optional)
Returns:
An array of tensor values that is reduced from the dimension of tensor value.
Exception:
ValueError: If num is not specified and cannot be obtained.
ValueError: If axis goes out of range [-R, R].

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.