Learning to Build a Generative Diffusion Model for Crystals
From the publisher’s feed, collected in the ZeroLabs newsroom.
Photo by Christine Galligan on UnsplashFinding new materials is usually a slow process. Traditionally, scientists use complex simulations to test how atoms fit together, which takes massive amount of computing power and time.AI research labs have started to change this by using Generative AI, mainly diffusion models. As a student I wanted to see if I could implement a basic version of this model myself to understand the mechanics beneath this.In this post, we’ll walk through this journey of building “CrytsalDiff”, a deep learning project that generates 3D Perovskite crystal (ABO3) from pure noise, instead of just importing a pre-built black-box library. I implemented the core diffusion loop and a basic E(n)-Equivariant Graph Neural Network in PyTorch.The System Flowchart: How CrystalDiff WorksBefore diving into math, here is the workflow of the system. The pipeline takes completely random atomic coordinates and refine them using physics aware neural network until stable crystal emerges.===================================================================== PHASE 1: DATA ENGINEERING===================================================================== [Materials Project API] │ ▼ Fetch Stable Oxides (ABO₃) ──► Extract: │ 1. Identity (Z: Atomic Numbers) │ 2. Geometry (X: 3D Coordinates) ▼ 3. Property (Y: Band Gap) Center Coordinates to (0,0,0) │ ▼ Build Fully Connected Graph (Edges between all 5 atoms)===================================================================== PHASE 2: GENERATIVE DIFFUSION & EGNN===================================================================== [Random 3D Noise] + [Z (Atom Types)] + [Y (Target Band Gap)] │ ▼ ┌─────────────────────────────────────────────────────────────────┐ │ REVERSE DIFFUSION LOOP (e.g., T = 50 steps to 0) │ │ │ │ 1. Temporal & Property Embedding: │ │ Convert time 't' and Band Gap 'Y' into dense vectors. │ │ Add to initial atom features (h). │ │ │ │ 2. E(n)-Equivariant Message Passing (EGNN): │ │ ├─► Calc relative distances ||Xi - Xj||² (Invariant) │ │ ├─► Pass messages between neighbors to update features (h) │ │ └─► Update 3D coords strictly along vector (Xi - Xj) │ │ │ │ 3. Noise Prediction & Update: │ │ Predict the noise (ε_θ) and subtract it from current coords.│ └─────────────────────────────────────────────────────────────────┘ │ ▼ [Denoised, Stable 3D Coordinates]===================================================================== PHASE 3: VALIDATION & DEPLOYMENT===================================================================== │ ├──────────────────────────────────┐ ▼ ▼ [Scientific Validation] [Streamlit Web App] 1. Compute RDF Analysis 1. User selects A-site Cation 2. Verify Pauli Exclusion (0-1.5Å) 2. User sets Noise & Steps 3. Verify Covalent Bonds (~1.9Å) 3. Native HTML py3Dmol Render │ │ ▼ ▼ [Proof of Physical Chemistry] [Interactive UI Demo]2. The Core: Denoising Diffusion Probabilistic Models (DDPMs)To teach a neural network how to build crystal, we first teach it how to destroy one.Imagine a perfect crystal lattice. If we slowly heat it, the atom vibrate, break their bonds, and eventually melt into random cloud of gas. In our model “heat” is Gaussian noise applied over a series of step.The Forward Process :- We take a stable crystal from the Material Project Database with initial atomic coordinates xo. Over a series of time-steps T, we progressively inject noise ε.Mathematically, the distribution of positions at step t is give by:The Reverse Process (Generative De-noising) :- The goal is to train a neural network to reverse this entropy. The model looks at a noisy point cloud xt, the current timesteps t, and the atomic identites z (e.g., Titanium, Oxygen), and predicts the noise that needs to be subtracted.This step looks like this :3. The Physics Challenge : Rotational SymmetryIf we feed 3D coordinates into a standard Multi-Layer Perceptron (MLP), it will fail. If we rotate a crystal by 45 degrees, its internal energy and chemical stability remain identical. Physics demands rotational in-variance. A standard neural network, however, sees completely different (X,Y,Z) numbers and think it is looking at an entirely new material.E(n)-Equivariance respects Euclidean symmetry.To learn how this works, I implemented a basic version of EGNN architecture defined by satorras et al. in PyTorch.In this framework, the network is restricted from operaating on absolute coordinates. Instead, it compute the relative distance vector between atoms and scales the vector by a learned, invariant message.Calculate Invariant Message :2. Equivariant Coordinate Update :3. Feature Update :4. Data Engineering & ConditioningTo train the model, I built a simple data pipeline using the Material Project API (mp-api). I downloaded stable oxide structure, extracted their atomic numbers z, and centered their unit cell coordinates to (0, 0, 0) to stabilize the diffusion training.To make the generation more controllable, I added Property Conditioning, I extracted the Band Gap scalar y for each crystal, embedded it through a linear layer, and injected it into the latent node features h. This allow user to input a target band gap into the app, lightly biasing the model towards generating a specific type of material.5. Did it actually learn anything?To check, I plotted the Radial Distribution Function (RDF) of generated crystals against baseline random noise. An RDF measures the probability of finding another atom at a specific distance r from any given atom.The Pauli Exclusion Principle : Notice the flat blue line from 0.0 to 1.5 A. The probability density is strictly zero. The model learned that atoms physically cannot overlap, naturally pushing them apart.Covalent Bond : The first sharp peak occur exactly at ~1.9 A. Analyzing spatial point cloud during training, the model figured out the correct Titanium-Oxygen bond length.Structure : The distinct secondary peak (>2.5 A) show model is building a structured lattice, not just a random clump of atoms.6. DeploymentI deployed the inference pipeline as a live streamlit application.User can select a target atom, set diffusion steps, and watch model denoise the structure. I used Streamlit’s native HTML components to render the py3Dmol interactive visualization.https://cryst-diff.streamlit.app/Here are the foundational papers that inspired me:E(n) Equivariant Graph Neural Networks (Satorras et al., ICML 2021)Crystal Diffusion Variational Autoencoder (CDVAE) (Xie et al., ICLR 2022)Scaling deep learning for materials discovery (GNoME) (Merchant et al. / Google DeepMind, Nature 2023)You can explore implementation here:GitHub: https://github.com/adiManethia/CrystalDiffWeb App: https://cryst-diff.streamlit.app/Learning to Build a Generative Diffusion Model for Crystals was originally published in Towards AI on Medium, where people are continuing the conversation by highlighting and responding to this story.