English (unofficial) translations of posts at kexue.fm
Source

UniVAE: A Single-Model, Multi-Scale VAE Based on Transformer

Translated by DeepSeek V4 Pro. Translations can be inaccurate, please refer to the original post for important stuff.

As is well known, the \mathcal{O}(n^2) complexity of the Transformer is one of its "fatal flaws." However, every disadvantage has its advantage; the \mathcal{O}(n^2) complexity also provides significant room for maneuver. We can flexibly customize different attention masks to design Transformer models for various purposes, such as UniLM and K-BERT.

This article introduces a UniVAE model I conceived for text. It follows a similar logic to UniLM, integrating a VAE into a single Transformer model while also possessing multi-scale characteristics.

UniAE

I won’t provide a general introduction to VAE (Variational Autoencoder) here, as there are already several articles on this site covering it; feel free to search for them. A VAE can be understood as an AE (Autoencoder) with a regularization term. Generally, the Encoder is responsible for encoding the input into a vector that satisfies a certain distribution, while the Decoder is responsible for reconstructing the input from that encoding vector. Therefore, to implement UniVAE, we must first implement the corresponding UniAE.

In "From Language Models to Seq2Seq: Transformer is All About the Mask", we introduced UniLM (Uni stands for Unified), which uses the Attention Mask shown on the left in the figure below to enable the Transformer to perform Seq2Seq tasks. However, UniLM is not the UniAE we are looking for, because the Decoder part of UniLM relates to the entire encoded sequence of the input, rather than a single vector.

UniAE-style Attention Mask

However, we can further adjust the Attention Mask based on UniLM to the pattern shown on the right. In this way, decoding can only rely on the [CLS] vector of the encoding part and the already completed decoding results. This is the UniAE-style Attention Mask we are looking for. Since the input depends only on the [CLS] vector, and the size of the [CLS] vector is fixed, the source information during the generation process is essentially a fixed-size vector, and the input is also encoded into this fixed-size vector. This fulfills the AE function.

Schematic of UniAE-style Attention Correlation

Multi-scale

In other words, through the UniAE-style Attention Mask, we can implement a Seq2Seq model similar to UniLM, which is equivalent to the Encoder encoding the input into a fixed-length vector and the Decoder decoding that vector. If this is still not clear, we can also decompose it into an Encoder-Decoder architecture to understand it, as shown below:

Understanding by decomposing into an Encoder-Decoder structure

The difference from a conventional Seq2Seq architecture is that the weights of the Encoder and Decoder here are shared. As can be seen from the figure above, if we add this kind of Mask to every layer of Attention, the Decoder will depend on the [CLS] vector of every input layer. This means that if there are L layers of Attention, the concatenation of all [CLS] vectors from the input sequences of these L layers constitutes the complete encoding vector of the input text (of course, the first layer can be omitted because the [CLS] of the first layer is its Embedding vector, which is a constant vector for every input). The [CLS] vector of a single layer is not the complete encoding vector.

For the Decoder, every layer of Attention has a [CLS] vector passed in, which actually forms a multi-scale structure. In Computer Vision (CV), the most advanced generative models are basically multi-scale structures, such as StyleGAN, Glow, and NVAE. However, this seems less common in NLP. It is not hard to imagine that in a multi-scale structure, the degree of control that inputs at different levels have over the generation results varies. Variables closer to the input layer control "trivial" parts, while variables closer to the output layer control the key information of the generation results. Ideally, after training a multi-scale model, we can achieve control over different levels of the generation results by editing input variables at different levels.

Reducing Dimensionality

Some readers might think: if the dimension of each layer is d and there are L layers, then concatenating all [CLS] vectors would result in Ld dimensions. For BERT-base, this would be 12 \times 768 = 9216 dimensions. Isn’t this encoding vector dimension too large? Indeed, for an ordinary AE or VAE, an encoding vector of nearly ten thousand dimensions is too large.

Schematic of the dimensionality reduction process

The solution is actually very simple. We only need to use a fully connected layer to first reduce the dimensionality of the [CLS] vector of each layer, and then use another fully connected layer to increase the dimensionality back, finally concatenating it with the remaining (L-1) vectors of dimension d, as shown in the figure above. In this way, although the input sequence is still of size L \times d, the [CLS] vector can actually be expressed by a lower-dimensional vector. We only need to concatenate these lower-dimensional vectors from each layer to serve as the total encoding vector.

Schematic of the Encoder-Decoder after dimensionality reduction

Decoupling Capability

The previous design and discussion were targeted at ordinary AEs. For a VAE, it involves adding a reparameterization operation to the AE’s encoding vector and adding a KL divergence term to the loss function. Therefore, once UniAE is designed, UniVAE is theoretically designed as well.

However, in practice, there is still room for improvement. Theoretically, a well-trained VAE possesses a certain degree of disentanglement capability, meaning that each dimension of the latent variables is independent and controls a specific aspect of the generation result, allowing for random adjustment. Disentanglement is a very challenging task. If the VAE’s Encoder can produce disentangled encoding vectors, its fitting capability must be relatively strong; in other words, its structure needs to be sufficiently complex.

Looking back at the Encoder of UniAE, its encoding vector is the concatenation of the [CLS] vectors (or corresponding low-dimensional vectors) from each layer. For the earlier layers, their [CLS] vectors are only the outputs of a few Transformer layers. Their encoding capability is very weak and insufficient to produce disentangled vectors. Therefore, using them as latent variables for the VAE is inappropriate.

Thus, when designing UniVAE in practice, we should not use all [CLS] vectors of UniAE as the encoding vector. We should set a starting layer number; the Decoder only uses [CLS] vectors from layers greater than this number, while [CLS] vectors from layers less than or equal to this number are not used. This corresponds to using the Attention Mask on the right in the figure below:

Near the input layer, use Independent Attention Mask

This is equivalent to the following Encoder-Decoder structure:

Schematic effect of using Independent Mask for the first two layers of Attention

Other Details

At this point, the key parts of UniVAE have been introduced. Below, I will share some important details from the implementation process.

First is the issue of length leakage. Whether it is UniLM or UniVAE, because the Encoder and Decoder are integrated into a single model, we concatenate the input and output as a single sample for training. Consequently, the starting position of the Decoder part for each sample is different, depending on the length of the input text. This means the input length is also passed into the Decoder as an input condition, which is length leakage.

There are two solutions to this problem: the first is to make all inputs the same length through truncation or padding, which prevents length leakage; the second is even simpler—do nothing. That is, treat the length as a conditional input and control the generation length by controlling the starting position. However, a potential issue is that the length information might not be completely decoupled from the encoding vector, so the same encoding vector paired with different lengths might yield unreasonable results.

Then there is the issue of choosing the number of layers and dimensions. As mentioned earlier, to give the latent variables better decoupling capability, we add an independent Attention Mask to the first k layers of Attention, and the remaining L-k layers use the UniAE-style Attention Mask. How should k be chosen? This is a hyperparameter that needs careful adjustment. A smaller k retains more information, which is beneficial for reconstruction but not for decoupling; conversely, a larger k is more conducive to decoupling but detrimental to reconstruction. In my experiments, k=8 was used.

A similar issue arises in the choice of dimensionality for reduction. A larger dimension is naturally beneficial for reconstruction but also hinders decoupling, and vice versa. This parameter needs to be adjusted specifically based on the complexity of the task itself. The general direction for adjustment is to observe the random sampling effect and the reconstruction effect. If most of the randomly sampled samples are readable and the reconstruction of natural sentences is also good, then the dimension is appropriate; otherwise, it needs to be adjusted accordingly.

Finally, it is worth mentioning that the UniAE design can be used not only for VAEs but also for constructing VQ-VAEs. One only needs to quantize each [CLS] vector, and it becomes a VQ-VAE model that encodes variable-length sentences into fixed-length discrete sequences.

Reference Implementation

Here is a reference implementation of UniVAE:

The code uses the vMF-VAE variant, implemented based on bert4keras, with RoFormer as the base architecture (which can also be replaced with BERT). Below are the results of UniVAE trained on a question dataset.

Random Sampling Results

  • How can I play the game I downloaded on Steam on my computer???

  • Which andrology hospital in Hohhot is better and more affordable?

  • I have high blood pressure, and my mother’s hands and feet are numb. What’s going on?

  • How to check traffic violation records and penalties?

  • Why is there a bit of lag in the questions I ask?

  • Does Xiaomi 2s use a China Mobile or China Unicom card?

  • How to develop early childhood education in kindergartens?

  • Is the ranking of graduate schools in the UK important for international students?

  • Are there professional training institutions for Excel spreadsheet databases?

  • Why is it easy to cough at night, but not when I’m awake?

Reconstruction Results

  • Original: The digital TV set-top box is broken, can it be repaired for free?
    Reconstruction: The digital TV set-top box is broken, can it be replaced?

  • Original: What is good to stir-fry with green peppers?
    Reconstruction: What is good to stir-fry with green peppers?

  • Original: What does "carry you" mean in Honor of Kings?
    Reconstruction: What does "carry Mulan" mean in Honor of Kings?

  • Original: I don’t have a cold but I’m always coughing, what medicine should I take?
    Reconstruction: I don’t have a cold but I’m always coughing, what medicine should I take?

  • Original: How is Qinyuan (Jinke Xicheng Dayuan store), default reviews of whether it’s good or not
    Reconstruction: How is Qinyuan (Jinyuan store), default reviews of whether it’s good or not

Randomly Replacing the First 32 Dimensions of Latent Variables

  • Original: What medicine should I take for bleeding gums?

  • Results:

    • What anti-inflammatory medicine is good for bleeding gums?

    • Is Amoxicillin effective for bleeding gums?

    • Is bleeding gums caused by liver fire?

    • How much does it cost to go to the hospital for bleeding gums?

    • Which department should I go to for bleeding gums at the dentist?

    • Where is a good place to see a dentist in Shenzhen for bleeding gums?

  • Original: Which is more fun, Guangzhou or Shenzhen?

  • Results:

    • Which city is better developed, Guangzhou or Shenzhen? High salary?

    • Guangzhou and Shenzhen, which is more developed? Is the flight from Shenzhen to Guangzhou expensive?

    • Which is better compared between Guangzhou and Shenzhen?

    • Which has a higher GDP per capita, Guangzhou or Shenzhen?

    • Housing price increases in Guangzhou and Shenzhen.

    • Are the self-study exams the same in Guangzhou and Shenzhen?

Randomly Replacing the Last 16 Dimensions of Latent Variables

  • Original: What medicine should I take for bleeding gums?

  • Results:

    • What business is good to do in the next 21 years?

    • What harm does eczema do to the body?

    • What configuration should I buy for the Lavida now?

    • How much does a visa for Malaysia cost?

    • What fruit is good to give children in the morning?

    • Which department should I go to for dizziness and fever?

  • Original: Which is more fun, Guangzhou or Shenzhen?

  • Results:

    • What is the difference between 99 and 98?

    • How to change the phone number for WeChat and Alipay?

    • My nails and flesh are very different, what’s going on?

    • How long after taking Metronidazole can I drink alcohol?

    • Can longan and red dates be used to make tea together?

    • Which is better, Xiaomi or Huawei?

As can be seen, the random sampling and reconstruction effects are quite good. By randomly replacing latent variables of different dimensions, we can roughly observe the effect of the multi-scale structure: replacing the earlier dimensions of the latent variables roughly keeps the keywords (topics) unchanged; replacing the later dimensions roughly keeps the sentence structure unchanged. Of course, the structural nature of natural language itself is weak, so there are usually some exceptions in the examples.

Summary

This article introduced the UniVAE design I conceived. It follows a logic similar to UniLM, integrating a VAE into a single Transformer model through a specific Attention Mask, and it also possesses multi-scale characteristics. In addition to conventional VAE models, this design can also be applied to models like VQ-VAE.

Reprinted from: https://kexue.fm/archives/8475

For more details on reprinting, please refer to: "Scientific Space FAQ"