text stringlengths 0 4.99k |
|---|
test_masked_embeddings, |
test_unmasked_positions, |
test_mask_indices, |
test_unmask_indices, |
) = self.model.patch_encoder(test_patches) |
test_encoder_outputs = self.model.encoder(test_unmasked_embeddings) |
test_encoder_outputs = test_encoder_outputs + test_unmasked_positions |
test_decoder_inputs = tf.concat( |
[test_encoder_outputs, test_masked_embeddings], axis=1 |
) |
test_decoder_outputs = self.model.decoder(test_decoder_inputs) |
# Show a maksed patch image. |
test_masked_patch, idx = self.model.patch_encoder.generate_masked_image( |
test_patches, test_unmask_indices |
) |
print(f\"\nIdx chosen: {idx}\") |
original_image = test_augmented_images[idx] |
masked_image = self.model.patch_layer.reconstruct_from_patch( |
test_masked_patch |
) |
reconstructed_image = test_decoder_outputs[idx] |
fig, ax = plt.subplots(nrows=1, ncols=3, figsize=(15, 5)) |
ax[0].imshow(original_image) |
ax[0].set_title(f\"Original: {epoch:03d}\") |
ax[1].imshow(masked_image) |
ax[1].set_title(f\"Masked: {epoch:03d}\") |
ax[2].imshow(reconstructed_image) |
ax[2].set_title(f\"Resonstructed: {epoch:03d}\") |
plt.show() |
plt.close() |
Learning rate scheduler |
# Some code is taken from: |
# https://www.kaggle.com/ashusma/training-rfcx-tensorflow-tpu-effnet-b2. |
class WarmUpCosine(keras.optimizers.schedules.LearningRateSchedule): |
def __init__( |
self, learning_rate_base, total_steps, warmup_learning_rate, warmup_steps |
): |
super(WarmUpCosine, self).__init__() |
self.learning_rate_base = learning_rate_base |
self.total_steps = total_steps |
self.warmup_learning_rate = warmup_learning_rate |
self.warmup_steps = warmup_steps |
self.pi = tf.constant(np.pi) |
def __call__(self, step): |
if self.total_steps < self.warmup_steps: |
raise ValueError(\"Total_steps must be larger or equal to warmup_steps.\") |
cos_annealed_lr = tf.cos( |
self.pi |
* (tf.cast(step, tf.float32) - self.warmup_steps) |
/ float(self.total_steps - self.warmup_steps) |
) |
learning_rate = 0.5 * self.learning_rate_base * (1 + cos_annealed_lr) |
if self.warmup_steps > 0: |
if self.learning_rate_base < self.warmup_learning_rate: |
raise ValueError( |
\"Learning_rate_base must be larger or equal to \" |
\"warmup_learning_rate.\" |
) |
slope = ( |
self.learning_rate_base - self.warmup_learning_rate |
) / self.warmup_steps |
warmup_rate = slope * tf.cast(step, tf.float32) + self.warmup_learning_rate |
learning_rate = tf.where( |
step < self.warmup_steps, warmup_rate, learning_rate |
) |
return tf.where( |
step > self.total_steps, 0.0, learning_rate, name=\"learning_rate\" |
) |
total_steps = int((len(x_train) / BATCH_SIZE) * EPOCHS) |
warmup_epoch_percentage = 0.15 |
warmup_steps = int(total_steps * warmup_epoch_percentage) |
scheduled_lrs = WarmUpCosine( |
learning_rate_base=LEARNING_RATE, |
total_steps=total_steps, |
warmup_learning_rate=0.0, |
warmup_steps=warmup_steps, |
) |
lrs = [scheduled_lrs(step) for step in range(total_steps)] |
plt.plot(lrs) |
plt.xlabel(\"Step\", fontsize=14) |
plt.ylabel(\"LR\", fontsize=14) |
plt.show() |
# Assemble the callbacks. |
train_callbacks = [TrainMonitor(epoch_interval=5)] |
png |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.