detach and clone: Guidance and memory copying are two axes
When it is necessary to keep intermediate results for logs, teacher targets or reprocessing, there are regular encounters with detach and clone.
Learning objectives and first steps2 objectives 2 first fixes
Try to do it after reading
- Distinguishing Breakcharts from Copy Storage
- Predicts three combinations of gradients
Suggested first
- Python Basic Syntax:
- Thick shape with Index
The value copy is not the same as the guide copy
When it is necessary to keep intermediate results for logs, teacher targets or reprocessing, there are regular encounters with detach and clone. detach mainly severes the current guidance relationship and usually shares storage with the original volume; clone creates a copy of the value, but by default you can return the lead to the original input in the calculation chart. Both are interpreted as “copying” and errors are made at both the gradient and writing levels.
Sets x =2 and requires a gradient. If y = x. clone(), L = y 2, forward 4 and the number of guides can still be returned to x, receive 4. If y = x. detach() is still at its current value of 2, but L is calculated only by this separated y, the default is no longer to form this path for the original x. The same forwards do not represent the same reliance on training objectives.
If you want to break the gradient and get a separate writeable memory, you can use the appropriate combination of detach and clone. If only detach were to modify the shared content, the original volume value could also be altered and even destroy the old value needed in reverse. It is not a read-only snapshot that can be safely written in all cases.
A common use is the fixed branch of teachers: teachers export as a target and do not want soft targets to change with the students ' goals. At this point, the decomposition number has a clear algorithm meaning. Another use is the cache reasoning value, which needs to be considered whether storage is independent and the cache is ineffective, and the breakcharting should not be mistaken for having been copied to CPU or written into a permanent file.
Also distinguish between re-constructing lengths and clone. Converting existing values to Python and creating new loads may not have the intention of cutting off guidance and changing dtype or device. The changes that need to be kept should be made using a clear and minimal volume operation; When a break is needed, the reason is expressed in the code, avoiding the training curve still falling and the new branch never contributing.
(b) The normal PyTorch compact volume, with storage aliases and automatic microconnectivity to be separately judged; The same value is only the third attribute. When debugging, three entries are listed as different checks to explain the difference between subsequent writing and the reverse. The minimum check shall be accompanied by a comparison of values, storage shares and original input gradients, and the equivalent of two writings cannot be determined by just one assert output.
Can you still get the gradient after clone?
As a rule, it is not necessary to cut off the minor relationship by itself, provided that other conditions permit.
Why is it still possible to change training after detach has the same result?
It deletes a lead path, and the gradient received for the original parameter may be different, even if the current value is the same for the forward loss.
If only the current values are kept for comparison later, it is also usually ensured that they are stored independently; The gradient path cannot be cut if the original input continues to be guided. These two requirements should be decided separately.
Sources and reconciliations
Definitions, methods and interfaces are available.
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.