This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
step = num_trans_layer_student_init_model // num_trans_layer_student_model student_init_model_selected_transformer_layers = [i for i in range(0, num_trans_layer_student_init_model, step)] | |
student_model_trans_layer_prefix = "encoder.layers." | |
student_model_transformer_layers = [i for i in range(num_trans_layer_student_model)] | |
for student_layer_i, init_layer_i in zip(student_model_transformer_layers, student_init_model_selected_transformer_layers): | |
for transformer_part in transformer_parts: | |
layer_name = student_model_trans_layer_prefix + str(student_layer_i) + transformer_part | |
param = student_init_model_state[student_init_model_trans_layer_prefix + str(init_layer_i) + transformer_part] | |
student_model_state[layer_name].copy_(param) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
optimizer = torch.optim.Adam(self.parameters(), lr=self.lr) | |
def lr_lambda(current_epoch): | |
if current_epoch < self.num_lr_warm_up_epoch: | |
return float(current_epoch+1) / float(max(1, self.num_lr_warm_up_epoch)) | |
else: | |
return max( 0.0, float(self.max_epoch - current_epoch) / float(max(1, self.max_epoch - self.num_lr_warm_up_epoch))) | |
scheduler = torch.optim.lr_scheduler.LambdaLR(optimizer, lr_lambda) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
features_pen = features.float().pow(2).mean() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
torch.nn.functional.kl_div(student_log_prob, teacher_prob, reduction='batchmean') * (self.temperature**2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
student_net_output = self.student_model(*batch) | |
student_log_prob = student_net_output["log_prob"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
with torch.no_grad(): | |
teacher_net_output = self.teacher_model(*batch) | |
teacher_prob = teacher_net_output["prob"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
self.teacher_model.eval() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
decoder_out = decoder.decode(emissions) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
encoder_out = model(**encoder_input) | |
emissions = model.get_normalized_probs(encoder_out, log_probs=True) | |
emissions = emissions.transpose(0, 1).float().cpu().contiguous() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dev_clean_librispeech_data = torchaudio.datasets.LIBRISPEECH(data_path, url='dev-clean', download=False) | |
data_loader = torch.utils.data.DataLoader(dev_clean_librispeech_data, batch_size=1, shuffle=False) |