File size: 8,798 Bytes
32c143f 7d7c95a 32c143f | 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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 | import torch
import torch.nn as nn
from transformers import EsmModel, EsmConfig
from typing import Optional
from peft import get_peft_model, LoraConfig, TaskType
class TaxonomyAwareESM(nn.Module):
def __init__(self, num_classes, pretrained_model_name="facebook/esm2_t33_650M_UR50D", use_lora=True, lora_rank=8, vocab_sizes=None):
super().__init__()
# 1. ESM Backbone
print(f"Loading ESM model: {pretrained_model_name}")
self.esm = EsmModel.from_pretrained(pretrained_model_name)
if use_lora:
print(f"Injecting LoRA adapters (Rank={lora_rank})...")
# Define configuration
peft_config = LoraConfig(
task_type=TaskType.FEATURE_EXTRACTION,
inference_mode=False,
r=lora_rank, # Intrinsic Rank
lora_alpha=32, # Scaling Factor
lora_dropout=0.1,
target_modules=["query", "value"]
)
# Wrap the model
self.esm = get_peft_model(self.esm, peft_config)
self.esm.print_trainable_parameters()
else:
# Traditional Freezing
for param in self.esm.parameters():
param.requires_grad = False
self.hidden_dim = self.esm.config.hidden_size # e.g. 1280 for 650M
# 2. Taxonomy Encoder
# Ranks: Phylum, Class, Order, Family, Genus, Species, Subspecies (7 levels)
if vocab_sizes is None:
# Fallback to defaults if not provided (though train.py should pass them)
# Using sufficiently large defaults relative to known biology data
print("Warning: vocab_sizes not provided, using hardcoded defaults.")
self.vocab_sizes = [1000, 5000, 10000, 20000, 50000, 100000, 50000]
else:
self.vocab_sizes = vocab_sizes
print(f"Model initialized with vocab sizes: {self.vocab_sizes}")
self.tax_embedding_dim = 128 # Dimension for tax embeddings
self.tax_embeddings = nn.ModuleList([
nn.Embedding(self.vocab_sizes[i], self.tax_embedding_dim, padding_idx=0)
for i in range(len(self.vocab_sizes))
])
# Project combined tax embeddings (7 * 128) to match ESM dim?
# Or project ESM to match Tax?
# Goal: Cross Attention using Sequence as Query, Tax as Key/Value.
# Sequence (B, L, D_esm)
# Tax (B, 7, D_tax)
self.tax_projector = nn.Linear(self.tax_embedding_dim, self.hidden_dim)
# 3. Cross Attention Fusion
# Query: Protein Sequence (B, L, H)
# Key/Value: Taxonomy Info (B, 7, H)
# Output: Enhanced Sequence (B, L, H)
self.cross_attention = nn.MultiheadAttention(embed_dim=self.hidden_dim, num_heads=8, batch_first=True)
self.layer_norm = nn.LayerNorm(self.hidden_dim)
self.dropout = nn.Dropout(0.1)
# 4. Classifier Head
# Pooling? "Mean Pooling" or "CLS". ESM has CLS.
# Let's use Mean Pooling of the Fused Sequence representation.
self.classifier = nn.Linear(self.hidden_dim, num_classes)
def forward(self, input_ids, attention_mask, tax_vector):
"""
input_ids: (B, L)
attention_mask: (B, L)
tax_vector: (B, 7)
"""
# 1. Forward ESM
outputs = self.esm(input_ids=input_ids, attention_mask=attention_mask)
sequence_output = outputs.last_hidden_state # (B, L, H)
# 2. Forward Taxonomy
# tax_vector is (B, 7) integers.
# We need to embed each column with its specific embedding layer.
tax_embeds = []
for i in range(7):
idx = tax_vector[:, i] # (B,)
# Clamp to vocab size to avoid index out of bounds if new species appear?
# Or assume 0 (UNK) handling upstream.
# Ideally safety check:
idx = idx.clamp(0, self.vocab_sizes[i] - 1)
emb = self.tax_embeddings[i](idx) # (B, D_tax)
tax_embeds.append(emb)
# Stack to get (B, 7, D_tax)
tax_sequence = torch.stack(tax_embeds, dim=1) # (B, 7, D_tax)
# Project to match ESM dim
tax_sequence = self.tax_projector(tax_sequence) # (B, 7, H)
# 3. Cross Attention
# Query: Sequence (B, L, H)
# Key: Tax (B, 7, H)
# Value: Tax (B, 7, H)
# We want to inject Tax info into Sequence.
attn_output, _ = self.cross_attention(
query=sequence_output,
key=tax_sequence,
value=tax_sequence
)
# Residual Connection + Norm
# fused_sequence = LayerNorm(Sequence + Dropout(Attn))
fused_sequence = self.layer_norm(sequence_output + self.dropout(attn_output))
# 4. Pooling
# Masked Mean Pooling
mask_expanded = attention_mask.unsqueeze(-1).expand(fused_sequence.size()).float()
sum_embeddings = torch.sum(fused_sequence * mask_expanded, dim=1)
sum_mask = torch.clamp(mask_expanded.sum(dim=1), min=1e-9)
pooled_output = sum_embeddings / sum_mask # (B, H)
# 5. Classifier
logits = self.classifier(pooled_output) # (B, NumClasses)
return logits
class AsymmetricLoss(nn.Module):
def __init__(self, gamma_neg=4, gamma_pos=0, clip=0.05, eps=1e-8, disable_torch_grad_focal_loss=True, reduction='mean', ic_weights: Optional[torch.Tensor] = None, normalize_ic: bool = True):
super(AsymmetricLoss, self).__init__()
self.gamma_neg = gamma_neg
self.gamma_pos = gamma_pos
self.clip = clip
self.eps = eps
self.disable_torch_grad_focal_loss = disable_torch_grad_focal_loss
self.reduction = reduction
self.normalize_ic = normalize_ic
# IC ๊ฐ์ค์น ์ฒ๋ฆฌ
self.use_ic_weights = ic_weights is not None
if self.use_ic_weights:
if normalize_ic:
# 0์ด ์๋ ๊ฐ์ค์น๋ง ์ฌ์ฉํ์ฌ ์ ๊ทํ (mean=1)
nonzero_mask = ic_weights > 0
if nonzero_mask.sum() > 0:
nonzero_weights = ic_weights[nonzero_mask]
ic_weights = ic_weights / (nonzero_weights.mean() + 1e-8)
# ์ต์ ๊ฐ์ค์น ์ค์ ๋ฐ ๋ฒํผ ๋ฑ๋ก
ic_weights = torch.clamp(ic_weights, min=0.1)
self.register_buffer('ic_weights', ic_weights)
print("โ
AsymmetricLoss with IC weights initialized.")
print(f" IC weights range: [{self.ic_weights.min():.4f}, {self.ic_weights.max():.4f}]")
def forward(self, x, y):
"""
Parameters
----------
x: input logits
y: targets (multi-label binarized vector)
"""
# Calculating Probabilities
x_sigmoid = torch.sigmoid(x)
xs_pos = x_sigmoid
xs_neg = 1 - x_sigmoid
# Asymmetric Clipping
if self.clip is not None and self.clip > 0:
xs_neg = (xs_neg + self.clip).clamp(max=1)
# Basic CE calculation
los_pos = y * torch.log(xs_pos.clamp(min=self.eps))
los_neg = (1 - y) * torch.log(xs_neg.clamp(min=self.eps))
# Asymmetric Focusing
if self.gamma_neg > 0 or self.gamma_pos > 0:
if self.disable_torch_grad_focal_loss:
torch.set_grad_enabled(False)
pt0 = xs_pos * y
pt1 = xs_neg * (1 - y) # pt = p if t > 0 else 1-p
pt = pt0 + pt1
one_sided_gamma = self.gamma_pos * y + self.gamma_neg * (1 - y)
one_sided_w = torch.pow(1 - pt, one_sided_gamma)
if self.disable_torch_grad_focal_loss:
torch.set_grad_enabled(True)
los_pos = one_sided_w * los_pos
los_neg = one_sided_w * los_neg
# IC ๊ฐ์ค์น ์ ์ฉ (Positive์ Negative Loss ๋ชจ๋์)
# ํ๊ฐ ์ FP์๋ IC ๊ฐ์ค์น๊ฐ ์ ์ฉ๋๋ฏ๋ก, ํ์ต ์์๋ ์ผ๊ด์ฑ์ ์ํด negative loss์๋ ์ ์ฉ
if self.use_ic_weights:
los_pos = los_pos * self.ic_weights.unsqueeze(0)
los_neg = los_neg * self.ic_weights.unsqueeze(0)
loss = - (los_pos + los_neg)
if self.reduction == 'mean':
# loss = loss.mean()
loss = loss.sum() / x.size(0)
elif self.reduction == 'sum':
loss = loss.sum()
return loss
|