-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel_utils.py
43 lines (34 loc) · 1.44 KB
/
model_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from torchmetrics.text import ROUGEScore
def unfreeze_layers(model, part, list_layers):
"""
Unfreeze certain layers of the model for fine-tuning
"""
model.model.shared.weight.requires_grad = True
model.model.encoder.embed_positions.weight.requires_grad = True
if part == "encoder":
for layer in list_layers:
for param in model.model.encoder.layers[layer].parameters():
param.requires_grad = True
for param in model.model.encoder.layernorm_embedding.parameters():
param.requires_grad = True
for param in model.model.encoder.layer_norm.parameters():
param.requires_grad = True
elif part == "decoder":
for layer in list_layers:
for param in model.model.decoder.layers[layer].parameters():
param.requires_grad = True
for param in model.model.decoder.layernorm_embedding.parameters():
param.requires_grad = True
for param in model.model.decoder.layer_norm.parameters():
param.requires_grad = True
else:
raise ValueError("Invalid part. Choose 'encoder' or 'decoder'")
def compute_detailed_scores_pytorch(target_text, output_text):
"""
Compute the evaluation metrics
"""
# Calculate ROUGE score
rouge = ROUGEScore()
rouge_score = rouge(output_text, target_text)
# Return the scores
return {'rouge': rouge_score}