AhmedRabie01 commited on
Commit
0991cf9
·
verified ·
1 Parent(s): 2ef0ae4

Create multitask_bert.pyimport torch import torch.nn as nn from transformers import AutoConfig, AutoModel class MultiTaskBert(nn.Module): def __init__( self, model_name: str, n_sentiment: int, n_intent: int, n_topic: int, dropout: float = 0.2, init_from_pretrained: bool = False, # IMPORTANT ): super().__init__() self.model_name = model_name if init_from_pretrained: # trust_remote_code lets us load architectures (e.g., ModernBERT/mmBERT) # that may not be bundled with the installed transformers version. self.encoder = AutoModel.from_pretrained( model_name, trust_remote_code=True, ) else: config = AutoConfig.from_pretrained( model_name, trust_remote_code=True, ) self.encoder = AutoModel.from_config( config, trust_remote_code=True, ) hidden_size = self.encoder.config.hidden_size self.dropout = nn.Dropout(dropout) self.sentiment_head = nn.Linear(hidden_size, n_sentiment) self.intent_head = nn.Linear(hidden_size, n_intent) self.topic_head = nn.Linear(hidden_size, n_topic) def forward(self, input_ids, attention_mask): outputs = self.encoder( input_ids=input_ids, attention_mask=attention_mask, ) pooled = outputs.last_hidden_state[:, 0] # CLS pooled = self.dropout(pooled) return ( self.sentiment_head(pooled), self.intent_head(pooled), self.topic_head(pooled), )

Browse files
Sentiment/ml/model/multitask_bert.pyimport torch import torch.nn as nn from transformers import AutoConfig, AutoModel class MultiTaskBert(nn.Module): def __init__( self, model_name: str, n_sentiment: int, n_intent: int, n_topic: int, dropout: float = 0.2, init_from_pretrained: bool = False, # IMPORTANT ): super().__init__() self.model_name = model_name if init_from_pretrained: # trust_remote_code lets us load architectures (e.g., ModernBERT/mmBERT) # that may not be bundled with the installed transformers version. self.encoder = AutoModel.from_pretrained( model_name, trust_remote_code=True, ) else: config = AutoConfig.from_pretrained( model_name, trust_remote_code=True, ) self.encoder = AutoModel.from_config( config, trust_remote_code=True, ) hidden_size = self.encoder.config.hidden_size self.dropout = nn.Dropout(dropout) self.sentiment_head = nn.Linear(hidden_size, n_sentiment) self.intent_head = nn.Linear(hidden_size, n_intent) self.topic_head = nn.Linear(hidden_size, n_topic) def forward(self, input_ids, attention_mask): outputs = self.encoder( input_ids=input_ids, attention_mask=attention_mask, ) pooled = outputs.last_hidden_state[:, 0] # CLS pooled = self.dropout(pooled) return ( self.sentiment_head(pooled), self.intent_head(pooled), self.topic_head(pooled), ) ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torch.nn as nn
3
+ from transformers import AutoConfig, AutoModel
4
+
5
+
6
+ class MultiTaskBert(nn.Module):
7
+ def __init__(
8
+ self,
9
+ model_name: str,
10
+ n_sentiment: int,
11
+ n_intent: int,
12
+ n_topic: int,
13
+ dropout: float = 0.2,
14
+ init_from_pretrained: bool = False, # IMPORTANT
15
+ ):
16
+ super().__init__()
17
+
18
+ self.model_name = model_name
19
+
20
+ if init_from_pretrained:
21
+ # trust_remote_code lets us load architectures (e.g., ModernBERT/mmBERT)
22
+ # that may not be bundled with the installed transformers version.
23
+ self.encoder = AutoModel.from_pretrained(
24
+ model_name,
25
+ trust_remote_code=True,
26
+ )
27
+ else:
28
+ config = AutoConfig.from_pretrained(
29
+ model_name,
30
+ trust_remote_code=True,
31
+ )
32
+ self.encoder = AutoModel.from_config(
33
+ config,
34
+ trust_remote_code=True,
35
+ )
36
+
37
+ hidden_size = self.encoder.config.hidden_size
38
+
39
+ self.dropout = nn.Dropout(dropout)
40
+ self.sentiment_head = nn.Linear(hidden_size, n_sentiment)
41
+ self.intent_head = nn.Linear(hidden_size, n_intent)
42
+ self.topic_head = nn.Linear(hidden_size, n_topic)
43
+
44
+ def forward(self, input_ids, attention_mask):
45
+ outputs = self.encoder(
46
+ input_ids=input_ids,
47
+ attention_mask=attention_mask,
48
+ )
49
+
50
+ pooled = outputs.last_hidden_state[:, 0] # CLS
51
+ pooled = self.dropout(pooled)
52
+
53
+ return (
54
+ self.sentiment_head(pooled),
55
+ self.intent_head(pooled),
56
+ self.topic_head(pooled),
57
+ )