Skip to main content
CodeSpeedTest
Languages
Start TypingJump into a test — pick any languageAdaptive TrainingUnlock chars as you master themPractice DrillsFocused sessions targeting weak spotsDaily ChallengesNew coding challenges every dayRace ModeCompete against others in real timeAI OpponentRace against an AI at your WPM levelTournamentsLive coding speed tournamentsArcade GamesZType, Overkill Survival, Glyphica & moreGamificationXP, coins, badges & quests
LeaderboardGlobal rankings for every languageCertificatesEarn verifiable Bronze / Silver / Gold certsActivityDaily streaks & historical analyticsProfileYour stats, badges & achievements
Browse Languages500+ languages with real code examplesBlogTips, guides & deep divesFree ToolsWPM calculator, typing speed report & moreFAQCommon questions answeredGetting StartedNew to CodeSpeedTest?AboutOur story & missionSupportGet help — Pro users get priorityContactGet in touch with the team
Pricing
  1. Home
  2. /
  3. Learn
  4. /
  5. Jax

Learn Jax - 10 Code Examples & CST Typing Practice Test

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.

View all 10 Jax code examples →
JAX Simple Linear Regression ExampleJAX 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

Learn JAX with Real Code Examples

Updated Nov 24, 2025

Explain

JAX provides a NumPy-compatible API with hardware acceleration on CPU, GPU, and TPU.

It offers automatic differentiation for gradients of arbitrary Python functions using `grad`, `vmap`, `jit`, and `pmap`.

JAX enables composable transformations like vectorization, parallelization, and just-in-time compilation, making it ideal for research and large-scale ML experiments.

Core Features

Autograd for forward- and reverse-mode differentiation

JIT compilation for CPU/GPU/TPU performance

Vectorized operations over batches with `vmap`

Parallel computation across devices with `pmap`

Random number generation with functional, reproducible API

Basic Concepts Overview

Array: JAX’s primary data structure, similar to NumPy arrays

grad: computes derivatives of functions automatically

jit: compiles Python functions for optimized execution

vmap: vectorizes functions over batch dimensions

pmap: parallelizes functions across multiple devices

Project Structure

scripts/ - JAX model and function scripts

datasets/ - input data for training or simulations

notebooks/ - exploratory computation and experimentation

models/ - saved parameters or checkpoints

logs/ - performance metrics and experiment tracking

Building Workflow

Define functions using JAX-compatible NumPy API

Apply `grad` for automatic differentiation

Use `jit` to compile functions for hardware acceleration

Vectorize computations over batches with `vmap`

Parallelize computations across devices with `pmap` for scalability

Difficulty Use Cases

Beginner: basic array operations and gradients

Intermediate: JIT compilation and batched operations

Advanced: multi-device parallelization with `pmap`

Expert: custom differentiable functions and research pipelines

Enterprise: scaling scientific computing or ML models on TPU clusters

Comparisons

JAX vs NumPy: JAX adds autograd, JIT, vmap, pmap, GPU/TPU support

JAX vs TensorFlow: JAX is functional, research-focused, flexible; TensorFlow has higher-level APIs and production tools

JAX vs PyTorch: JAX uses functional programming and composable transformations; PyTorch is imperative and popular for production

JAX vs Numpy+Autograd: JAX is faster, supports hardware acceleration and composable transforms

JAX vs MATLAB: JAX is Python-first, differentiable, and GPU/TPU compatible

Versioning Timeline

2018 - Initial release by Google Research

2019 - Added `vmap` for vectorization and `pmap` for multi-device parallelism

2020 - Stable releases with enhanced JIT compilation

2021 - Full TPU support and Flax integration

2025 - Latest release with expanded scientific computing ecosystem and GPU/TPU optimizations

Glossary

JAX: high-performance numerical computing library

grad: automatic differentiation function

jit: just-in-time compilation

vmap: vectorized map for batch processing

pmap: parallel map across multiple devices

Installation Setup

Install Python 3.8+

Install JAX and appropriate version for your device: `pip install jax jaxlib`

For GPU: `pip install jax jaxlib[cuda]` with matching CUDA/cuDNN version

Verify installation with a simple NumPy-like computation

Test `grad`, `jit`, and `vmap` functions for expected outputs

Environment Setup

Install Python 3.8+

Install JAX and device-specific jaxlib

Verify GPU/TPU availability using jax.devices()

Test `grad`, `jit`, `vmap`, `pmap` on example functions

Install Optax, Flax, or Haiku if needed for ML

Config Files

scripts/ - model and computation scripts

datasets/ - input data files

models/ - saved parameters or checkpoints

logs/ - training or simulation logs

notebooks/ - experiments and analysis

Cli Commands

pip install --upgrade jax jaxlib

python script.py to run experiments

Use environment variables to specify GPU/TPU devices

Monitor logs via standard Python logging

Integrate with cloud TPU/GPU services for large-scale jobs

Internationalization

Unicode dataset support

Documentation and tutorials in English

Community contributions from multiple regions

Adopted by global ML research labs

Compatible with international scientific computing standards

Accessibility

Cross-platform Python support

Works on CPU, GPU, TPU

Open-source and free under Apache 2.0

Accessible to researchers and developers

Integrates with standard Python ecosystem

Ui Styling

Jupyter or Colab notebooks for development

Matplotlib/Plotly for visualizations

TensorBoard or custom dashboards for metrics

Monitor computation via logs

Export figures and metrics for reporting

State Management

Track experiment parameters and random keys

Save model parameters for reproducibility

Version scripts and functions

Backup logs and results

Ensure reproducibility across devices

Data Management

Use JAX arrays for computation

Preprocess with NumPy/SciPy functions compatible with JAX

Maintain reproducible random seeds with PRNGKey

Batch data for vectorization with `vmap`

Cache computations for repeated experiments

Architecture

Functional programming approach with pure functions

Autograd-based differentiation engine

XLA (Accelerated Linear Algebra) backend for compilation

Device-agnostic computations with CPU/GPU/TPU support

Composable transformations (`jit`, `grad`, `vmap`, `pmap`)

Rendering Model

Functional transformations applied to pure functions

Array-based numerical computations

JIT-compiled execution for performance

Composable transformations (`grad`, `vmap`, `pmap`)

Integration with ML frameworks for neural networks

Architectural Patterns

Functional API with pure functions

Differentiation engine for automatic gradients

XLA-backed compilation for acceleration

Device abstraction for CPU/GPU/TPU

Composable transformation pipeline for research workloads

Real World Architectures

Deep reinforcement learning pipelines

Physics and biology simulations requiring gradients

Transformer and neural network research

Generative models and probabilistic programming

Large-scale TPU/GPU research experiments

Design Principles

Functional programming and pure functions

Composability of transformations

High performance via XLA compilation

Automatic differentiation of arbitrary Python functions

Hardware-agnostic with CPU/GPU/TPU support

Scalability Guide

Vectorize functions using `vmap` for batch efficiency

Parallelize across devices with `pmap`

Use JIT compilation to accelerate repeated computations

Distribute computations across multi-GPU/TPU clusters

Cache intermediate computations when feasible

Migration Guide

Upgrade JAX via pip

Verify jaxlib version matches hardware

Test old scripts with latest JAX transformations

Update Flax/Optax dependencies if used

Ensure reproducibility with PRNGKey management

Performance Notes

JIT-compiled functions are orders of magnitude faster than pure Python

Vectorization with `vmap` eliminates explicit Python loops

Parallelization with `pmap` scales efficiently across devices

Avoid frequent device-host memory transfers for efficiency

Profiling with `jax.profiler` helps identify bottlenecks

Security Notes

Ensure data privacy before sending to GPU/TPU

Avoid using untrusted code in JIT-compiled functions

Validate inputs for scientific simulations to prevent invalid states

Follow secure ML pipeline best practices

Encrypt sensitive datasets if using cloud TPUs

Monitoring Analytics

Log training and simulation metrics

Monitor computation performance with profiling tools

Track device utilization for GPU/TPU

Visualize loss and accuracy curves

Audit function outputs for correctness

Code Quality

Use functional, side-effect-free functions

Document all transformation pipelines

Test gradients and compiled functions

Version scripts and parameters

Ensure reproducibility with PRNG keys

Practical Examples

Compute gradient of a scalar function with `grad`

Train a simple neural network using JAX arrays and `grad`

Vectorize loss computation over a batch with `vmap`

JIT-compile a physics simulation for GPU execution

Parallelize reinforcement learning environment rollout across multiple GPUs using `pmap`

Troubleshooting

Ensure functions are pure (no side effects) for `jit`/`grad` compatibility

Check data types: JAX often requires float32 arrays for GPU

Debug uncompiled functions before applying `jit`

Use `jax.debug.print` for intermediate values

Update `jax` and `jaxlib` to compatible versions

Testing Guide

Validate functions on small arrays before batching

Compare gradients with numerical approximations

Test JIT-compiled and vectorized functions separately

Ensure reproducibility with PRNG keys

Benchmark performance on target device

Deployment Options

Run JAX computations on CPU/GPU/TPU

Export trained models parameters for Flax/Haiku

Integrate with production ML pipelines via XLA-compiled functions

Serve batch predictions using compiled functions

Use JAX in research simulations or cloud TPU workflows

Tools Ecosystem

NumPy for array operations

SciPy for scientific computation

Optax for gradient-based optimization

Flax/Haiku for neural network modeling

TensorFlow Datasets (TFDS) for dataset loading

Integrations

Optax for optimizers

Flax/Haiku for high-level neural networks

TensorFlow and PyTorch interoperability via ONNX/XLA

GPU/TPU hardware for acceleration

NumPy and SciPy for scientific computation

Productivity Tips

Use JIT to accelerate heavy computations

Vectorize functions instead of Python loops

Keep functions pure for composability

Cache and reuse compiled functions

Leverage multi-device parallelism with `pmap`

Challenges

Transitioning to functional programming mindset

Debugging JIT-compiled functions

Ensuring reproducibility with PRNG keys

Managing multi-device parallelism

Integrating JAX with larger ML frameworks for production

Learning Path

Learn Python and NumPy fundamentals

Understand functional programming principles

Practice autograd and `grad` on simple functions

Experiment with `jit`, `vmap`, and `pmap`

Build research or ML pipelines using Flax/Optax/JAX

Skill Improvement Plan

Week 1: NumPy-like computations and arrays

Week 2: Automatic differentiation with `grad`

Week 3: JIT compilation and benchmarking

Week 4: Vectorization with `vmap` and parallelization with `pmap`

Week 5: Full ML pipelines with Flax/Optax and TPU/GPU acceleration

Interview Questions

What is JAX and how is it different from NumPy?

Explain `grad`, `jit`, `vmap`, and `pmap` with examples

How does JAX handle GPU/TPU acceleration?

How do you implement a neural network using JAX?

What are the advantages of composable transformations in JAX?

Cheat Sheet

Array = JAX array (like NumPy)

grad(f) = derivative of function f

jit(f) = compiled function for speed

vmap(f) = vectorized map over batches

pmap(f) = parallel map across devices

Books

Programming with JAX

Deep Learning with JAX and Flax

Hands-On Differentiable Programming

Functional Machine Learning in Python

JAX for Scientific Computing

Tutorials

JAX official tutorials

Flax/Optax example notebooks

YouTube walkthroughs for JAX ML pipelines

Colab examples for GPU/TPU acceleration

Hands-on exercises for vectorization and parallelization

Official Docs

https://jax.readthedocs.io/

https://github.com/google/jax

Community Links

JAX GitHub repository

StackOverflow JAX tag

Flax/Haiku communities

Reddit ML and AI research channels

Discord/Slack ML groups

Community Support

JAX GitHub repository

Google Research JAX discussions

StackOverflow JAX tag

Flax/Haiku communities

Reddit and Discord AI/ML channels

Monetization

Research consulting using JAX pipelines

Scientific simulations for enterprise clients

ML model development and optimization

High-performance computing services

Training workshops and tutorials

Future Roadmap

Enhanced TPU/GPU optimizations

Better integration with scientific Python ecosystem

Expanded libraries for differentiable programming

Higher-level ML frameworks built on JAX

Improved tooling for production deployment

When Not To Use

Quick prototyping for simple computations on CPU

Projects needing extensive high-level ML framework support

Non-research applications with no need for gradient computation

Legacy codebases not compatible with functional transformations

Very small scripts where JIT or GPU acceleration overhead outweighs benefits

Final Summary

JAX is a high-performance Python library for numerical computing with autograd, JIT, and hardware acceleration.

Enables functional, composable, and differentiable programming.

Ideal for ML research, scientific computing, and TPU/GPU-accelerated pipelines.

Integrates seamlessly with Optax, Flax, and Haiku for deep learning.

Supports vectorization, parallelization, and scalable numerical computation.

Faq

Is JAX free?

Yes - open-source under Apache 2.0 license.

Which devices are supported?

CPU, GPU, and TPU (via XLA backend).

Can JAX compute gradients automatically?

Yes - using `grad` for scalar or vector functions.

Is JAX suitable for deep learning?

Yes - often used with Flax or Haiku for neural networks.

Can JAX scale to multiple devices?

Yes - `pmap` allows parallelization across GPUs/TPUs.

Code Sample Descriptions

1

JAX Simple Linear Regression Example

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)

A minimal JAX example performing linear regression using automatic differentiation.

Let’s Try →
2

JAX Logistic Regression Example

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)

A logistic regression implementation using JAX for binary classification.

Let’s Try →
3

JAX Neural Network Forward Pass Example

import jax.numpy as jnp

# Input
x = jnp.array([1.0, 2.0, 3.0])

# Network parameters
W1 = jnp.array([[0.1,0.2,0.3],[0.4,0.5,0.6]])
b1 = jnp.array([0.1,0.2])
W2 = jnp.array([[0.7,0.8]])
b2 = jnp.array([0.3])

# Forward pass
def relu(x):
    return jnp.maximum(0, x)

h = relu(jnp.dot(W1, x) + b1)
y_pred = jnp.dot(W2, h) + b2

print('Output:', y_pred)

Forward pass of a simple 2-layer neural network in JAX.

Let’s Try →
4

JAX Mean Squared Error Example

import jax.numpy as jnp

# Sample predictions and targets
y_true = jnp.array([1.0,2.0,3.0])
y_pred = jnp.array([1.1,1.9,3.2])

# Mean squared error
def mse(y_true, y_pred):
    return jnp.mean((y_true - y_pred)**2)

print('MSE:', mse(y_true, y_pred))

Computing mean squared error using JAX for vectorized operations.

Let’s Try →
5

JAX Gradient Computation Example

import jax.numpy as jnp
from jax import grad

# Function
def f(x):
    return x**2 + 3*x + 2

# Compute gradient
grad_f = grad(f)

x = 5.0
print('Gradient at x=5:', grad_f(x))

Illustrating automatic differentiation using JAX.

Let’s Try →
6

JAX Vectorized Operations Example

import jax.numpy as jnp

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

# Element-wise operations
z = x + y
d = x * y

print('Sum:', z)
print('Product:', d)

Demonstrating JAX's vectorized operations on arrays.

Let’s Try →
7

JAX JIT Compilation Example

import jax.numpy as jnp
from jax import jit

@jit
def f(x):
    return jnp.sin(x) ** 2 + jnp.cos(x) ** 2

x = jnp.linspace(0, 10, 1000)
print('JIT function output:', f(x))

Using JIT compilation in JAX to speed up function execution.

Let’s Try →
8

JAX Neural Network Training Example

import jax.numpy as jnp
from jax import grad

# Data
X = jnp.array([[1.0],[2.0],[3.0],[4.0]])
y = jnp.array([2.0,4.0,6.0,8.0])

# Parameters
w = 0.0
b = 0.0

# Prediction
def predict(w, b, X):
    return w * X + b

# Loss
def loss(w, b):
    y_pred = predict(w, b, X)
    return jnp.mean((y - y_pred)**2)

grad_loss = grad(loss, argnums=(0,1))

# Gradient descent
for _ in range(1000):
    dw, db = grad_loss(w, b)
    w -= 0.01 * dw
    b -= 0.01 * db

print('Learned w,b:', w, b)

Training a small neural network using JAX and gradient descent.

Let’s Try →
9

JAX Softmax Classification Example

import jax.numpy as jnp
from jax import grad

# Data
X = jnp.array([[1,2],[3,4],[5,6]])
y = jnp.array([0,1,2])

# Parameters
W = jnp.zeros((3,2))
b = jnp.zeros(3)

# Softmax
def softmax(z):
    e_z = jnp.exp(z - jnp.max(z))
    return e_z / e_z.sum(axis=0)

# Loss
def loss(W, b):
    logits = jnp.dot(X, W.T) + b
    y_pred = jnp.array([softmax(l) for l in logits])
    return -jnp.mean(jnp.log(y_pred[jnp.arange(len(y)), y]))

grad_loss = grad(loss, argnums=(0,1))

Performing multi-class classification using softmax in JAX.

Let’s Try →
10

JAX Convolution Example

import jax.numpy as jnp
from jax import lax

# Input sequence
x = jnp.array([1.0, 2.0, 3.0, 4.0, 5.0])
# Kernel
w = jnp.array([0.2, 0.5, 0.2])

# 1D convolution
conv = lax.conv_general_dilated(x[jnp.newaxis, :, jnp.newaxis],
                                w[jnp.newaxis, :, jnp.newaxis],
                                window_strides=(1,),
                                padding='VALID',
                                dimension_numbers=('NWC','WIO','NWC'))
print('Convolution output:', conv)

Performing 1D convolution using JAX for sequence data.

Let’s Try →

Frequently Asked Questions about Jax

What is Jax?

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.

What are the primary use cases for Jax?

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

What are the strengths of Jax?

Extremely fast and hardware-optimized for large computations. Highly composable functional transformations. Seamless integration with NumPy and SciPy. Strong support for research in ML and differentiable programming. Works efficiently on TPUs and multi-GPU clusters

What are the limitations of Jax?

Steep learning curve for beginners in functional programming style. Limited ecosystem compared to TensorFlow or PyTorch for high-level models. Debugging JIT-compiled code can be tricky. Some Python libraries are incompatible with JAX’s functional transformations. Primarily research-focused; fewer production deployment utilities

How can I practice Jax typing speed?

CodeSpeedTest offers 10+ real Jax code examples for typing practice. You can measure your WPM, track accuracy, and improve your coding speed with guided exercises.

Learn Other Programming Languages

CReactPythonC++RustTypeScriptKotlinPHPJavaC#RubyMqlCqlN1qlCypherGremlinPartiqlHaskellElixirFsharpView all languages →
CodeSpeedTest

Improve your coding speed, code accuracy, and programming syntax WPM with practice sessions across 500+ programming languages.

Quick Links

HomeAboutFeaturesGetting StartedLanguages

Legal & Support

Pro ⚡ PricingContactPrivacy PolicyTerms of Service

Connect

CodeSpeedTest on GitHubCodeSpeedTest on TwitterEmail CodeSpeedTest

© 2026 CodeSpeedTest. All rights reserved.