File size: 1,419 Bytes
cf9e03f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | """
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)
|