"TensorFlow Learning" tf.split () and Tf.squeeze ()

Source: Internet
Author: User
Split (
    value,
    num_or_size_splits,
    axis=0,
    num=none,
    name= ' Split '
)

Input:
Value: tensor of the input
Num_or_size_splits: If it is an integer n, the input tensor is divided into N sub-tensor. If it is a tensor T, the input tensor is divided into Len (T) Sub tensor.
Axis: Default is 0, calculate Value.shape[axis], must be divisible by num_or_size_splits.

Example:

# num_or_size_splits is tensor t,len (T) is 3, so divided into 3 sub-tensor,
# axis 1, so value.shape[1] for 30,4+15+11 just for the split0
, Split1, Split2 = Tf.split (value, [4, 5, all], 1)

Tf.shape (split0)  # [, 4]
tf.shape (split1)  # [5, 15]
  tf.shape (Split2)  # [5, one]

# num_or_size_splits is an integer 3, divided into 3 tensor,value.shape[1] is 30, can be divisible by 3.
split0, split1, split2 = Tf.split (value, num_or_size_splits=3, Axis=1)
tf.shape (split0)  # [5, 10]

To give an example:

>>> A=np.reshape (Range), (4,2,3)) >>> a array ([[[[[[0], 1, 2], [3, 4 
        , 5]], [[6, 7, 8], [9, 10, 11], [[12, 13, 14], [15, 16, 17]], [[18, 19, 20],

[21, 22, 23]]) 
>>> SESS=TF.
InteractiveSession () # divides a into two tensor,a.shape (1) to 2, which can be divided evenly, without error. # The output should be 2 shape for [4,1,3] tensor >>> b= tf.split (a,2,1) >>> b [<TF. Tensor ' split:0 ' shape= (4, 1, 3) Dtype=int32>, <TF. 

       Tensor ' Split:1 ' shape= (4, 1, 3) Dtype=int32>] >>> Sess.run (b) [Array ([[[[[0, 1, 2]], [[6, 7, 8]],

       [[[5], [[]]], [[[]], Array ([[[3, 4,]], [[9, 10, 11]], [[15, 16, 17]],
[[21, 22, 23]]] >>> c= Tf.split (a,2,0) # a.shape (0) is 4, divisible by 2, Output 2 [2,2,3] tensor >>> c [<TF]. Tensor ' split_1:0 ' shape= (2, 2, 3) Dtype=int32>, <TF. 

       Tensor ' Split_1:1 ' shape= (2, 2, 3) Dtype=int32>] >>> Sess.run (c) [Array ([[[[0, 1, 2], [3, 4, 5]],
        [[6, 7, 8], [9, ten, one]]], Array ([[[12, 13, 14], [15, 16, 17]], [[18, 19, 20],
[21, 22, 23]]]
>>>d= Tf.split (a,2,2) # A.shape (2) is 3, not divisible by 2, error. Traceback (most recent): File "D:\Anaconda2\envs\tensorflow\lib\site-packages\tensorflow\python\framework\common_shapes.py", line 671, in _  Call_cpp_shape_fn_impl input_tensors_as_shapes, status) File "D:\Anaconda2\envs\tensorflow\lib\contextlib.py", line __exit__ Next (Self.gen) File "D:\Anaconda2\envs\tensorflow\lib\site-packages\tensorflow\python\framework\err ors_impl.py ", line 466, in Raise_exception_on_not_ok_status Pywrap_tensorflow. Tf_getcode (status)) Tensorflow.python.framework.errors_impl. Invalidargumenterror:dimension size must be evenly divisible by 2 It's 3 number of ways to split should evenly Divide the split dimension for ' Split_1 ' (op: ' Split ') with input shapes: [], [4,2,3] and with computed input TENSORS:INP
Ut[0] = <2>. >>> d= Tf.split (a,3,2) # changed to 3,a.shape (2) to 3, divisible, no error, return 3 [4,2,1] of tensor >>> d [&LT;TF. Tensor ' split_2:0 ' shape= (4, 2, 1) dtype=int32>, &LT;TF. Tensor ' Split_2:1 ' shape= (4, 2, 1) dtype=int32>, &LT;TF. Tensor ' split_2:2 ' shape= (4, 2, 1) dtype=int32>] >>> Sess.run (d) [Array ([[[0], [3]], [[6], [9]],

       [[[] [[[]], [[[]], [[[]], [[]]], Array ([[[1], [4]], [[7], [10]],

       [[+], [+]], [[[], [[]]]], Array ([[[2], [5]], [[8], [11]], [14], [17]], [[20], [23]]]

Attention:
Unlike Tf.split and reshape, the relative order between values is not changed. Only each dimension can be smaller and not grow.
Values are taken in the order of axis-1.

Tf.squeeze
Squeeze (
    input,
    axis=none,
    name=none,
    squeeze_dims=none
)

Remove the dimension of dimension 1.
Give me a chestnut:

# ' t ' is a tensor of shape [1, 2, 1, 3, 1, 1]
tf.shape (Tf.squeeze (t))  # [2, 3]

You can also specify which dimension to remove:

# ' t ' is a tensor of shape [1, 2, 1, 3, 1, 1]
tf.shape (Tf.squeeze (t, [2, 4]))  # [1, 2, 3, 1]

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.