tensorflow之tf.slice()

Reprinted: https: //www.jianshu.com/p/71e6ef6c121b

   https://www.cnblogs.com/chamie/p/11073363.html

def slice(input_, begin, size, name=None):

Where "input_" is the tensor you enter, that is to be cut.

"Begin" is the beginning of each dimension, this in detail below.

"Size" is equivalent to ask each dimension take a few elements out.


See Example 1 below:

t = tf.constant([[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]], [[5, 5, 5], [6, 6, 6]]])
tf.slice(t, [1, 0, 0], [1, 1, 3])

This output is:

[[[3, 3, 3]]]

First as a 3-dimensional array t, you must first understand his shape is [3,2,3].  


Shape:

This is how to shape it? Let's break this t look like to understand. There was a lot of brackets t, just look at its outermost parentheses, then, can be seen as:

t = [A, B, C] # This is the first dimension

Then each of which has two things that can be written as:

A = [i, j], B = [k, l], C = [m, n] # This is the second dimension

Finally, this i, j, k, l, m, n which are:

i = [1, 1, 1], j = [2, 2, 2], k = [3, 3, 3], l = [4, 4, 4], m = [5, 5, 5], n = [6, 6, 6] # this is the third dimension

So the shape is the number in brackets [] hierarchy in the unit.

For t, the outermost brackets have three things, namely A, B, C. Each of these three things there are two stuff, i and j, k and l, m and n.

Inside each of them there are three figures. Therefore, the shape is t [3,2,3] . This is the way I understand it.


Slice:

Before explaining slice, one thing to know is that the array index python is zero-based.

With this foundation, we will look at an example:

tf.slice(t, [1, 0, 0], [1, 1, 3])  # begin = [1, 0, 0]

Here we know the order, begin is [1, 0, 0], size is [1, 1, 3]. They sense the two arrays are from left to right, each number represents a dimension. Above that begin mean starting position, [1, 0, 0] means that in three dimensions, where each dimension counting.

The first dimension is [A, B, C]. begin in [1, 0, 0] is 1, that is, counting from B. Secondly, in the second dimension B = [k, l] (Note ah, I am here only to write B = [k, l], B can not represent only useful if the size in the first number is 2, then, B and C will are taken), the begin in the second number is 0, that is, counting from k. The third dimension k = [3, 3, 3], begin in the third number is 0, that is counted from the first three.

And now you can understand, right? Knowing this starting point after three, look at size.

mean size is the size of each dimension, each dimension is taken several elements. The size of the final output should be the tensor of shape.

Examples of which:

tf.slice(t, [1, 0, 0], [1, 1, 3])  # size = [1, 1, 3]

Lane 1 is a first size, which means to take an element in the first dimension. t = [A, B, C ] begin starting is be B, B that takes a chant. Then the result is the first dimension [B]

The second size is 1, a second dimension B = [k, l], begin in starting is k, is taking a k. The result is then a second dimension [[K]] .

3 is the third size, a third dimension k = [3, 3, 3], begin starting in the first 3. 3 3 take the number three, it should have taken three 3, so is

[[[3, 3, 3]]]

Example 2:

t = tf.constant([[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]], [[5, 5, 5], [6, 6, 6]]])
tf.slice(t, [1, 0, 0], [1, 2, 3])

Read the first, second look is simple. Here also begin as [1, 0, 0]. size takes a first dimension, or [B]. Then this is not a 1, a 2, meaning taking two. Remember B = [k, l] it? This is not just a k, k, and l should be. 3 taken third dimension, i.e. not only is k = [3, 3, 3], l = [4, 4, 4] have to slice away.

To summarize, the first dimension taken [B] . Replaced in the second dimension B [k, l] , becomes [[k, l]] . The third dimension was put into k [3, 3, 3], to replace l [4, 4 , 4], the final result after replacing

[[[3, 3, 3], [4, 4, 4]]]

It is not that a quite simple to understand, but may not be accustomed to this way of thinking.


Example 3:

t = tf.constant([[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]], [[5, 5, 5], [6, 6, 6]]])
tf.slice(t, [1, 0, 0], [-1, -1, -1])

In this case, a word in the source code comments:

If `size[i]` is -1, all remaining elements in dimension i are included in the slice. In other words, this is equivalent to setting: `size[i] = input.dim_size(i) - begin[i]`

In other words, if the size of the input value is -1, then walk the rest of the number in that dimension will slice. In the above example, the begin is [1, 0, 0]. Three dimensions are -1, then the results: The first dimension is [B, C]; second dimension is [[k, l], [m, n]]; The third dimension is [[[3,3 , 3], [4,4,4]], [[5,5,5], [6,6,6]]]

Guess you like

Origin www.cnblogs.com/happystudyeveryday/p/11073991.html