Tensor Algebra, Broadcasting, and Shape Reasoning
Tensor Shapes as Contracts
Shape reasoning
Shapes act like contracts between layers and transformations. In advanced TensorFlow code, explicit shape reasoning reduces runtime failures, improves compile-time optimization, and makes large distributed systems more reliable. A batch dimension is often introduced as , where is batch size and is feature dimension.
Important notation
Broadcasting rules
Broadcasting lets operations act on tensors with different shapes by expanding singleton dimensions conceptually. For example, adding and is common in bias terms. However, overuse of implicit broadcasting can mask bugs, especially when a dimension of size is accidentally introduced.
Dense vs ragged vs sparse representations
Dense tensor
- Best for regular grids and GPU efficiency
- May waste memory when data are irregular
Ragged tensor
- Represents variable-length sequences directly
- Useful for NLP and event data
Which statement about broadcasting is correct?
Broadcasting is a shape compatibility mechanism that often avoids explicit replication.
Correct answer: It allows implicit expansion when dimensions are compatible
Why can a mistaken singleton dimension be dangerous in model code?
Such bugs are hard to detect because TensorFlow may not raise an immediate error.
Correct answer: It may broadcast silently and produce numerically valid but semantically wrong results.
Linear Algebra for Deep Learning
Matrix operations
Many layers are compositions of affine maps and nonlinearities: . Understanding matrix multiplication, transposition, norms, and tensor contraction is essential for debugging attention mechanisms, recurrent networks, and low-rank adaptation methods.
In , what is the role of ?
The bias shifts the affine transformation and is added elementwise after multiplication.
Correct answer: A bias vector added after the linear map
Write one reason norms are useful in machine learning.
Norms appear in weight decay, gradient clipping, and convergence analysis.
Correct answer: They quantify magnitude and are used in regularization, stability analysis, and optimization.