Logistic Regression Example - Jax Typing CST Test
Loading…
Logistic Regression Example — Jax Code
A logistic regression implementation using JAX for binary classification.
import jax.numpy as jnp
from jax import grad
# Sample data
X = jnp.array([[0,0],[0,1],[1,0],[1,1]])
y = jnp.array([0,1,1,0]) # XOR example
# Initialize parameters
w = jnp.zeros(2)
b = 0.0
# Sigmoid function
def sigmoid(z):
return 1 / (1 + jnp.exp(-z))
# Loss function
def loss(w, b):
y_pred = sigmoid(jnp.dot(X, w) + b)
return -jnp.mean(y * jnp.log(y_pred) + (1-y) * jnp.log(1-y_pred))
grad_loss = grad(loss, argnums=(0,1))
# Gradient descent
for _ in range(1000):
dw, db = grad_loss(w, b)
w -= 0.1 * dw
b -= 0.1 * db
print('Learned weights:', w, 'bias:', b)Jax Language Guide
JAX is an open-source Python library for high-performance numerical computing, combining NumPy-like API with automatic differentiation (autograd), GPU/TPU acceleration, and composable function transformations for machine learning and scientific computing.
Primary Use Cases
- ▸High-performance machine learning and deep learning model development
- ▸Gradient-based optimization and automatic differentiation
- ▸Physics simulations and scientific computing requiring differentiable functions
- ▸Research in reinforcement learning and generative models
- ▸GPU/TPU accelerated numerical computing at scale
Notable Features
- ▸NumPy-compatible API with hardware acceleration
- ▸Automatic differentiation with `grad`
- ▸Just-in-time compilation (`jit`) for optimized performance
- ▸Vectorization (`vmap`) and parallelization (`pmap`) of functions
- ▸Composable transformations for advanced research pipelines
Origin & Creator
JAX was developed by researchers at Google Research starting in 2018, building on Autograd and XLA (Accelerated Linear Algebra) to enable high-performance, differentiable programming.
Industrial Note
JAX is widely used in cutting-edge machine learning research, physics simulations, reinforcement learning, and differentiable programming, where composable gradients and hardware acceleration are essential.