Download ml-engine/models/phishssl_contrastive.py from Amal340/phishing-framework-code: direct link, hf CLI and curl.
- Browser
- Download file 1.42 kB
-
https://huggingface.co/datasets/Amal340/phishing-framework-code/resolve/main/ml-engine/models/phishssl_contrastive.py
- Command line
-
hf download hf://datasets/Amal340/phishing-framework-code/ml-engine/models/phishssl_contrastive.py
-
curl -L -o phishssl_contrastive.py https://huggingface.co/datasets/Amal340/phishing-framework-code/resolve/main/ml-engine/models/phishssl_contrastive.py
1.42 kB
| """ | |
| PhishSSL: Self-Supervised Contrastive Learning for Zero-Day Phishing Detection | |
| (Li et al., 2025) | |
| Trains the multimodal encoder on unlabeled webpage views using Normalized Temperature-scaled | |
| Cross-Entropy Loss (NT-Xent) to detect zero-day morphological mutations. | |
| """ | |
| import numpy as np | |
| class PhishSSLContrastiveTrainer: | |
| def __init__(self, proj_input_dim: int = 112, proj_dim: int = 32, temperature: float = 0.15, seed: int = 303): | |
| self.temperature = temperature | |
| rng = np.random.default_rng(seed) | |
| self.W_proj = rng.normal(0, np.sqrt(2.0 / proj_input_dim), size=(proj_input_dim, proj_dim)) | |
| def project(self, z_Concat: np.ndarray) -> np.ndarray: | |
| h = z_Concat @ self.W_proj | |
| norm = np.linalg.norm(h, axis=-1, keepdims=True) + 1e-8 | |
| return h / norm | |
| def compute_nt_xent_loss(self, z_view1: np.ndarray, z_view2: np.ndarray) -> float: | |
| """ | |
| Computes InfoNCE / NT-Xent contrastive loss between two augmented views of webpage representations. | |
| """ | |
| p1 = self.project(z_view1) | |
| p2 = self.project(z_view2) | |
| sim_matrix = (p1 @ p2.T) / self.temperature | |
| sim_max = np.max(sim_matrix, axis=1, keepdims=True) | |
| exp_sim = np.exp(sim_matrix - sim_max) | |
| pos_sim = np.diag(exp_sim) | |
| denom = np.sum(exp_sim, axis=1) + 1e-8 | |
| loss = -np.mean(np.log(pos_sim / denom + 1e-8)) | |
| return float(loss) | |