Create pisco_clf.py
Browse files- pisco_clf.py +40 -0
pisco_clf.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
import numpy as np
|
| 4 |
+
from huggingface_hub import PyTorchModelHubMixin
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
class PISCOClassifier(nn.Module, PyTorchModelHubMixin):
|
| 8 |
+
def __init__(self, d: int, hidden: int = 512, threshold: float = 0.5):
|
| 9 |
+
super().__init__()
|
| 10 |
+
self.net = nn.Sequential(
|
| 11 |
+
nn.Linear(d, hidden),
|
| 12 |
+
nn.LayerNorm(hidden),
|
| 13 |
+
nn.GELU(),
|
| 14 |
+
nn.Dropout(0.3),
|
| 15 |
+
nn.Linear(hidden, hidden // 4),
|
| 16 |
+
nn.GELU(),
|
| 17 |
+
nn.Dropout(0.2),
|
| 18 |
+
nn.Linear(hidden // 4, 1),
|
| 19 |
+
)
|
| 20 |
+
self.threshold = threshold
|
| 21 |
+
|
| 22 |
+
def forward(self, x):
|
| 23 |
+
return self.net(x).squeeze(-1)
|
| 24 |
+
|
| 25 |
+
@torch.inference_mode()
|
| 26 |
+
def predict_proba(self, X) -> np.ndarray:
|
| 27 |
+
self.eval()
|
| 28 |
+
x = self._as_tensor(X)
|
| 29 |
+
return torch.sigmoid(self.net(x)).cpu().numpy()
|
| 30 |
+
|
| 31 |
+
def predict(self, X, threshold: float | None = None) -> np.ndarray:
|
| 32 |
+
"""Binary predictions. Uses stored threshold if not given."""
|
| 33 |
+
t = threshold if threshold is not None else self.threshold
|
| 34 |
+
return (self.predict_proba(X) >= t).astype(int)
|
| 35 |
+
|
| 36 |
+
@staticmethod
|
| 37 |
+
def _as_tensor(X) -> torch.Tensor:
|
| 38 |
+
if isinstance(X, torch.Tensor):
|
| 39 |
+
return X.float()
|
| 40 |
+
return torch.tensor(np.asarray(X), dtype=torch.float32)
|