add combined ckpt
Browse files- README.md +10 -0
- config.json +1 -0
- model.safetensors +3 -0
- probe_clf.py +111 -0
- scaler.pkl +3 -0
README.md
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
tags:
|
| 3 |
+
- model_hub_mixin
|
| 4 |
+
- pytorch_model_hub_mixin
|
| 5 |
+
---
|
| 6 |
+
|
| 7 |
+
This model has been pushed to the Hub using the [PytorchModelHubMixin](https://huggingface.co/docs/huggingface_hub/package_reference/mixins#huggingface_hub.PyTorchModelHubMixin) integration:
|
| 8 |
+
- Code: [More Information Needed]
|
| 9 |
+
- Paper: [More Information Needed]
|
| 10 |
+
- Docs: [More Information Needed]
|
config.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
{"hidden_dim": null, "normalize": true}
|
model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:78fc8ada6a9dd12c0d3937fce13c72ae2b931a1b4ec2799727cd6d9867bcb692
|
| 3 |
+
size 65676
|
probe_clf.py
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import os
|
| 3 |
+
import pickle
|
| 4 |
+
import numpy as np
|
| 5 |
+
import torch
|
| 6 |
+
import torch.nn as nn
|
| 7 |
+
|
| 8 |
+
from huggingface_hub import PyTorchModelHubMixin
|
| 9 |
+
from sklearn.preprocessing import StandardScaler
|
| 10 |
+
from safetensors.torch import save_file, load_file
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
class LinearProbeTorch(PyTorchModelHubMixin):
|
| 14 |
+
def __init__(
|
| 15 |
+
self,
|
| 16 |
+
normalize=True,
|
| 17 |
+
device="cpu",
|
| 18 |
+
random_state=42,
|
| 19 |
+
hidden_dim=1024,
|
| 20 |
+
):
|
| 21 |
+
self.normalize = normalize
|
| 22 |
+
self.device = device if torch.cuda.is_available() else "cpu"
|
| 23 |
+
self.random_state = random_state
|
| 24 |
+
self.hidden_dim = hidden_dim
|
| 25 |
+
self.model = None
|
| 26 |
+
self.scaler = None
|
| 27 |
+
|
| 28 |
+
def _create_model(self, input_dim, positive_class_proportion=0.5):
|
| 29 |
+
model = nn.Linear(input_dim, 1)
|
| 30 |
+
nn.init.zeros_(model.weight)
|
| 31 |
+
p = np.clip(positive_class_proportion, 0.01, 0.99)
|
| 32 |
+
nn.init.constant_(model.bias, np.log(p / (1 - p)))
|
| 33 |
+
return model
|
| 34 |
+
|
| 35 |
+
def predict_proba(self, X):
|
| 36 |
+
if self.model is None:
|
| 37 |
+
raise ValueError("Model not fitted yet. Call fit() first.")
|
| 38 |
+
X = np.asarray(X.cpu().numpy() if isinstance(X, torch.Tensor) else X)
|
| 39 |
+
if self.normalize and self.scaler is not None:
|
| 40 |
+
X = self.scaler.transform(X)
|
| 41 |
+
self.model.eval()
|
| 42 |
+
with torch.no_grad():
|
| 43 |
+
|
| 44 |
+
logits = self.model(torch.FloatTensor(X).to(self.device)).squeeze(-1)
|
| 45 |
+
proba_pos = torch.sigmoid(logits).cpu().numpy()
|
| 46 |
+
return np.column_stack([1 - proba_pos, proba_pos])
|
| 47 |
+
|
| 48 |
+
def predict(self, X):
|
| 49 |
+
return (self.predict_proba(X)[:, 1] >= 0.5).astype(int)
|
| 50 |
+
|
| 51 |
+
def _save_pretrained(self, save_directory):
|
| 52 |
+
os.makedirs(save_directory, exist_ok=True)
|
| 53 |
+
|
| 54 |
+
save_file(
|
| 55 |
+
self.model.state_dict(), os.path.join(save_directory, "model.safetensors")
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
config = {
|
| 59 |
+
"hidden_dim": self.hidden_dim,
|
| 60 |
+
"normalize": self.normalize,
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
with open(os.path.join(save_directory, "config.json"), "w") as f:
|
| 64 |
+
json.dump(config, f)
|
| 65 |
+
|
| 66 |
+
if self.scaler is not None:
|
| 67 |
+
with open(os.path.join(save_directory, "scaler.pkl"), "wb") as f:
|
| 68 |
+
pickle.dump(self.scaler, f)
|
| 69 |
+
|
| 70 |
+
@classmethod
|
| 71 |
+
def _from_pretrained(
|
| 72 |
+
cls,
|
| 73 |
+
model_id,
|
| 74 |
+
*args,
|
| 75 |
+
config=None,
|
| 76 |
+
cache_dir=None,
|
| 77 |
+
force_download=False,
|
| 78 |
+
**kwargs,
|
| 79 |
+
):
|
| 80 |
+
|
| 81 |
+
from huggingface_hub import hf_hub_download
|
| 82 |
+
import pickle
|
| 83 |
+
|
| 84 |
+
weights_path = hf_hub_download(model_id, "model.safetensors")
|
| 85 |
+
config_path = hf_hub_download(model_id, "config.json")
|
| 86 |
+
|
| 87 |
+
with open(config_path) as f:
|
| 88 |
+
cfg = json.load(f)
|
| 89 |
+
|
| 90 |
+
model = cls(**cfg)
|
| 91 |
+
|
| 92 |
+
state_dict = load_file(weights_path)
|
| 93 |
+
input_dim = state_dict["weight"].shape[1]
|
| 94 |
+
model.model = model._create_model(input_dim)
|
| 95 |
+
|
| 96 |
+
model.model.load_state_dict(state_dict)
|
| 97 |
+
|
| 98 |
+
try:
|
| 99 |
+
scaler_path = hf_hub_download(model_id, "scaler.pkl")
|
| 100 |
+
with open(scaler_path, "rb") as f:
|
| 101 |
+
model.scaler = pickle.load(f)
|
| 102 |
+
except:
|
| 103 |
+
pass
|
| 104 |
+
|
| 105 |
+
model.model.eval()
|
| 106 |
+
|
| 107 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 108 |
+
model.device = device
|
| 109 |
+
model.model = model.model.to(device)
|
| 110 |
+
|
| 111 |
+
return model
|
scaler.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:6ac70e5600225546d0378c25f84a8d0bdaaa528d856ecc595ecdca0bef132d72
|
| 3 |
+
size 393706
|