text stringlengths 0 4.99k |
|---|
The Perceiver consists of two modules: a cross-attention module and a standard Transformer with self-attention. |
Cross-attention module |
The cross-attention expects a (latent_dim, projection_dim) latent array, and the (data_dim, projection_dim) data array as inputs, to produce a (latent_dim, projection_dim) latent array as an output. To apply cross-attention, the query vectors are generated from the latent array, while the key and value vectors are gene... |
Note that the data array in this example is the image, where the data_dim is set to the num_patches. |
def create_cross_attention_module( |
latent_dim, data_dim, projection_dim, ffn_units, dropout_rate |
): |
inputs = { |
# Recieve the latent array as an input of shape [1, latent_dim, projection_dim]. |
\"latent_array\": layers.Input(shape=(latent_dim, projection_dim)), |
# Recieve the data_array (encoded image) as an input of shape [batch_size, data_dim, projection_dim]. |
\"data_array\": layers.Input(shape=(data_dim, projection_dim)), |
} |
# Apply layer norm to the inputs |
latent_array = layers.LayerNormalization(epsilon=1e-6)(inputs[\"latent_array\"]) |
data_array = layers.LayerNormalization(epsilon=1e-6)(inputs[\"data_array\"]) |
# Create query tensor: [1, latent_dim, projection_dim]. |
query = layers.Dense(units=projection_dim)(latent_array) |
# Create key tensor: [batch_size, data_dim, projection_dim]. |
key = layers.Dense(units=projection_dim)(data_array) |
# Create value tensor: [batch_size, data_dim, projection_dim]. |
value = layers.Dense(units=projection_dim)(data_array) |
# Generate cross-attention outputs: [batch_size, latent_dim, projection_dim]. |
attention_output = layers.Attention(use_scale=True, dropout=0.1)( |
[query, key, value], return_attention_scores=False |
) |
# Skip connection 1. |
attention_output = layers.Add()([attention_output, latent_array]) |
# Apply layer norm. |
attention_output = layers.LayerNormalization(epsilon=1e-6)(attention_output) |
# Apply Feedforward network. |
ffn = create_ffn(hidden_units=ffn_units, dropout_rate=dropout_rate) |
outputs = ffn(attention_output) |
# Skip connection 2. |
outputs = layers.Add()([outputs, attention_output]) |
# Create the Keras model. |
model = keras.Model(inputs=inputs, outputs=outputs) |
return model |
Transformer module |
The Transformer expects the output latent vector from the cross-attention module as an input, applies multi-head self-attention to its latent_dim elements, followed by feedforward network, to produce another (latent_dim, projection_dim) latent array. |
def create_transformer_module( |
latent_dim, |
projection_dim, |
num_heads, |
num_transformer_blocks, |
ffn_units, |
dropout_rate, |
): |
# input_shape: [1, latent_dim, projection_dim] |
inputs = layers.Input(shape=(latent_dim, projection_dim)) |
x0 = inputs |
# Create multiple layers of the Transformer block. |
for _ in range(num_transformer_blocks): |
# Apply layer normalization 1. |
x1 = layers.LayerNormalization(epsilon=1e-6)(x0) |
# Create a multi-head self-attention layer. |
attention_output = layers.MultiHeadAttention( |
num_heads=num_heads, key_dim=projection_dim, dropout=0.1 |
)(x1, x1) |
# Skip connection 1. |
x2 = layers.Add()([attention_output, x0]) |
# Apply layer normalization 2. |
x3 = layers.LayerNormalization(epsilon=1e-6)(x2) |
# Apply Feedforward network. |
ffn = create_ffn(hidden_units=ffn_units, dropout_rate=dropout_rate) |
x3 = ffn(x3) |
# Skip connection 2. |
x0 = layers.Add()([x3, x2]) |
# Create the Keras model. |
model = keras.Model(inputs=inputs, outputs=x0) |
return model |
Perceiver model |
The Perceiver model repeats the cross-attention and Transformer modules num_iterations times—with shared weights and skip connections—to allow the latent array to iteratively extract information from the input image as it is needed. |
class Perceiver(keras.Model): |
def __init__( |
self, |
patch_size, |
data_dim, |
latent_dim, |
projection_dim, |
num_heads, |
num_transformer_blocks, |
ffn_units, |
dropout_rate, |
num_iterations, |
classifier_units, |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.