Rare Events Fade in Both Directions: Measuring and Correcting Class-Dependent Contraction in Contrastive Neural ODE Encoders

When a model is trained with contrastive learning on imbalanced data, the common classes produce most of the gradient. Contrastive learning here means pulling similar examples together in embedding space and pushing dissimilar ones apart. Because the common classes dominate, the rare classes get crowded into a small corner of the embedding space. This "gradient norm disparity" is well documented for image encoders. The standard fixes either reweight the loss or anchor each class to a learned or fixed reference point.

Neural ordinary differential equations (Neural ODEs) are encoders that evolve a hidden state continuously in time under a learned vector field. They are a natural choice for irregularly sampled clinical and sensor data. In these encoders, gradient disparity has a second component that no image-side fix touches. A rare event observed at time $t$, such as an acute physiological spike, reaches the final embedding only through the flow of the learned vector field. The gradient that teaches the field about that event travels back along the same path. If the field shrinks perturbations along rare-class trajectories, the rare event fades in the forward pass and its gradient fades in the backward pass. This proposal measures that class-dependent shrinkage, tests whether it explains the minority-class failures of contrastive ODE encoders, and corrects it with a cheap regularizer that keeps minority trajectories from contracting.

Core Claim

Neural Ordinary Differential Equations trains ODE models with the adjoint method, which integrates a "sensitivity" vector backward in time. Write $h(t)$ for the hidden state, $f_\theta$ for the learned vector field, $\mathcal{L}$ for the contrastive loss, and $\Phi(t_1,t)=\partial h(t_1)/\partial h(t)$ for the flow Jacobian. The flow Jacobian says how much a small change at time $t$ moves the final state at time $t_1$. The parameter gradient is then

$$\frac{d\mathcal{L}}{d\theta}=\int_{t_0}^{t_1}\frac{\partial \mathcal{L}}{\partial h(t_1)}^{\!\top}\,\Phi(t_1,t)\,\frac{\partial f_\theta}{\partial \theta}\,dt .$$

This follows from the adjoint equation in that paper, since the adjoint is linear in its terminal condition. The gradient factors into two parts. The first is a terminal factor $\partial\mathcal{L}/\partial h(t_1)$, which is what Parametric Contrastive Learning shows is skewed by class frequency. The second is a propagation factor $\Phi(t_1,t)$.

Every existing rebalancing method acts only on the terminal factor. That includes class-weighted losses, the parametric class centers of Parametric Contrastive Learning, and the polar prototypes and minority-only supervision of A Tale of Two Classes. Scaling a sample's loss multiplies its whole backward signal by a constant. That cannot change how the signal is spread over time.

Our hypothesis is that the propagation factor is also class-dependent. A field shaped mostly by smooth majority trajectories learns to contract, so for minority sequences $\|\Phi(t_1,t)\|$ decays quickly as $t$ moves back toward the transient that defines the class. This would unify two observations in the literature that have been treated separately. One is the over-smoothing of sharp clinical events by continuous models, noted by Multi-Time Attention Networks. The other is the praise of that same smoothing as noise filtering in Hierarchical ODE. Both are the forward-pass face of $\Phi$. Our claim is that its backward-pass face is a gradient disparity that terminal rebalancing cannot repair.

Relation to Prior Work

Prior work plays three roles here.

Work that motivates the proposal. AdaTS builds dynamic soft contrasts for time series on discrete backbones such as TS2Vec. It reports that its embedding space shrinks when one class is oversampled tenfold. This is direct evidence that time-series contrastive learning collapses under imbalance, but it says nothing about continuous-time encoders. An NCDE-based Framework for Universal Representation Learning of Time Series (CTRL) combines a neural controlled differential equation backbone with reconstruction and hierarchical contrastive losses. It is evaluated only on balanced benchmarks and never examines how contrastive gradients pass through the solver. It is the closest existing system, and it is our substrate rather than our competitor. Theoretical Analysis of Contrastive Learning under Imbalanced Data shows that minority features fail to win neurons in a one-layer model and leaves continuous representations explicitly open. Our analysis is a continuous-depth counterpart that focuses on propagation rather than on neuron allocation.

Work that supplies the method. We reuse Latent ODE and ODE-RNN encoders from Latent Ordinary Differential Equations for Irregularly-Sampled Time Series. ODE-RNN applies a discrete GRU update at each observation and evolves the state continuously in between. We also reuse the adjoint machinery of Neural Ordinary Differential Equations. We use the Class Alignment Consistency metric of A Tale of Two Classes because it detects single-point collapse, which ordinary alignment and uniformity metrics miss.

Work that constrains the design. Multi-Time Attention Networks shows that an attention model with continuous time embeddings beats ODE encoders on PhysioNet 2012 and trains over 85 times faster than ODE-RNN. Any ODE-specific fix must therefore justify itself against a non-ODE encoder. Jump models such as Neural Jump Ordinary Differential Equations already inject transients through discrete updates. Our analysis must show that the decay happens after the jump, during the continuous flow, rather than at the jump itself.

Research Program

Diagnosis. We pretrain ODE-RNN, Latent ODE, and CTRL-style encoders with supervised contrastive loss on data with controlled imbalance. For each sample and each observation time we log three quantities, all of which the adjoint pass already provides at almost no extra cost:

  • the terminal gradient norm;
  • the adjoint norm $\|a(t)\|$;
  • the attenuation ratio $\|a(t)\|/\|a(t_1)\|$.

We then test three predictions. First, minority sequences show stronger attenuation than majority sequences at matched times. Second, this gap grows with the imbalance ratio and with training. Third, the gap persists after class-weighted or prototype-based terminal rebalancing, while the terminal-norm gap disappears.

Intervention. We add a forward-only near-isometry regularizer on minority trajectories. At a randomly chosen observation time we perturb the state by a small random direction $\delta$. We then integrate the perturbed and unperturbed states jointly to $t_1$ and penalize $\big(\log\|\delta h(t_1)\|-\log\|\delta\|\big)^2$ when it is negative. This penalizes contraction only and leaves expansion free.

The regularizer costs one extra forward solve and needs no second-order gradients. It is applied with a weight that depends on class frequency, so the vector field may still smooth majority noise, which is the property Hierarchical ODE values. It is not allowed to erase the dynamics that distinguish the rare class. We pair it with the standard terminal fixes, because the claim is that the two factors need separate treatment, not that terminal rebalancing is useless.

Cost. Stiffer, less contracting fields may raise the number of solver function evaluations. We report solver evaluations per epoch and wall-clock time alongside accuracy.

Baselines

Naive reference. Plain supervised contrastive pretraining on an ODE-RNN encoder, followed by a linear probe. This shows how badly the uncorrected continuous encoder fails.

Strongest terminal rebalancing on the same encoder. The same ODE-RNN encoder trained with the parametric class centers and Balanced Softmax of Parametric Contrastive Learning. We also run the SupConProto and SupMin variants from A Tale of Two Classes, which that paper reports improve downstream accuracy by up to 35% over SupCon at a 1% minority ratio. This is the most dangerous competitor, because it may already close most of the gap.

Strongest existing irregular-series encoder, which uses no ODE. Multi-Time Attention Networks reports a mortality AUC of 0.858 on PhysioNet 2012. The interpolation-prediction network of Modeling Irregularly Sampled Clinical Time Series reports an AUC of 0.853 and an AUPRC of 0.418 on MIMIC-III mortality. We run mTAN with the same terminal rebalancing as above. For CTRL under imbalance there is no reported result on Lacuna, so we run it ourselves.

Ablations. Two ablations isolate the key ingredient:

  • the isometry regularizer applied uniformly to all classes, which tests whether the benefit comes from class-targeting or just from generically less contracting dynamics;
  • a "time-dense contrast" variant that adds contrastive losses on intermediate states, which injects gradient early without touching $\Phi$.

Fairness and the bar for beating terminal rebalancing. All methods share the same data splits and encoder width. Each gets the same hyperparameter search budget, 30 configurations per method selected on validation AUPRC. All are matched for wall-clock pretraining time, not just epochs, given mTAN's speed advantage. Our method beats terminal rebalancing only if it improves minority recall at fixed precision and AUPRC over the best terminal-rebalanced ODE encoder across five seeds, with non-overlapping confidence intervals. It must also measurably shrink the attenuation gap.

Why not just…?

Why not just reweight the contrastive loss or add prototypes? Because of the factorization above, this rescales each sample's whole backward signal. It cannot move credit toward early transients. That is a claim the diagnosis tests directly. If terminal rebalancing alone also removes the attenuation gap and matches our minority recall, the propagation mechanism is not the bottleneck. We would then report that result and recommend Parametric Contrastive Learning-style fixes as sufficient for ODE encoders. This alternative is a real threat, and the experiment is designed so that it can win.

Why not just use mTAN and avoid ODEs? mTAN is faster and more accurate on PhysioNet 2012 in Multi-Time Attention Networks. If a rebalanced mTAN matches our regularized ODE encoder on minority recall, the practical recommendation is mTAN. The ODE contribution would then shrink to a diagnosis of why ODE encoders underperform on rare classes. Our reason to expect a difference is limited. ODE encoders retain advantages under heavy sparsity and distribution shift, as in the discrete-model collapse to false rejection rates above 0.90 reported by Hierarchical ODE. We therefore test at high missingness, where mTAN's fixed reference time points are most strained.

Why not just use jump ODEs, which handle transients with discrete updates? Jumps insert the transient into the state, but the continuous flow afterward can still contract it away. Our per-time logs separate the change in the adjoint across a jump from its decay between observations. If the decay happens mostly at jumps, the regularizer belongs on the jump network instead, and we will move it there.

Proposed Experiments

Controlled study. We start with a synthetic irregular-spiral task, building on the spiral setup of Neural Ordinary Differential Equations, where Latent ODE reached an RMSE of 0.16. The minority class differs only by a brief velocity kick at a random time. We sweep the imbalance ratio from 1 to 100 and the kick time from early to late. The decisive prediction is that the minority probe accuracy of terminal-rebalanced encoders degrades as the kick moves earlier, tracking the measured attenuation. Our regularizer should flatten that dependence.

Real benchmarks. We use the following tasks:

Task Native imbalance Reference result from the file
PhysioNet 2012 mortality Yes No AUPRC reported on Lacuna
MIMIC-III 48-hour mortality Yes Interpolation-prediction network: AUPRC 0.418
PhysioNet 2019 early sepsis prediction (challenge; Early Prediction of Sepsis from Clinical Data: the PhysioNet/Computing in Cardiology Challenge 2019) Yes, onset is rare and abrupt Winning teams: utility 0.33 to 0.36
Human Activity with artificially subsampled rare activities Imbalance ratios 10 and 100 mTAN reports 0.910 accuracy on the balanced version

The rare activities are subsampled to the stated ratios. We also add observation dropout at 50% and 90% to the clinical tasks.

Metrics. We report AUPRC, minority recall at fixed precision, and the sepsis utility score. We add Class Alignment Consistency, the embedding metric from A Tale of Two Classes found to track downstream performance (R² of 0.69). Finally, we report the class-conditional attenuation profile.

Decisive checks. We consider the claim supported only if three things hold. First, terminal rebalancing closes the terminal-norm gap but leaves a significant attenuation gap. Second, attenuation predicts per-sample minority errors after controlling for terminal norm. Third, the class-targeted regularizer outperforms both the uniform version and the time-dense contrast ablation on minority recall at matched compute. If the first check fails, the paper becomes a clean negative result showing that image-side rebalancing transfers unchanged to continuous-time encoders. That outcome would still be useful, given that CTRL and AdaTS never tested it.