TensorTonic
Problems
Hessian-Vector Product
JAX
Medium

Compute the product of the Hessian of a loss with a vector v by applying forward-mode differentiation to the gradient, without ever forming the Hessian matrix.

The loss depends on a weight vector w of length D:

f(w)=1N∑n=1N(log⁡(1+exn⋅w)−yn xn⋅w)+α4∑i=1Dwi4f(w) = \frac{1}{N} \sum_{n=1}^{N} \Big( \log\big(1 + e^{x_n \cdot w}\big) - y_n \, x_n \cdot w \Big) + \frac{\alpha}{4} \sum_{i=1}^{D} w_i^4f(w)=N1​n=1∑N​(log(1+exn​⋅w)−yn​xn​⋅w)+4α​i=1∑D​wi4​

Here x has shape (N, D) with rows xnx_nxn​, y has shape (N,) with labels in {0, 1}, and α\alphaα is the argument alpha. The Hessian H(w)H(w)H(w) is the D by D matrix of second derivatives ∂2f/∂wi ∂wj\partial^2 f / \partial w_i \, \partial w_j∂2f/∂wi​∂wj​, and the Hessian-vector product is H(w) vH(w)\,vH(w)v.

Return H(w) vH(w)\,vH(w)v as a float32 JAX array of shape (D,).

Examples

Input: w = [0.5,-0.5], v = [1.0,0.0], x = [[1.0,2.0],[0.0,1.0],[-1.0,1.0]], y = [1.0,0.0,1.0], alpha = 0.0

Output: [0.143872,0.091132]

Explanation: With alpha = 0 only the logistic term has curvature. The returned vector equals the first column of the 2 by 2 Hessian, because v = [1, 0] selects that column.

Input: w = [1.0,-2.0,0.5], v = [0.5,1.0,-1.0], x = [[0.5,-1.0,2.0],[1.0,0.0,-0.5]], y = [0.0,1.0], alpha = 0.5

Output: [0.839386,6.039123,-0.50772]

Input: w = [0.0,1.0,0.0,-1.0], v = [0.0,0.0,1.0,0.0], x = [[1.0,-1.0,2.0,0.5]], y = [1.0], alpha = 0.2

Output: [0.298293,-0.298293,0.596586,0.149146]

Hint 1

jax.jvp(f, (x,), (v,)) returns (f(x), directional derivative of f along v).

Hint 2

Pass jax.grad(f) as the f of jax.jvp to differentiate the gradient.

Hint 3

jax.nn.softplus keeps log(1 + exp(z)) finite for large z.

Requirements

  • Combine forward-mode (jax.jvp) and reverse-mode (jax.grad) differentiation. Solutions that do not call both are rejected.
  • Never build the D by D Hessian. When D is larger than N, any intermediate array with D * D or more entries, such as one built by jax.hessian or by hand, is rejected.
  • Your function is also traced by jax.make_jaxpr, so keep every step in jax.numpy and never turn traced values into Python numbers or NumPy arrays.

Constraints

  • w and v are float32 JAX arrays of shape (D,) with D between 1 and 20.
  • x is a float32 JAX array of shape (N, D) with N between 1 and 5 and entries at most 12 in absolute value.
  • y is a float32 JAX array of shape (N,) whose entries are 0 or 1.
  • alpha is a non-negative Python float.
Try Similar Problems
Per-Example Gradient ClippingMediumStraight-Through Estimator with Custom VJPMediumValue and Grad of a LossEasyPad and Mask to Bucket LengthMediumTraced Learning Rate ScheduleMediumExponential Moving Average with ScanMediumSpectral Norm by Power IterationMediumBatched Attention with vmapMediumPairwise Distances with Nested vmapMediumGlobal Norm Gradient ClippingMediumAdamW from ScratchMediumMixed Precision with Loss ScalingMedium

Accepts: array

Accepts: array

Accepts: array

Accepts: array

Accepts: number

You must run your code first.
Hessian-Vector Product

Hessian-Vector Product

JAX
Medium

Compute the product of the Hessian of a loss with a vector v by applying forward-mode differentiation to the gradient, without ever forming the Hessian matrix.

The loss depends on a weight vector w of length D:

f(w)=1N∑n=1N(log⁡(1+exn⋅w)−yn xn⋅w)+α4∑i=1Dwi4f(w) = \frac{1}{N} \sum_{n=1}^{N} \Big( \log\big(1 + e^{x_n \cdot w}\big) - y_n \, x_n \cdot w \Big) + \frac{\alpha}{4} \sum_{i=1}^{D} w_i^4f(w)=N1​n=1∑N​(log(1+exn​⋅w)−yn​xn​⋅w)+4α​i=1∑D​wi4​

Here x has shape (N, D) with rows xnx_nxn​, y has shape (N,) with labels in {0, 1}, and α\alphaα is the argument alpha. The Hessian H(w)H(w)H(w) is the D by D matrix of second derivatives ∂2f/∂wi ∂wj\partial^2 f / \partial w_i \, \partial w_j∂2f/∂wi​∂wj​, and the Hessian-vector product is H(w) vH(w)\,vH(w)v.

Return H(w) vH(w)\,vH(w)v as a float32 JAX array of shape (D,).

Examples

Input: w = [0.5,-0.5], v = [1.0,0.0], x = [[1.0,2.0],[0.0,1.0],[-1.0,1.0]], y = [1.0,0.0,1.0], alpha = 0.0

Output: [0.143872,0.091132]

Explanation: With alpha = 0 only the logistic term has curvature. The returned vector equals the first column of the 2 by 2 Hessian, because v = [1, 0] selects that column.

Input: w = [1.0,-2.0,0.5], v = [0.5,1.0,-1.0], x = [[0.5,-1.0,2.0],[1.0,0.0,-0.5]], y = [0.0,1.0], alpha = 0.5

Output: [0.839386,6.039123,-0.50772]

Input: w = [0.0,1.0,0.0,-1.0], v = [0.0,0.0,1.0,0.0], x = [[1.0,-1.0,2.0,0.5]], y = [1.0], alpha = 0.2

Output: [0.298293,-0.298293,0.596586,0.149146]

Hint 1

jax.jvp(f, (x,), (v,)) returns (f(x), directional derivative of f along v).

Hint 2

Pass jax.grad(f) as the f of jax.jvp to differentiate the gradient.

Hint 3

jax.nn.softplus keeps log(1 + exp(z)) finite for large z.

Requirements

  • Combine forward-mode (jax.jvp) and reverse-mode (jax.grad) differentiation. Solutions that do not call both are rejected.
  • Never build the D by D Hessian. When D is larger than N, any intermediate array with D * D or more entries, such as one built by jax.hessian or by hand, is rejected.
  • Your function is also traced by jax.make_jaxpr, so keep every step in jax.numpy and never turn traced values into Python numbers or NumPy arrays.

Constraints

  • w and v are float32 JAX arrays of shape (D,) with D between 1 and 20.
  • x is a float32 JAX array of shape (N, D) with N between 1 and 5 and entries at most 12 in absolute value.
  • y is a float32 JAX array of shape (N,) whose entries are 0 or 1.
  • alpha is a non-negative Python float.
Try Similar Problems
Per-Example Gradient ClippingMediumStraight-Through Estimator with Custom VJPMediumValue and Grad of a LossEasyPad and Mask to Bucket LengthMediumTraced Learning Rate ScheduleMediumExponential Moving Average with ScanMediumSpectral Norm by Power IterationMediumBatched Attention with vmapMediumPairwise Distances with Nested vmapMediumGlobal Norm Gradient ClippingMediumAdamW from ScratchMediumMixed Precision with Loss ScalingMedium

Accepts: array

Accepts: array

Accepts: array

Accepts: array

Accepts: number

You must run your code first.
Problems
Loading...
1 / 1