4.4 C
Canberra
Monday, October 27, 2025

Posit AI Weblog: Stepping into the circulation: Bijectors in TensorFlow Likelihood


As of right this moment, deep studying’s best successes have taken place within the realm of supervised studying, requiring heaps and many annotated coaching information. Nevertheless, information doesn’t (usually) include annotations or labels. Additionally, unsupervised studying is enticing due to the analogy to human cognition.

On this weblog to this point, we now have seen two main architectures for unsupervised studying: variational autoencoders and generative adversarial networks. Lesser recognized, however interesting for conceptual in addition to for efficiency causes are normalizing flows (Jimenez Rezende and Mohamed 2015). On this and the subsequent put up, we’ll introduce flows, specializing in how you can implement them utilizing TensorFlow Likelihood (TFP).

In distinction to earlier posts involving TFP that accessed its performance utilizing low-level $-syntax, we now make use of tfprobability, an R wrapper within the type of keras, tensorflow and tfdatasets. A word concerning this bundle: It’s nonetheless below heavy improvement and the API could change. As of this writing, wrappers don’t but exist for all TFP modules, however all TFP performance is accessible utilizing $-syntax if want be.

Density estimation and sampling

Again to unsupervised studying, and particularly pondering of variational autoencoders, what are the principle issues they offer us? One factor that’s seldom lacking from papers on generative strategies are photos of super-real-looking faces (or mattress rooms, or animals …). So evidently sampling (or: technology) is a vital half. If we will pattern from a mannequin and acquire real-seeming entities, this implies the mannequin has discovered one thing about how issues are distributed on the earth: it has discovered a distribution.
Within the case of variational autoencoders, there may be extra: The entities are speculated to be decided by a set of distinct, disentangled (hopefully!) latent elements. However this isn’t the idea within the case of normalizing flows, so we aren’t going to elaborate on this right here.

As a recap, how will we pattern from a VAE? We draw from (z), the latent variable, and run the decoder community on it. The end result ought to – we hope – seem like it comes from the empirical information distribution. It shouldn’t, nevertheless, look precisely like every of the objects used to coach the VAE, or else we now have not discovered something helpful.

The second factor we could get from a VAE is an evaluation of the plausibility of particular person information, for use, for instance, in anomaly detection. Right here “plausibility” is imprecise on goal: With VAE, we don’t have a way to compute an precise density below the posterior.

What if we would like, or want, each: technology of samples in addition to density estimation? That is the place normalizing flows are available.

Normalizing flows

A circulation is a sequence of differentiable, invertible mappings from information to a “good” distribution, one thing we will simply pattern from and use to calculate a density. Let’s take as instance the canonical solution to generate samples from some distribution, the exponential, say.

We begin by asking our random quantity generator for some quantity between 0 and 1:

This quantity we deal with as coming from a cumulative chance distribution (CDF) – from an exponential CDF, to be exact. Now that we now have a price from the CDF, all we have to do is map that “again” to a price. That mapping CDF -> worth we’re in search of is simply the inverse of the CDF of an exponential distribution, the CDF being

[F(x) = 1 – e^{-lambda x}]

The inverse then is

[
F^{-1}(u) = -frac{1}{lambda} ln (1 – u)
]

which implies we could get our exponential pattern doing

lambda <- 0.5 # decide some lambda
x <- -1/lambda * log(1-u)

We see the CDF is definitely a circulation (or a constructing block thereof, if we image most flows as comprising a number of transformations), since

  • It maps information to a uniform distribution between 0 and 1, permitting to evaluate information chance.
  • Conversely, it maps a chance to an precise worth, thus permitting to generate samples.

From this instance, we see why a circulation needs to be invertible, however we don’t but see why it needs to be differentiable. This can turn into clear shortly, however first let’s check out how flows can be found in tfprobability.

Bijectors

TFP comes with a treasure trove of transformations, known as bijectors, starting from easy computations like exponentiation to extra advanced ones just like the discrete cosine remodel.

To get began, let’s use tfprobability to generate samples from the conventional distribution.
There’s a bijector tfb_normal_cdf() that takes enter information to the interval ([0,1]). Its inverse remodel then yields a random variable with the usual regular distribution:

Conversely, we will use this bijector to find out the (log) chance of a pattern from the conventional distribution. We’ll examine in opposition to an easy use of tfd_normal within the distributions module:

x <- 2.01
d_n <- tfd_normal(loc = 0, scale = 1) 

d_n %>% tfd_log_prob(x) %>% as.numeric() # -2.938989

To acquire that very same log chance from the bijector, we add two elements:

  • Firstly, we run the pattern by means of the ahead transformation and compute log chance below the uniform distribution.
  • Secondly, as we’re utilizing the uniform distribution to find out chance of a standard pattern, we have to monitor how chance adjustments below this transformation. That is performed by calling tfb_forward_log_det_jacobian (to be additional elaborated on under).
b <- tfb_normal_cdf()
d_u <- tfd_uniform()

l <- d_u %>% tfd_log_prob(b %>% tfb_forward(x))
j <- b %>% tfb_forward_log_det_jacobian(x, event_ndims = 0)

(l + j) %>% as.numeric() # -2.938989

Why does this work? Let’s get some background.

Likelihood mass is conserved

Flows are primarily based on the precept that below transformation, chance mass is conserved. Say we now have a circulation from (x) to (z):
[z = f(x)]

Suppose we pattern from (z) after which, compute the inverse remodel to acquire (x). We all know the chance of (z). What’s the chance that (x), the remodeled pattern, lies between (x_0) and (x_0 + dx)?

This chance is (p(x) dx), the density occasions the size of the interval. This has to equal the chance that (z) lies between (f(x)) and (f(x + dx)). That new interval has size (f'(x) dx), so:

[p(x) dx = p(z) f'(x) dx]

Or equivalently

[p(x) = p(z) * dz/dx]

Thus, the pattern chance (p(x)) is decided by the bottom chance (p(z)) of the remodeled distribution, multiplied by how a lot the circulation stretches area.

The identical goes in greater dimensions: Once more, the circulation is in regards to the change in chance quantity between the (z) and (y) areas:

[p(x) = p(z) frac{vol(dz)}{vol(dx)}]

In greater dimensions, the Jacobian replaces the by-product. Then, the change in quantity is captured by absolutely the worth of its determinant:

[p(mathbf{x}) = p(f(mathbf{x})) bigg|detfrac{partial f({mathbf{x})}}{partial{mathbf{x}}}bigg|]

In follow, we work with log possibilities, so

[log p(mathbf{x}) = log p(f(mathbf{x})) + log bigg|detfrac{partial f({mathbf{x})}}{partial{mathbf{x}}}bigg| ]

Let’s see this with one other bijector instance, tfb_affine_scalar. Beneath, we assemble a mini-flow that maps a couple of arbitrary chosen (x) values to double their worth (scale = 2):

x <- c(0, 0.5, 1)
b <- tfb_affine_scalar(shift = 0, scale = 2)

To match densities below the circulation, we select the conventional distribution, and have a look at the log densities:

d_n <- tfd_normal(loc = 0, scale = 1)
d_n %>% tfd_log_prob(x) %>% as.numeric() # -0.9189385 -1.0439385 -1.4189385

Now apply the circulation and compute the brand new log densities as a sum of the log densities of the corresponding (x) values and the log determinant of the Jacobian:

z <- b %>% tfb_forward(x)

(d_n  %>% tfd_log_prob(b %>% tfb_inverse(z))) +
  (b %>% tfb_inverse_log_det_jacobian(z, event_ndims = 0)) %>%
  as.numeric() # -1.6120857 -1.7370857 -2.1120858

We see that because the values get stretched in area (we multiply by 2), the person log densities go down.
We will confirm the cumulative chance stays the identical utilizing tfd_transformed_distribution():

d_t <- tfd_transformed_distribution(distribution = d_n, bijector = b)
d_n %>% tfd_cdf(x) %>% as.numeric()  # 0.5000000 0.6914625 0.8413447

d_t %>% tfd_cdf(y) %>% as.numeric()  # 0.5000000 0.6914625 0.8413447

To date, the flows we noticed have been static – how does this match into the framework of neural networks?

Coaching a circulation

On condition that flows are bidirectional, there are two methods to consider them. Above, we now have largely burdened the inverse mapping: We wish a easy distribution we will pattern from, and which we will use to compute a density. In that line, flows are typically known as “mappings from information to noise” – noise largely being an isotropic Gaussian. Nevertheless in follow, we don’t have that “noise” but, we simply have information.
So in follow, we now have to be taught a circulation that does such a mapping. We do that by utilizing bijectors with trainable parameters.
We’ll see a quite simple instance right here, and go away “actual world flows” to the subsequent put up.

The instance relies on half 1 of Eric Jang’s introduction to normalizing flows. The primary distinction (aside from simplification to point out the essential sample) is that we’re utilizing keen execution.

We begin from a two-dimensional, isotropic Gaussian, and we need to mannequin information that’s additionally regular, however with a imply of 1 and a variance of two (in each dimensions).

library(tensorflow)
library(tfprobability)

tfe_enable_eager_execution(device_policy = "silent")

library(tfdatasets)

# the place we begin from
base_dist <- tfd_multivariate_normal_diag(loc = c(0, 0))

# the place we need to go
target_dist <- tfd_multivariate_normal_diag(loc = c(1, 1), scale_identity_multiplier = 2)

# create coaching information from the goal distribution
target_samples <- target_dist %>% tfd_sample(1000) %>% tf$forged(tf$float32)

batch_size <- 100
dataset <- tensor_slices_dataset(target_samples) %>%
  dataset_shuffle(buffer_size = dim(target_samples)[1]) %>%
  dataset_batch(batch_size)

Now we’ll construct a tiny neural community, consisting of an affine transformation and a nonlinearity.
For the previous, we will make use of tfb_affine, the multi-dimensional relative of tfb_affine_scalar.
As to nonlinearities, at the moment TFP comes with tfb_sigmoid and tfb_tanh, however we will construct our personal parameterized ReLU utilizing tfb_inline:

# alpha is a learnable parameter
bijector_leaky_relu <- operate(alpha) {
  
  tfb_inline(
    # ahead remodel leaves optimistic values untouched and scales destructive ones by alpha
    forward_fn = operate(x)
      tf$the place(tf$greater_equal(x, 0), x, alpha * x),
    # inverse remodel leaves optimistic values untouched and scales destructive ones by 1/alpha
    inverse_fn = operate(y)
      tf$the place(tf$greater_equal(y, 0), y, 1/alpha * y),
    # quantity change is 0 when optimistic and 1/alpha when destructive
    inverse_log_det_jacobian_fn = operate(y) {
      I <- tf$ones_like(y)
      J_inv <- tf$the place(tf$greater_equal(y, 0), I, 1/alpha * I)
      log_abs_det_J_inv <- tf$log(tf$abs(J_inv))
      tf$reduce_sum(log_abs_det_J_inv, axis = 1L)
    },
    forward_min_event_ndims = 1
  )
}

Outline the learnable variables for the affine and the PReLU layers:

d <- 2 # dimensionality
r <- 2 # rank of replace

# shift of affine bijector
shift <- tf$get_variable("shift", d)
# scale of affine bijector
L <- tf$get_variable('L', c(d * (d + 1) / 2))
# rank-r replace
V <- tf$get_variable("V", c(d, r))

# scaling issue of parameterized relu
alpha <- tf$abs(tf$get_variable('alpha', checklist())) + 0.01

With keen execution, the variables have for use contained in the loss operate, so that’s the place we outline the bijectors. Our little circulation now could be a tfb_chain of bijectors, and we wrap it in a TransformedDistribution (tfd_transformed_distribution) that hyperlinks supply and goal distributions.

loss <- operate() {
  
 affine <- tfb_affine(
        scale_tril = tfb_fill_triangular() %>% tfb_forward(L),
        scale_perturb_factor = V,
        shift = shift
      )
 lrelu <- bijector_leaky_relu(alpha = alpha)  
 
 circulation <- checklist(lrelu, affine) %>% tfb_chain()
 
 dist <- tfd_transformed_distribution(distribution = base_dist,
                          bijector = circulation)
  
 l <- -tf$reduce_mean(dist$log_prob(batch))
 # maintain monitor of progress
 print(spherical(as.numeric(l), 2))
 l
}

Now we will really run the coaching!

optimizer <- tf$prepare$AdamOptimizer(1e-4)

n_epochs <- 100
for (i in 1:n_epochs) {
  iter <- make_iterator_one_shot(dataset)
  until_out_of_range({
    batch <- iterator_get_next(iter)
    optimizer$reduce(loss)
  })
}

Outcomes will differ relying on random initialization, however you need to see a gradual (if gradual) progress. Utilizing bijectors, we now have really educated and outlined just a little neural community.

Outlook

Undoubtedly, this circulation is just too easy to mannequin advanced information, however it’s instructive to have seen the essential ideas earlier than delving into extra advanced flows. Within the subsequent put up, we’ll try autoregressive flows, once more utilizing TFP and tfprobability.

Jimenez Rezende, Danilo, and Shakir Mohamed. 2015. “Variational Inference with Normalizing Flows.” arXiv e-Prints, Could, arXiv:1505.05770. https://arxiv.org/abs/1505.05770.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

[td_block_social_counter facebook="tagdiv" twitter="tagdivofficial" youtube="tagdiv" style="style8 td-social-boxed td-social-font-icons" tdc_css="eyJhbGwiOnsibWFyZ2luLWJvdHRvbSI6IjM4IiwiZGlzcGxheSI6IiJ9LCJwb3J0cmFpdCI6eyJtYXJnaW4tYm90dG9tIjoiMzAiLCJkaXNwbGF5IjoiIn0sInBvcnRyYWl0X21heF93aWR0aCI6MTAxOCwicG9ydHJhaXRfbWluX3dpZHRoIjo3Njh9" custom_title="Stay Connected" block_template_id="td_block_template_8" f_header_font_family="712" f_header_font_transform="uppercase" f_header_font_weight="500" f_header_font_size="17" border_color="#dd3333"]
- Advertisement -spot_img

Latest Articles