text stringlengths 0 4.99k |
|---|
): |
super(Perceiver, self).__init__() |
self.latent_dim = latent_dim |
self.data_dim = data_dim |
self.patch_size = patch_size |
self.projection_dim = projection_dim |
self.num_heads = num_heads |
self.num_transformer_blocks = num_transformer_blocks |
self.ffn_units = ffn_units |
self.dropout_rate = dropout_rate |
self.num_iterations = num_iterations |
self.classifier_units = classifier_units |
def build(self, input_shape): |
# Create latent array. |
self.latent_array = self.add_weight( |
shape=(self.latent_dim, self.projection_dim), |
initializer=\"random_normal\", |
trainable=True, |
) |
# Create patching module. |
self.patcher = Patches(self.patch_size) |
# Create patch encoder. |
self.patch_encoder = PatchEncoder(self.data_dim, self.projection_dim) |
# Create cross-attenion module. |
self.cross_attention = create_cross_attention_module( |
self.latent_dim, |
self.data_dim, |
self.projection_dim, |
self.ffn_units, |
self.dropout_rate, |
) |
# Create Transformer module. |
self.transformer = create_transformer_module( |
self.latent_dim, |
self.projection_dim, |
self.num_heads, |
self.num_transformer_blocks, |
self.ffn_units, |
self.dropout_rate, |
) |
# Create global average pooling layer. |
self.global_average_pooling = layers.GlobalAveragePooling1D() |
# Create a classification head. |
self.classification_head = create_ffn( |
hidden_units=self.classifier_units, dropout_rate=self.dropout_rate |
) |
super(Perceiver, self).build(input_shape) |
def call(self, inputs): |
# Augment data. |
augmented = data_augmentation(inputs) |
# Create patches. |
patches = self.patcher(augmented) |
# Encode patches. |
encoded_patches = self.patch_encoder(patches) |
# Prepare cross-attention inputs. |
cross_attention_inputs = { |
\"latent_array\": tf.expand_dims(self.latent_array, 0), |
\"data_array\": encoded_patches, |
} |
# Apply the cross-attention and the Transformer modules iteratively. |
for _ in range(self.num_iterations): |
# Apply cross-attention from the latent array to the data array. |
latent_array = self.cross_attention(cross_attention_inputs) |
# Apply self-attention Transformer to the latent array. |
latent_array = self.transformer(latent_array) |
# Set the latent array of the next iteration. |
cross_attention_inputs[\"latent_array\"] = latent_array |
# Apply global average pooling to generate a [batch_size, projection_dim] repesentation tensor. |
representation = self.global_average_pooling(latent_array) |
# Generate logits. |
logits = self.classification_head(representation) |
return logits |
Compile, train, and evaluate the mode |
def run_experiment(model): |
# Create LAMB optimizer with weight decay. |
optimizer = tfa.optimizers.LAMB( |
learning_rate=learning_rate, weight_decay_rate=weight_decay, |
) |
# Compile the model. |
model.compile( |
optimizer=optimizer, |
loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True), |
metrics=[ |
keras.metrics.SparseCategoricalAccuracy(name=\"acc\"), |
keras.metrics.SparseTopKCategoricalAccuracy(5, name=\"top5-acc\"), |
], |
) |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.