1. Home
  2. /
  3. Jax
  4. /
  5. Simple Linear Regression Example

Simple Linear Regression Example - Jax Typing CST Test

Loading…

Simple Linear Regression Example — Jax Code

A minimal JAX example performing linear regression using automatic differentiation.

import jax.numpy as jnp
from jax import grad, jit

# Sample data
x = jnp.array([1,2,3,4])
y = jnp.array([2,4,6,8])

# Initialize parameters
a = 0.0
b = 0.0

# Define loss function
def loss(a, b):
    y_pred = a * x + b
    return jnp.mean((y - y_pred)**2)

# Compute gradients
grad_loss = grad(loss, argnums=(0,1))

# Simple gradient descent loop
for _ in range(1000):
    da, db = grad_loss(a, b)
    a -= 0.01 * da
    b -= 0.01 * db

print('Learned parameters:', a, 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.

More Jax Typing Exercises

JAX Logistic Regression ExampleJAX Neural Network Forward Pass ExampleJAX Mean Squared Error ExampleJAX Gradient Computation ExampleJAX Vectorized Operations ExampleJAX JIT Compilation ExampleJAX Neural Network Training ExampleJAX Softmax Classification ExampleJAX Convolution Example

Practice Other Languages

CReactPythonC++RustTypeScriptKotlinPHPJavaC#RubyMqlCqlN1qlCypher