Text Classification
Transformers
JAX
pcmlp
feature-extraction
predictive-coding
local-loss
flax
tiny-model
custom-architecture
custom_code
Eval Results (legacy)
Instructions to use zeechimp/pc-mlp-tiny with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use zeechimp/pc-mlp-tiny with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-classification", model="zeechimp/pc-mlp-tiny", trust_remote_code=True)# pip install -U transformers accelerate # Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("zeechimp/pc-mlp-tiny", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
Create modeling_pcmlp.py
Browse files- modeling_pcmlp.py +46 -0
modeling_pcmlp.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""PC-MLP model for HuggingFace Hub with auto_map."""
|
| 2 |
+
import math
|
| 3 |
+
import torch
|
| 4 |
+
import torch.nn as nn
|
| 5 |
+
from transformers import PreTrainedModel
|
| 6 |
+
from .configuration_pcmlp import PCMLPConfig
|
| 7 |
+
|
| 8 |
+
class PCMLP(PreTrainedModel):
|
| 9 |
+
config_class = PCMLPConfig
|
| 10 |
+
|
| 11 |
+
def __init__(self, config):
|
| 12 |
+
super().__init__(config)
|
| 13 |
+
d = config.vocab_size
|
| 14 |
+
h = config.hidden_dim
|
| 15 |
+
scale = 1.0 / math.sqrt(h)
|
| 16 |
+
|
| 17 |
+
self.tok = nn.Embedding(config.vocab_size, d)
|
| 18 |
+
self.pos = nn.Embedding(config.seq_len, d)
|
| 19 |
+
self.W1 = nn.Linear(d, h, bias=False)
|
| 20 |
+
self.W2 = nn.Linear(h, h, bias=False)
|
| 21 |
+
self.head = nn.Linear(h, config.num_classes)
|
| 22 |
+
|
| 23 |
+
# match JAX init scale
|
| 24 |
+
nn.init.normal_(self.tok.weight, std=0.05)
|
| 25 |
+
nn.init.normal_(self.pos.weight, std=0.05)
|
| 26 |
+
nn.init.normal_(self.W1.weight, std=scale)
|
| 27 |
+
nn.init.normal_(self.W2.weight, std=scale)
|
| 28 |
+
nn.init.normal_(self.head.weight, std=0.02)
|
| 29 |
+
|
| 30 |
+
def forward(self, input_ids, labels=None):
|
| 31 |
+
B, T = input_ids.shape
|
| 32 |
+
pos_ids = torch.arange(T, device=input_ids.device)
|
| 33 |
+
h = self.tok(input_ids) + self.pos(pos_ids)
|
| 34 |
+
h = h.reshape(B, -1)[:, :self.W1.in_features]
|
| 35 |
+
h0 = h
|
| 36 |
+
h1 = torch.nn.functional.gelu(self.W1(h0))
|
| 37 |
+
h2 = torch.nn.functional.gelu(self.W2(h1))
|
| 38 |
+
logits = self.head(h2)
|
| 39 |
+
|
| 40 |
+
loss = None
|
| 41 |
+
if labels is not None:
|
| 42 |
+
ce = torch.nn.functional.cross_entropy(logits, labels)
|
| 43 |
+
local = ((h1.mean(1) - h0.mean(1)) ** 2).mean() + \
|
| 44 |
+
((h2.mean(1) - h1.mean(1)) ** 2).mean()
|
| 45 |
+
loss = ce + self.config.local_loss_weight * local
|
| 46 |
+
return {"loss": loss, "logits": logits}
|