view and reshape: The shape is not compatible with the layout
In order to change the matrix to a vector, it is necessary to retain the sequence of logical elements as defined.
Learning objectives and first steps2 objectives 2 first fixes
Try to do it after reading
- Track the logical rollout of the transition
- Find out if reshape is possible to allocate new storage
Suggested first
- Python Basic Syntax:
- Thick shape with Index
Why can't six elements sometimes go straight to view in line
In order to change the matrix to a vector, it is necessary to retain the sequence of logical elements as defined. view is created only when the current stride is able to express the target layout and does not automatically re-hungry data to meet the request. reshape returns the view when possible and produces copies if necessary. Thus, the “total number of elements is the same” is one of the necessary conditions, not sufficient for view to necessarily succeed.
The sequential matrix[0,1,2], [3,4,5] was replaced by the logical ones [0,3], [1,4], [2,5]]. If carried out in the current logical order, it shall be [0,3,1,4,2]. The original storage remains [0,1,1,2,3,4,5], the order of access cannot be expressed in a single, ordinary one-dimensional stride, and the direct view to 6-dimensional vector usually fails.
When reshape becomes six elements, the frame can be copied into a new store in logical order. The output value is correct and does not mean that it continues to share data with the input. reshape cannot be considered an unconditional aliasing if the subsequent code depends on the result being modified in situ to affect the original volume; If values are only concerned, additional copies primarily affect memory and performance.
contiguous can be used to visibly get some sort of continuous layout before view, but should not be called blindly before each calculation. The layout may not be replicated when it has been met, and when reordered, it increases the cost of reading and writing. A large number of hidden copies may appear after the attention axis exchange, image channel conversion or multi-source sequence integration, and the chain of operations should be viewed when located.
There's also a semantic risk: put B , T , D Direct reshape Yes. B , D , T ]] Change only the grouping pattern and the number of values, though the same, may be wrongly reorganized. The exchange axis is required using permute or transpose, then handles the layout on a downstream basis, without any operational reshape substitute semantic variation.
Scope of application is the PyTorch Intensive Threshold View interface. Each location can be tracked manually by six different small integer numbers, confirming the logical sequence, and then checking shared storage and performance. A zero or duplicate value input hides errors and is not suitable for such a regression sample.
Can reshape success prove no replication?
No, it's a contract that allows replication if necessary. Codes relying on alias relationships should use clear assurances and validate specific layouts.
Why should the tests be zero to five instead of zero?
Different values expose elements to errors in the order of the elements; Zero is the same after any reordering, and it is not clear whether semantics have been broken.
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.