Update modeling_roberta_sentiment.py
Browse files- modeling_roberta_sentiment.py +86 -86
modeling_roberta_sentiment.py
CHANGED
|
@@ -1,86 +1,86 @@
|
|
| 1 |
-
import torch
|
| 2 |
-
import torch.nn as nn
|
| 3 |
-
import
|
| 4 |
-
|
| 5 |
-
class
|
| 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 |
-
self.
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
self.
|
| 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 |
-
cls_token = x[:, 0]
|
| 85 |
-
logits = self.classifier(cls_token)
|
| 86 |
-
return logits
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
from transformers import PreTrainedModel, PretrainedConfig
|
| 4 |
+
|
| 5 |
+
class RobertaSentimentConfig(PretrainedConfig):
|
| 6 |
+
model_type = "roberta-sentiment"
|
| 7 |
+
|
| 8 |
+
def __init__(self,
|
| 9 |
+
vocab_size=30000,
|
| 10 |
+
hidden_size=512,
|
| 11 |
+
num_attention_heads=8,
|
| 12 |
+
num_hidden_layers=6,
|
| 13 |
+
intermediate_size=2048,
|
| 14 |
+
max_position_embeddings=128,
|
| 15 |
+
num_labels=5,
|
| 16 |
+
hidden_dropout_prob=0.1,
|
| 17 |
+
**kwargs):
|
| 18 |
+
super().__init__(**kwargs)
|
| 19 |
+
self.vocab_size = vocab_size
|
| 20 |
+
self.hidden_size = hidden_size
|
| 21 |
+
self.num_attention_heads = num_attention_heads
|
| 22 |
+
self.num_hidden_layers = num_hidden_layers
|
| 23 |
+
self.intermediate_size = intermediate_size
|
| 24 |
+
self.max_position_embeddings = max_position_embeddings
|
| 25 |
+
self.num_labels = num_labels
|
| 26 |
+
self.hidden_dropout_prob = hidden_dropout_prob
|
| 27 |
+
|
| 28 |
+
class TransformerBlock(nn.Module):
|
| 29 |
+
def __init__(self, hidden_dim, num_heads, ffn_dim, dropout):
|
| 30 |
+
super().__init__()
|
| 31 |
+
self.attn_norm = nn.LayerNorm(hidden_dim)
|
| 32 |
+
self.ffn_norm = nn.LayerNorm(hidden_dim)
|
| 33 |
+
self.attn = nn.MultiheadAttention(hidden_dim, num_heads, dropout=dropout, batch_first=True)
|
| 34 |
+
self.ffn = nn.Sequential(
|
| 35 |
+
nn.Linear(hidden_dim, ffn_dim),
|
| 36 |
+
nn.GELU(),
|
| 37 |
+
nn.Linear(ffn_dim, hidden_dim),
|
| 38 |
+
nn.Dropout(dropout)
|
| 39 |
+
)
|
| 40 |
+
self.dropout = nn.Dropout(dropout)
|
| 41 |
+
|
| 42 |
+
def forward(self, x, attention_mask):
|
| 43 |
+
batch_size, seq_len, _ = x.size()
|
| 44 |
+
x_norm = self.attn_norm(x)
|
| 45 |
+
attn_mask = (1 - attention_mask).bool()
|
| 46 |
+
attn_out, _ = self.attn(x_norm, x_norm, x_norm, key_padding_mask=attn_mask)
|
| 47 |
+
x = x + self.dropout(attn_out)
|
| 48 |
+
x_norm = self.ffn_norm(x)
|
| 49 |
+
x = x + self.dropout(self.ffn(x_norm))
|
| 50 |
+
return x
|
| 51 |
+
|
| 52 |
+
class RobertaForSentimentClassification(PreTrainedModel):
|
| 53 |
+
config_class = RobertaSentimentConfig
|
| 54 |
+
|
| 55 |
+
def __init__(self, config):
|
| 56 |
+
super().__init__(config)
|
| 57 |
+
|
| 58 |
+
self.token_emb = nn.Embedding(config.vocab_size, config.hidden_size)
|
| 59 |
+
self.position_emb = nn.Embedding(config.max_position_embeddings, config.hidden_size)
|
| 60 |
+
self.dropout = nn.Dropout(config.hidden_dropout_prob)
|
| 61 |
+
|
| 62 |
+
self.layers = nn.ModuleList([
|
| 63 |
+
TransformerBlock(config.hidden_size, config.num_attention_heads,
|
| 64 |
+
config.intermediate_size, config.hidden_dropout_prob)
|
| 65 |
+
for _ in range(config.num_hidden_layers)
|
| 66 |
+
])
|
| 67 |
+
|
| 68 |
+
self.classifier = nn.Sequential(
|
| 69 |
+
nn.Linear(config.hidden_size, config.hidden_size),
|
| 70 |
+
nn.GELU(),
|
| 71 |
+
nn.Dropout(config.hidden_dropout_prob),
|
| 72 |
+
nn.Linear(config.hidden_size, config.num_labels)
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
self.init_weights()
|
| 76 |
+
|
| 77 |
+
def forward(self, input_ids, attention_mask):
|
| 78 |
+
batch_size, seq_len = input_ids.size()
|
| 79 |
+
positions = torch.arange(0, seq_len, device=input_ids.device).unsqueeze(0).expand(batch_size, seq_len)
|
| 80 |
+
x = self.token_emb(input_ids) + self.position_emb(positions)
|
| 81 |
+
x = self.dropout(x)
|
| 82 |
+
for layer in self.layers:
|
| 83 |
+
x = layer(x, attention_mask)
|
| 84 |
+
cls_token = x[:, 0]
|
| 85 |
+
logits = self.classifier(cls_token)
|
| 86 |
+
return {"logits": logits}
|