gather: Which axis the index follows, where the gradient returns

In batch tasks, each sample may need to select a number of values based on its own index, such as extracting certain candidate scores.

• Step by stepCollate in 1 diagram2 self-measured2 First-hand sources
Start Body
Learning objectives and first steps2 objectives 2 first fixes

Try to do it after reading

  • Handline gather Output
  • Cumulatively repeat the reverse contribution of the selection of the location

Suggested first

  • Python Basic Syntax:
  • Thick shape with Index

Select a different feature position per line

In batch tasks, each sample may need to select a number of values based on its own index, such as extracting certain candidate scores. gather reads by the specified axis, and the index load determines which value to take from each output position. Unlike the operation of selecting the whole batch using the same column index, the correspondence of the non-selection axis must also be clarified.

Enter X = [(10,20,30], [40,50,60], shape as [2,3]. Index I = [(,0,1,1], shape is [2,2] selected along dim = 1. First line output [30,10], second line output [50,50]. Index values point to the column that you enter in the corresponding row, rather than the global subscript after the equalizing of the entire 2-D.

If the loss is the sum of the four outputs, the input gradient is [[1,1], [0,2,0]]. The second line, column 1, was selected twice, so two upstream gradients were received. The unselected position is zero in this reading path, which does not amount to the position being contributed to the rest of the entire model.

Specific limits on the dimensions and non-selecting axle size of input and indexes are specified in the common PyTorch compacts, indexed to integer; gather will not broadcast input and index at will. Cross-border indexes should be processed as data or as shape errors and cannot be modified quietly by example, otherwise the actual selected candidate has changed.

When a sequence is extracted from the length index for the final validity, it is also confirmed that the transition to the lower mark usually requires a reduction of one and a zero-length sample is processed. Test only on a fixed full sequence, leaving out the padding boundary; Negative numbers or empty lengths cannot be inserted directly into another API because of some index syntax support.

Each element of the index load is selected once, and repeating the index means repeating the same input and not weighting. If the task needs to be assembled rather than multiple, the re-assembly should be clearly accomplished in the data definition. The error of two different index lines with a duplicate index, and then the selection of axes, cross-border and dtype, allows for faster positioning issues than randomly large loads.

The upper gradient for each output position is not necessarily 1. If two output weights 2 and 3 are given in the second row, a repeat 50 will receive a total gradient 5; Therefore, the reverse cannot be a simple count of the number of occurrences, but should accumulate the actual upstream contribution.

Review with standard Python

X=[[10,20,30],[40,50,60]]
I=[[2,0],[1,1]]
out=[[row[j] for j in idx] for row,idx in zip(X,I)]
assert out==[[30,10],[50,50]]

Only fixed arithmeticals and indexes are validated above and no framework training is called.

Repeats the location of the same inputEnter the 50 of the second row is selected by two output options, both of which are returned to this position in reverse, total two.505050Output 0 Contribution1Output 1 Contribution 1Input gradient total2
Arrows are selected forward and accumulated in reverse to the source position using the same index.
Why is the second line not [20,20]?

The non-choice axis remains the second line of identity, so index 1 reads the 50th of the second line of X, instead of the 20th line.

Repeat 50 times. Can you keep only one gradient?

In the case of output peace, contribution 1 should be used twice and accumulated into 2.

Sources and reconciliations

Definitions, methods and interfaces are available.

  1. PyTorch — Introduction to PyTorch Tensors
  2. torch.gather

Figures and calculations are reorganized according to this chapter. Mathematical measurements, framework operations and real business effects are described separately in the body of the text.

← Back to the knowledge directory
Ask Max Ask about this site
Ask Max ↗

Opening the assistant…

Paper figure

Photo comments

LEAVE A NOTE

About this photograph

Connecting to the discussion board…

    Comments become public after moderation. You can delete your own comments from this browser.
    No email is required. Clearing cookies prevents this browser from claiming earlier comments.

    MAX WANG / INDEX

    Find something

    Enter a keyword, or explore the directory below.