Spaces:
Runtime error
Runtime error
Download model.py from Shivatm/transformer-backend: direct link, hf CLI and curl.
- Browser
- Download file 3.87 kB
-
https://huggingface.co/spaces/Shivatm/transformer-backend/resolve/main/model.py
- Command line
-
hf download hf://spaces/Shivatm/transformer-backend/model.py
-
curl -L -o model.py https://huggingface.co/spaces/Shivatm/transformer-backend/resolve/main/model.py
3.87 kB
| import torch | |
| import torch.nn as nn | |
| import torch.nn.functional as F | |
| import math | |
| class PositionalEncoding(nn.Module): | |
| def __init__(self, embed_dim, max_len=512, dropout=0.1): | |
| super().__init__() | |
| self.dropout = nn.Dropout(dropout) | |
| pe = torch.zeros(max_len, embed_dim) | |
| pos = torch.arange(max_len).unsqueeze(1) | |
| div = torch.exp( | |
| torch.arange(0, embed_dim, 2).float() | |
| * (-math.log(10000.0) / embed_dim) | |
| ) | |
| pe[:, 0::2] = torch.sin(pos * div) | |
| pe[:, 1::2] = torch.cos(pos * div) | |
| self.register_buffer('pe', pe.unsqueeze(0)) | |
| def forward(self, x): | |
| x = x + self.pe[:, :x.size(1)] | |
| return self.dropout(x) | |
| class MultiHeadSelfAttention(nn.Module): | |
| def __init__(self, embed_dim, num_heads, dropout=0.1): | |
| super().__init__() | |
| self.num_heads = num_heads | |
| self.head_dim = embed_dim // num_heads | |
| self.scale = self.head_dim ** -0.5 | |
| self.W_q = nn.Linear(embed_dim, embed_dim) | |
| self.W_k = nn.Linear(embed_dim, embed_dim) | |
| self.W_v = nn.Linear(embed_dim, embed_dim) | |
| self.W_o = nn.Linear(embed_dim, embed_dim) | |
| self.dropout = nn.Dropout(dropout) | |
| def forward(self, x, mask=None): | |
| B, T, C = x.shape | |
| H, D = self.num_heads, self.head_dim | |
| def split_heads(t): | |
| return t.view(B, T, H, D).transpose(1, 2) | |
| Q = split_heads(self.W_q(x)) | |
| K = split_heads(self.W_k(x)) | |
| V = split_heads(self.W_v(x)) | |
| scores = torch.matmul(Q, K.transpose(-2, -1)) * self.scale | |
| if mask is not None: | |
| scores = scores.masked_fill(mask.unsqueeze(1).unsqueeze(2), float('-inf')) | |
| attn = self.dropout(F.softmax(scores, dim=-1)) | |
| out = torch.matmul(attn, V) | |
| out = out.transpose(1, 2).contiguous().view(B, T, C) | |
| return self.W_o(out) | |
| class FeedForward(nn.Module): | |
| def __init__(self, embed_dim, ff_dim, dropout=0.1): | |
| super().__init__() | |
| self.net = nn.Sequential( | |
| nn.Linear(embed_dim, ff_dim), | |
| nn.GELU(), | |
| nn.Dropout(dropout), | |
| nn.Linear(ff_dim, embed_dim), | |
| nn.Dropout(dropout), | |
| ) | |
| def forward(self, x): | |
| return self.net(x) | |
| class TransformerEncoderBlock(nn.Module): | |
| def __init__(self, embed_dim, num_heads, ff_dim, dropout=0.1): | |
| super().__init__() | |
| self.norm1 = nn.LayerNorm(embed_dim) | |
| self.norm2 = nn.LayerNorm(embed_dim) | |
| self.attn = MultiHeadSelfAttention(embed_dim, num_heads, dropout) | |
| self.ff = FeedForward(embed_dim, ff_dim, dropout) | |
| def forward(self, x, mask=None): | |
| x = x + self.attn(self.norm1(x), mask) | |
| x = x + self.ff(self.norm2(x)) | |
| return x | |
| class TransformerClassifier(nn.Module): | |
| def __init__(self, vocab_size=20000, embed_dim=128, num_heads=8, | |
| num_layers=4, ff_dim=512, max_len=256, | |
| num_classes=2, dropout=0.1): | |
| super().__init__() | |
| self.embedding = nn.Embedding(vocab_size, embed_dim, padding_idx=0) | |
| self.pos_enc = PositionalEncoding(embed_dim, max_len, dropout) | |
| self.encoder = nn.ModuleList([ | |
| TransformerEncoderBlock(embed_dim, num_heads, ff_dim, dropout) | |
| for _ in range(num_layers) | |
| ]) | |
| self.norm = nn.LayerNorm(embed_dim) | |
| self.classifier = nn.Sequential( | |
| nn.Linear(embed_dim, embed_dim // 2), | |
| nn.GELU(), | |
| nn.Dropout(dropout), | |
| nn.Linear(embed_dim // 2, num_classes), | |
| ) | |
| def forward(self, input_ids): | |
| pad_mask = (input_ids == 0) | |
| x = self.embedding(input_ids) | |
| x = self.pos_enc(x) | |
| for block in self.encoder: | |
| x = block(x, pad_mask) | |
| x = self.norm(x) | |
| return self.classifier(x[:, 0, :]) | |