PIXELBANKv9.1.0
Menu

Compute the Gram matrix to capture style/texture statistics.

In neural style transfer, we need a way to represent the "style" of an image independent of its content. The Gram matrix captures correlations between feature channels, representing texture patterns.

Given feature map F of shape (C, N) where C is channels and N is spatial positions:

Gij=βˆ‘k=1NFikβ‹…FjkG_{ij} = \sum_{k=1}^{N} F_{ik} \cdot F_{jk}

Or in matrix form: G=Fβ‹…FTG = F \cdot F^T

The Gram matrix is:

  • Size C Γ— C (one entry per pair of channels)
  • Symmetric (Gij=GjiG_{ij} = G_{ji})
  • Captures which features tend to activate together

High correlation between two channels means those features often co-occur, representing a texture pattern.

Example:

Input:
gram_matrix([[1, 2], [3, 4]])
Output:
[[5, 11], [11, 25]]
Reasoning:
  • For features with 2 channels, 2 spatial positions:
  • F = [[1, 2], [3, 4]]

G[0][0] = F[0]Β·F[0] = 1Γ—1 + 2Γ—2 = 1 + 4 = 5 G[0][1] = F[0]Β·F[1] = 1Γ—3 + 2Γ—4 = 3 + 8 = 11 G[1][0] = F[1]Β·F[0] = 3Γ—1 + 4Γ—2 = 3 + 8 = 11 (symmetric) G[1][1] = F[1]Β·F[1] = 3Γ—3 + 4Γ—4 = 9 + 16 = 25

Result: [[5, 11], [11, 25]]

Constraints:

  • features: 2D list of shape [channels][spatial_positions]
  • Return: Gram matrix as 2D list of shape [channels][channels]
  • Each entry G[i][j] is the dot product of row i and row j
solution.py

Test Results

0/0
Run code to see test results.