Compute the loss of a small logistic regression model and its gradient with respect to every parameter.
The model has a weight vector w and a scalar bias b, stored in the dict params under the keys w and b. For the n-th example, with feature row xn and label yn∈{0,1}, the logit is zn=xn⋅w+b, and the loss is
L(w,b)=N1n=1∑N(log(1+ezn)−ynzn)+2λi=1∑Dwi2Here x has shape (N, D) with rows xn, y has shape (N,), N is the number of examples, D is the number of features, and λ is the argument l2. The penalty covers w only and never b.
Return a dict with two keys. The value under loss is L as a float32 JAX array of shape (). The value under grads is a dict with keys w and b holding ∂L/∂w as a float32 JAX array of shape (D,) and ∂L/∂b as a float32 JAX array of shape ().
Input: params = {"w":[0.5,-1.0],"b":0.25}, x = [[1.0,2.0],[-1.0,0.5],[0.0,-1.0],[2.0,1.0]], y = [0.0,1.0,1.0,0.0], l2 = 0.1
Output: {"loss":0.679167,"grads":{"w":[0.556558,0.122672],"b":-0.029251}}
Explanation: The logits are x @ w + 0.25 = [-1.25, -0.75, 1.25, 0.25]. The mean logistic loss over the four examples is 0.616667, and the penalty adds 0.1 / 2 * (0.5^2 + 1^2) = 0.0625, giving 0.679167. The bias gradient has no penalty term.
Input: params = {"w":[0.2,0.0,-0.4],"b":-0.5}, x = [[1.0,0.0,2.0],[0.5,1.5,-1.0],[-2.0,1.0,0.0]], y = [1.0,0.0,1.0], l2 = 0.0
Output: {"loss":1.107212,"grads":{"w":[0.307213,0.013017,-0.66684],"b":-0.320403}}
Input: params = {"w":[3.0],"b":1.0}, x = [[40.0],[-40.0]], y = [0.0,1.0], l2 = 0.01
Output: {"loss":120.045,"grads":{"w":[40.03],"b":0.0}}
loss, grads = jax.value_and_grad(loss_fn)(params) returns both in one call.
jax.nn.softplus(z) computes log(1 + exp(z)) without overflow.
grads is a dict with the same keys as params.
Accepts: object
Accepts: array
Accepts: array
Accepts: number
Compute the loss of a small logistic regression model and its gradient with respect to every parameter.
The model has a weight vector w and a scalar bias b, stored in the dict params under the keys w and b. For the n-th example, with feature row xn and label yn∈{0,1}, the logit is zn=xn⋅w+b, and the loss is
L(w,b)=N1n=1∑N(log(1+ezn)−ynzn)+2λi=1∑Dwi2Here x has shape (N, D) with rows xn, y has shape (N,), N is the number of examples, D is the number of features, and λ is the argument l2. The penalty covers w only and never b.
Return a dict with two keys. The value under loss is L as a float32 JAX array of shape (). The value under grads is a dict with keys w and b holding ∂L/∂w as a float32 JAX array of shape (D,) and ∂L/∂b as a float32 JAX array of shape ().
Input: params = {"w":[0.5,-1.0],"b":0.25}, x = [[1.0,2.0],[-1.0,0.5],[0.0,-1.0],[2.0,1.0]], y = [0.0,1.0,1.0,0.0], l2 = 0.1
Output: {"loss":0.679167,"grads":{"w":[0.556558,0.122672],"b":-0.029251}}
Explanation: The logits are x @ w + 0.25 = [-1.25, -0.75, 1.25, 0.25]. The mean logistic loss over the four examples is 0.616667, and the penalty adds 0.1 / 2 * (0.5^2 + 1^2) = 0.0625, giving 0.679167. The bias gradient has no penalty term.
Input: params = {"w":[0.2,0.0,-0.4],"b":-0.5}, x = [[1.0,0.0,2.0],[0.5,1.5,-1.0],[-2.0,1.0,0.0]], y = [1.0,0.0,1.0], l2 = 0.0
Output: {"loss":1.107212,"grads":{"w":[0.307213,0.013017,-0.66684],"b":-0.320403}}
Input: params = {"w":[3.0],"b":1.0}, x = [[40.0],[-40.0]], y = [0.0,1.0], l2 = 0.01
Output: {"loss":120.045,"grads":{"w":[40.03],"b":0.0}}
loss, grads = jax.value_and_grad(loss_fn)(params) returns both in one call.
jax.nn.softplus(z) computes log(1 + exp(z)) without overflow.
grads is a dict with the same keys as params.
Accepts: object
Accepts: array
Accepts: array
Accepts: number