|
7 | 7 |
|
8 | 8 |
|
9 | 9 |
|
10 |
| -Flowfusion is a Julia package for learning and sampling from conditional diffusion processes across continuous, discrete, and manifold spaces. It provides a unified framework for: |
11 |
| - |
12 |
| -- Learning conditional flows between states |
13 |
| -- Sampling from learned distributions |
14 |
| -- Working with various state types (continuous, discrete, manifold) |
15 |
| -- Handling partial observations and masked states |
| 10 | +Flowfusion is a Julia package for training and sampling from diffusion and flow matching models (and some things in between), across continuous, discrete, and manifold spaces. It provides a unified framework for: |
16 | 11 |
|
17 | 12 | ## Features
|
18 | 13 |
|
19 |
| -### Multiple State Types |
20 |
| -- Continuous states (Euclidean spaces) |
21 |
| -- Discrete states (categorical variables) |
22 |
| -- Manifold states (probability simplexes, tori, rotations) |
23 |
| -- Masked states for partial conditioning |
24 |
| - |
25 |
| -### Supported Processes |
26 |
| -- Deterministic flows |
27 |
| -- Brownian motion |
28 |
| -- Ornstein-Uhlenbeck |
29 |
| -- Discrete flows (InterpolatingDiscreteFlow, NoisyInterpolatingDiscreteFlow) |
30 |
| -- Manifold-specific processes |
31 |
| - |
32 |
| -### Core Operations |
33 |
| -- `bridge(P, X0, X1, t)`: Sample intermediate states conditioned on start and end states |
34 |
| -- `gen(P, X0, model, steps)`: Generate sequences using a learned model |
35 |
| -- Support for both direct state prediction and tangent coordinate prediction |
| 14 | +- Controllable noise (or fully deterministic for flow matching) |
| 15 | +- Flexible initial $X_0$ distribution |
| 16 | +- Conditioning via masking |
| 17 | +- States: Continuous, discrete, and a wide variety of manifolds supported (via [Manifolds.jl](https://github.com/JuliaManifolds/Manifolds.jl)) |
| 18 | +- Compound states supported (e.g. jointly sampling from both continuous and discrete variables) |
36 | 19 |
|
37 |
| -### Training |
38 |
| -- Loss functions adapted to different state/process types |
39 |
| -- Support for masked training (partial observations) |
40 |
| -- Time scaling for improved training dynamics |
41 |
| -- Integration with Flux.jl for neural network models |
| 20 | +### Basic idea: |
| 21 | +- Generate `X0` and `X1` states from your favorite distribution, and a random `t` between 0 and 1 |
| 22 | +- `Xt = bridge(P, X0, X1, t)`: Sample intermediate states conditioned on start and end states |
| 23 | +- Train model to predict how to get to `X1` from `Xt` |
| 24 | +- `gen(P, X0, model, steps)`: Generate sequences using a learned model |
42 | 25 |
|
43 | 26 | ## Examples
|
44 | 27 |
|
45 | 28 | The package includes several examples demonstrating different use cases:
|
46 | 29 |
|
47 |
| -- `continuous.jl`: Learning flows between clusters in continuous space |
48 |
| -- `discrete.jl`: Learning categorical transitions |
49 |
| -- `torus.jl`: Learning flows on a torus manifold |
50 |
| -- `probabilitysimplex.jl`: Learning flows between probability distributions |
| 30 | +- `continuous.jl`: Learning a continuous distribution |
| 31 | +- `torus.jl`: Continous distributions on a manifold |
| 32 | +- `discrete.jl`: Discrete distributions with discrete processes |
| 33 | +- `probabilitysimplex.jl`: Discrete distributions with continuous processes via a probability simplex manifold |
| 34 | +- `continuous_masked.jl`: Conditioning on partial observations |
| 35 | +- `masked_tuple.jl`: Jointly sampling from continuous and discrete variables, with conditioning |
51 | 36 |
|
52 | 37 | ## Installation
|
53 | 38 |
|
54 | 39 | ```julia
|
55 |
| -using Pkg |
56 |
| -Pkg.add("Flowfusion") |
| 40 | +]add https://github.com/MurrellGroup/Flowfusion.jl |
57 | 41 | ```
|
58 | 42 |
|
59 |
| -## Quick Start |
| 43 | +## A full example |
60 | 44 |
|
61 | 45 | ```julia
|
62 |
| -using Flowfusion, Flux |
63 |
| -#To do. |
| 46 | +using ForwardBackward, Flowfusion, Flux, RandomFeatureMaps, Optimisers, Plots |
| 47 | + |
| 48 | +#Set up a Flux model: X̂1 = model(t,Xt) |
| 49 | +struct FModel{A} |
| 50 | + layers::A |
| 51 | +end |
| 52 | +Flux.@layer FModel |
| 53 | +function FModel(; embeddim = 128, spacedim = 2, layers = 3) |
| 54 | + embed_time = Chain(RandomFourierFeatures(1 => embeddim, 1f0), Dense(embeddim => embeddim, swish)) |
| 55 | + embed_state = Chain(RandomFourierFeatures(2 => embeddim, 1f0), Dense(embeddim => embeddim, swish)) |
| 56 | + ffs = [Dense(embeddim => embeddim, swish) for _ in 1:layers] |
| 57 | + decode = Dense(embeddim => spacedim) |
| 58 | + layers = (; embed_time, embed_state, ffs, decode) |
| 59 | + FModel(layers) |
| 60 | +end |
| 61 | +function (f::FModel)(t, Xt) |
| 62 | + l = f.layers |
| 63 | + tXt = tensor(Xt) |
| 64 | + tv = zero(tXt[1:1,:]) .+ expand(t, ndims(tXt)) |
| 65 | + x = l.embed_time(tv) .+ l.embed_state(tXt) |
| 66 | + for ff in l.ffs |
| 67 | + x = x .+ ff(x) |
| 68 | + end |
| 69 | + tXt .+ l.decode(x) .* (1.05f0 .- expand(t, ndims(tXt))) |
| 70 | +end |
| 71 | + |
| 72 | +model = FModel(embeddim = 256, layers = 3, spacedim = 2) |
| 73 | + |
| 74 | +#Distributions for training: |
| 75 | +T = Float32 |
| 76 | +sampleX0(n_samples) = rand(T, 2, n_samples) .+ 2 |
| 77 | +sampleX1(n_samples) = Flowfusion.random_literal_cat(n_samples, sigma = T(0.05)) |
| 78 | +n_samples = 400 |
| 79 | + |
| 80 | +#The process: |
| 81 | +P = BrownianMotion(0.15f0) |
| 82 | +#P = Deterministic() |
| 83 | + |
| 84 | +#Optimizer: |
| 85 | +eta = 0.001 |
| 86 | +opt_state = Flux.setup(AdamW(eta = eta), model) |
| 87 | + |
| 88 | +iters = 4000 |
| 89 | +for i in 1:iters |
| 90 | + #Set up a batch of training pairs, and t: |
| 91 | + X0 = ContinuousState(sampleX0(n_samples)) |
| 92 | + X1 = ContinuousState(sampleX1(n_samples)) |
| 93 | + t = rand(T, n_samples) |
| 94 | + #Construct the bridge: |
| 95 | + Xt = bridge(P, X0, X1, t) |
| 96 | + #Gradient & update: |
| 97 | + l,g = Flux.withgradient(model) do m |
| 98 | + floss(P, m(t,Xt), X1, scalefloss(P, t)) |
| 99 | + end |
| 100 | + Flux.update!(opt_state, model, g[1]) |
| 101 | + (i % 10 == 0) && println("i: $i; Loss: $l") |
| 102 | +end |
| 103 | + |
| 104 | +#Generate samples by stepping from X0 |
| 105 | +n_inference_samples = 5000 |
| 106 | +X0 = ContinuousState(sampleX0(n_inference_samples)) |
| 107 | +samples = gen(P, X0, model, 0f0:0.005f0:1f0) |
| 108 | + |
| 109 | +#Plotting |
| 110 | +pl = scatter(X0.state[1,:],X0.state[2,:], msw = 0, ms = 1, color = "blue", alpha = 0.5, size = (400,400), legend = :topleft, label = "X0") |
| 111 | +X1true = sampleX1(n_inference_samples) |
| 112 | +scatter!(X1true[1,:],X1true[2,:], msw = 0, ms = 1, color = "orange", alpha = 0.5, label = "X1 (true)") |
| 113 | +scatter!(samples.state[1,:],samples.state[2,:], msw = 0, ms = 1, color = "green", alpha = 0.5, label = "X1 (generated)") |
64 | 114 | ```
|
| 115 | + |
| 116 | +## Tracking trajectories |
| 117 | + |
| 118 | +```julia |
| 119 | +#Generate samples by stepping from X0 |
| 120 | +n_inference_samples = 5000 |
| 121 | +X0 = ContinuousState(sampleX0(n_inference_samples)) |
| 122 | +paths = Tracker() #<- A tracker to record the trajectory |
| 123 | +samples = gen(P, X0, model, 0f0:0.005f0:1f0, tracker = paths) |
| 124 | + |
| 125 | +#Plotting: |
| 126 | +pl = scatter(X0.state[1,:],X0.state[2,:], msw = 0, ms = 1, color = "blue", alpha = 0.5, size = (400,400), legend = :topleft, label = "X0") |
| 127 | +tvec = stack_tracker(paths, :t) |
| 128 | +xttraj = stack_tracker(paths, :xt) |
| 129 | +for i in 1:50:1000 |
| 130 | + plot!(xttraj[1,i,:], xttraj[2,i,:], color = "red", label = i==1 ? "Trajectory" : :none, alpha = 0.4) |
| 131 | +end |
| 132 | +X1true = sampleX1(n_inference_samples) |
| 133 | +scatter!(X1true[1,:],X1true[2,:], msw = 0, ms = 1, color = "orange", alpha = 0.5, label = "X1 (true)") |
| 134 | +scatter!(samples.state[1,:],samples.state[2,:], msw = 0, ms = 1, color = "green", alpha = 0.5, label = "X1 (generated)") |
| 135 | +``` |
0 commit comments