Fit the prototypical-network single-sample discriminator
Source:R/singlesample-protonet-scorer.R
fit_proto_net.RdTrains a BatchNorm-free MLP embedding on the per-sample-rCLR, universe-aligned TRAINING matrix by EPISODIC PROTOTYPICAL training (Snell 2017; via reticulate-python torch, Adam, prototypical loss – NOT cross-entropy), EXPORTS the weights up to and including the embedding layer to R as plain numeric matrices/vectors, DISCARDS the python module, then computes the frozen per-class PROTOTYPES (class-mean embeddings) using the PURE-R forward over ALL training rows (guaranteeing fit/score consistency despite float32 training). The fitted model holds no external pointer.
Usage
fit_proto_net(X_train, y_train, meta_train = NULL, hp = list())Arguments
- X_train
Numeric matrix (samples \(\times\) features) of non-negative abundances with unique, non-empty feature names (colnames).
- y_train
Numeric / integer 0/1 labels (1 = case), length
nrow(X_train), with at least one case and one control.- meta_train
Optional per-sample metadata. Accepted for interface uniformity and otherwise ignored.
- hp
Optional list of hyperparameters. Allowed fields:
hidden(positive-integer vector of layer widths up to the embedding; the LAST entry is the embedding dim \(d\); defaultc(64L, 32L)),activation("relu"(default) or"tanh"),epochs(number of training episodes, positive integer; default200L),lr(positive Adam learning rate; default1e-3),weight_decay(non-negative Adam L2; default1e-4),n_support(per-class support size per episode, positive integer; default5L),n_query(per-class query size per episode, positive integer; default5L),min_features(feature-overlap floor at scoring, positive integer; default3L),device("cpu"(default),"cuda", or"auto";"cuda"/"auto"fall back to CPU with no GPU), andseed(integer; default42L).n_support/n_queryare clamped per class so each episode is feasible (disjoint support/query, \(\geq 1\) of each per class).
Value
Object of class proto_net_model: a list with
feature_universe, weights (exported per-layer list(W, b)
up to the embedding), activation, proto_case, proto_ctrl
(length-\(d\) frozen prototypes), embedding_dim, device
(resolved), seed, and hp.
Details
The score is the squared-Euclidean log-likelihood ratio to the prototypes, \(s(z) = -\lVert z - \mu_{\mathrm{case}}\rVert^2 + \lVert z - \mu_{\mathrm{ctrl}}\rVert^2\), exactly the deepmaha score with \(\Sigma = I\). The distinction is the embedding: it is trained by the prototypical loss (CE over the softmax of negative squared-Euclidean distances to episodic class-mean prototypes), not a cross-entropy classification head.
References
Snell J, Swersky K, Zemel R. (2017) Prototypical Networks for Few-shot Learning. NeurIPS 30. arXiv:1703.05175.
Examples
if (FALSE) { # \dontrun{
set.seed(1)
n <- 80; p <- 30; k <- 8
L <- matrix(stats::rnorm(n * p, 4, 0.5), nrow = n,
dimnames = list(NULL, paste0("miR-", seq_len(p))))
y <- rep(c(0, 1), each = n / 2)
L[y == 1, seq_len(k)] <- L[y == 1, seq_len(k)] + 1.2
X <- exp(L)
model <- fit_proto_net(X, y)
score_proto_net(model, X[1, , drop = FALSE])
} # }