[Home]

Flow Network based Generative Models for Non-Iterative Diverse Candidate Generation

Emmanuel Bengio, Moksh Jain, Maksym Korablyov, Doina Precup, Yoshua Bengio

arXiv preprint, code
also see the GFlowNet Foundations paper
and a more recent (and thorough) tutorial on the framework.

What follows is a high-level overview of this work, for more details refer to our paper. Given a reward R ( x ) R(x) and a deterministic episodic environment where episodes end with a ``generate x x '' action, how do we generate diverse and high-reward x x s?
We propose to use Flow Networks to model discrete p ( x ) R ( x ) p(x) \propto R(x) from which we can sample sequentially (like episodic RL, rather than iteratively as MCMC methods would). We show that our method, GFlowNet, is very useful on a combinatorial domain, drug molecule synthesis, because unlike RL methods it generates diverse x x s by design.

Flow Networks

A flow network is a directed graph with sources and sinks, and edges carrying some amount of flow between them through intermediate nodes -- think of pipes of water. For our purposes, we define a flow network with a single source, the root or s 0 s_0 ; the sinks of the network correspond to the terminal states. We'll assign to each sink x x an ``out-flow'' R ( x ) R(x) .
s 0 s_{0} s 1 s_{1} s 2 s_{2} s 3 s_{3} x 3 x_{3} \top s 5 s_{5} x 5 x_{5} \top s 7 s_{7} s 8 s_{8} x 8 x_{8} \top s 10 s_{10} s 11 s_{11} x 11 x_{11} \top s 13 s_{13} x 13 x_{13} \top s 15 s_{15} s 16 s_{16} x 16 x_{16} \top
Given the graph structure and the out-flow of the sinks, we wish to calculate a valid flow between nodes, e.g. how much water each pipe is carrying. Generally there can be infinite solutions, but this is not a problem here -- any valid solution will do. For example above, there is almost no flow between s 7 s_7 and s 13 s_{13} that goes through s 11 s_{11} , it all goes through s 10 s_{10} , but the reverse solution would also be a valid flow.
Why is this useful? Such a construction corresponds to a generative model. If we follow the flow, we'll end up in a terminal state, a sink, with probability p ( x ) R ( x ) p(x) \propto R(x) . On top of that, we'll have the property that the in-flow of s 0 s_0 --the flow of the unique source--is x R ( x ) = Z \sum_x R(x)=Z , the partition function. If we assign to each intermediate node a state and to each edge an action, we recover a useful MDP.
Let F ( s , a ) = f ( s , s ) F(s,a)=f(s,s') be the flow between s s and s s' , where T ( s , a ) = s T(s,a)=s' , i.e. s s' is the (deterministic) state transitioned to from state s s and action a a . Let π ( a s ) = F ( s , a ) a F ( s , a ) \begin{aligned}\pi(a|s) = \frac{F(s,a)}{\sum_{a'}F(s,a')}\end{aligned} then following policy π \pi , starting from s 0 s_0 , leads to terminal state x x with probability R ( x ) R(x) (see the paper for proofs and more rigorous explanations).

Approximating Flow Networks

As you may suspect, there are only few scenarios in which we can build the above graph explicitly. For drug-like molecules, it would have around 1 0 16 10^{16} nodes!
Instead, we resort to function approximation, just like deep RL resorts to it when computing the (action-)value functions of MDPs.
Our goal here is to approximate the flow F ( s , a ) F(s,a) . Earlier we called a valid flow one that correctly routed all the flow from the source to the sinks through the intermediary nodes. Let's be more precise. For some node s s' , let the in-flow F ( s ) F(s') be the sum of incoming flows: F ( s ) = s , a : T ( s , a ) = s F ( s , a ) \begin{aligned}F(s') = \sum_{s,a:T(s,a)=s'} F(s,a)\end{aligned} Here the set { s , a : T ( s , a ) = s } \{s,a:T(s,a)=s'\} is the set of state-action pairs that lead to s s' . Now, let the out-flow be the sum of outgoing flows--or the reward if s s' is terminal: F ( s ) = R ( s ) + a A ( s ) F ( s , a ) . \begin{aligned}F(s') = R(s') + \sum_{a'\in\mathcal{A}(s')} F(s',a').\end{aligned} Note that we reused F ( s ) F(s') . This is because for a valid flow, the in-flow is equal to the out-flow, i.e. the flow through s s' , F ( s ) F(s') . Here A ( s ) \mathcal{A}(s) is the set of valid actions in state s s , which is the empty set when s s is a sink. R ( s ) R(s) is 0 unless s s is a sink, in which case R ( s ) > 0 R(s)>0 .
We can thus call the set of these equalities for all states s s 0 s'\neq s_0 the flow consistency equations: s , a : T ( s , a ) = s F ( s , a ) = R ( s ) + a A ( s ) F ( s , a ) . \begin{aligned}\sum_{s,a:T(s,a)=s'} F(s,a) = R(s') + \sum_{a'\in\mathcal{A}(s')} F(s',a').\end{aligned}
a 1 a_1 a 7 a_7 a 3 a_3 a 4 a_4 a 2 a_2 a 8 a_8 s 0 s_{0} s 1 s_{1} s 2 s_{2} s 3 s_{3} s 4 s_{4} s 5 s_{5} s 6 s_{6}
Here the set of parents { s , a : T ( s , a ) = s 3 } \{s,a:T(s,a)=s_3\} is { ( s 0 , a 1 ) , ( s 1 , a 7 ) , ( s 2 , a 3 ) } \{(s_0, a_1), (s_1, a_7), (s_2, a_3)\} , and A ( s 3 ) = { a 2 , a 4 , a 8 } \mathcal{A}(s_3)=\{a_2,a_4,a_8\} .
By now our RL senses should be tingling. We've defined a value function recursively, with two quantities that need to match.

A TD-Like Objective

Just like one can cast the Bellman equations into TD objectives, so do we cast the flow consistency equations into an objective. We want F θ F_\theta that minimizes the square difference between the two sides of the equations, but we add a few bells and whistles: L θ , ϵ ( τ ) = s τ s 0 ( log  ⁣ [ ϵ + s , a : T ( s , a ) = s exp F θ log ( s , a ) ] log  ⁣ [ ϵ + R ( s ) + a A ( s ) exp F θ log ( s , a ) ] ) 2 . \begin{aligned}\mathcal{L}_{\theta,\epsilon}(\tau) = \sum_{\mathclap{s'\in\tau\neq s_0}}\,\left(\log\! \left[\epsilon+{\sum_{\mathclap{s,a:T(s,a)=s'}}} \exp F^{\log}_\theta(s,a)\right]- \log\! \left[\epsilon + R(s') + \sum_{\mathclap{a'\in{\cal A}(s')}} \exp F^{\log}_\theta(s',a')\right]\right)^2.\end{aligned} First, we match the log \log of each side, which is important since as intermediate nodes get closer to the root, their flow will become exponentially bigger (remember that F ( s 0 ) = Z = x R ( x ) F(s_0) = Z = \sum_x R(x) ), but we care equally about all nodes. Second, we predict F θ log log F F^{\log}_\theta\approx\log F for the same reasons. Finally, we add an ϵ \epsilon value inside the log \log ; this doesn't change the minima of the objective, but gives more gradient weight to large values and less to small values.
We show in the paper that a minimizer of this objective achieves our desiderata, which is to have p ( x ) R ( x ) p(x)\propto R(x) when sampling from π ( a s ) \pi(a|s) as defined above.

GFlowNet as Amortized Sampling with an OOD Potential

It is interesting to compare GFlowNet with Monte-Carlo Markov Chain (MCMC) methods. MCMC methods can be used to sample from a distribution for which there is no analytical sampling formula but an energy function or unnormalized probability function is available. In our context, this unnormalized probability function is our reward function R ( x ) = e e n e r g y ( x ) R(x)=e^{-energy(x)} .
Like MCMC methods, GFlowNet can turn a given energy function into samples but it does it in an amortized way, converting the cost a lot of very expensive MCMC trajectories (to obtain each sample) into the cost training a generative model (in our case a generative policy which sequentially builds up x x ). Sampling from the generative model is then very cheap (e.g. adding one component at a time to a molecule) compared to an MCMC. But the most important gain may not be just computational, but in terms of the ability to discover new modes of the reward function.
MCMC methods are iterative, making many small noisy steps, which can converge in the neighborhood of a mode, and with some probability jump from one mode to a nearby one. However, if two modes are far from each other, MCMC can require exponential time to mix between the two. If in addition the modes occupy a tiny volume of the state space, the chances of initializing a chain near one of the unknown modes is also tiny, and the MCMC approach becomes unsatisfactory. Whereas such a situation seems hopeless with MCMC, GFlowNet has the potential to discover modes and jump there directly, if there is structure that relates the modes that it already knows, and if its inductive biases and training procedure make it possible to generalize there.
GFlowNet does not need to perfectly know where the modes are: it is sufficient to make guesses which occasionally work well. Like for MCMC methods, once a point in the region of new mode is discovered, further training of GFlowNet will sculpt that mode and zoom in on its peak.
Note that we can put R ( x ) R(x) to some power β \beta , a coefficient which acts like a temperature, and R ( x ) β = e β    e n e r g y ( x ) R(x)^\beta = e^{-\beta\; energy(x)} , making it possible to focus more or less on the highest modes (versus spreading probability mass more uniformly).

Generating molecule graphs

The motivation for this work is to be able to generate diverse molecules from a proxy reward R R that is imprecise because it comes from biochemical simulations that have a high uncertainty. As such, we do not care about the maximizer as RL methods would, but rather about a set of ``good enough'' candidates to send to a true biochemical assay.
Another motivation is to have diversity: by fitting the distribution of rewards rather than trying to maximize the expected reward, we're likely to find more modes than if we were being greedy after having found a good enough mode, which again and again we've found RL methods such as PPO to do.
Here we generate molecule graphs via a sequence of additive edits, i.e. we progressively build the graph by adding new leaf nodes to it. We also create molecules block-by-block rather than atom-by-atom.
We find experimentally that we get both good molecules, and diverse ones. We compare ourselves to PPO and MARS (an MCMC-based method).
Figure 3 shows that we're fitting a distribution that makes sense. If we change the reward by exponentiating it as R β R^\beta with β > 1 \beta>1 , this shifts the reward distribution to the right.
Figure 4 shows the top- k k found as a function of the number of episodes.

Finally, Figure 5 shows that using a biochemical measure of diversity to estimate the number of distinct modes found, GFlowNet finds much more varied candidates.

Active Learning experiments

The above experiments assume access to a reward R R that is cheap to evaluate. In fact it uses a neural network proxy trained from a large dataset of molecules. This setup isn't quite what we would get when interacting with biochemical assays, where we'd have access to much fewer data. To emulate such a setting, we consider our oracle to be a docking simulation (which is relatively expensive to run, ~30 cpu seconds).
In this setting, there is a limited budget for calls to the true oracle O O . We use a proxy M M initialized by training on a limited dataset of ( x , R ( x ) ) (x, R(x)) pairs D 0 D_0 , where R ( x ) R(x) is the true reward from the oracle. The generative model ( π θ \pi_{\theta} ) is then trained to fit R R but as predicted by the proxy M M . We then sample a batch B = { x 1 , x 2 , x k } B=\{x_1, x_2, \dots x_k\} where x i π θ x_i\sim \pi_{\theta} , which is evaluated with the oracle O O . The proxy M M is updated with this newly acquired and labeled batch, and the process is repeated for N N iterations.
By doing this on the molecule setting we again find that we can generate better molecules. This showcases the importance of having these diverse candidates.

For more figures, experiments and explanations, check out the paper, or reach out to us!