This repository contains a high-performance PyTorch implementation of an Amortized Bayesian Tensor Factorization framework. It is designed to address the critical scalability bottlenecks in spatial transcriptomics and single-cell RNA-sequencing (scRNA-seq) analysis. By replacing traditional, iterative Markov Chain Monte Carlo (MCMC) and local Variational Inference (VI) updates with an Amortized Neural Encoder, this framework scales probabilistic tensor decomposition to massive tissue datasets while strictly preserving Bayesian uncertainty quantification.
This architecture is specifically optimized for high-dimensional clinical datasets, such as those generated by the FunGen-AD consortium, enabling the discovery of latent biological pathways across complex spatial morphologies.
In spatial transcriptomics, biological tissue is naturally represented as a multi-way array, or tensor
Traditional deep learning matrix factorization yields deterministic point estimates, failing to capture the inherent noise and uncertainty of biological sequencing. Conversely, rigorous Bayesian Tensor Factorization provides excellent uncertainty estimates but relies on local parameter updates (e.g., Gibbs Sampling). The computational complexity of these updates scales linearly with the number of cells
The Solution: We bridge this gap via Amortized Inference. Instead of optimizing local variational parameters for every independent cell, we train a global deep neural network (the Encoder) to parameterize the variational distributions. This reduces the inference of a cell's latent state from an iterative optimization problem to a single, highly parallelizable
We assume the observed transcriptomic tensor
-
$\mathbf{A} \in \mathbb{R}^{I \times R}$ : Gene expression signatures. -
$\mathbf{B} \in \mathbb{R}^{J \times R}$ : Cell-specific pathway activation. -
$\mathbf{C} \in \mathbb{R}^{K \times R}$ : Spatial morphological dependencies.
The deterministic reconstruction of the tensor is given by the sum of
We treat the spatial (
We introduce an inference network
To allow gradient flow through the stochastic sampling process, we utilize the Reparameterization Trick:
The network and global parameters are jointly optimized by maximizing the Evidence Lower Bound (ELBO). We minimize the negative ELBO, which balances data likelihood (reconstruction fidelity) with a Kullback-Leibler (KL) divergence regularizer that penalizes deviation from a standard normal prior:
BayesTensor/
├── src/
│ ├── data_gen/
│ │ └── synthetic.py # Generates ground-truth multi-way arrays
│ ├── models/
│ │ ├── encoder.py # Multi-Layer Perceptron (MLP) Inference Network
│ │ └── amortized_cp.py # CP Tensor Decoder & Reparameterization framework
│ ├── real_data.py # Automated preprocessing pipeline for scRNA-seq
│ ├── train.py # Simulation Study (Identifiability Proof)
│ └── train_real.py # Biological Application Engine
├── outputs/
│ └── plots/ # Auto-generated metrics and factor visual validation
├── requirements.txt
└── README.md
To prove the architecture correctly discovers underlying topological structure, we generate a synthetic tensor with a known low-rank ground truth, inject Gaussian noise, and train the model to recover the exact spatial factors.
python -m src.trainExpected Output: Successful convergence logs and an auto-generated heatmap (saved to outputs/plots/) demonstrating alignment between the simulated Ground Truth spatial factors and the model's Amortized CP Inference factors.
This pipeline assesses the model's performance on highly sparse, correlated biological data. It fetches a benchmark 10x Genomics Peripheral Blood Mononuclear Cell (PBMC) dataset, executes standard bioinformatics preprocessing (normalization, log-transformation, HVG selection via scanpy), projects the cells into a spatial topology, and executes the Amortized training loop.
torch >= 2.0.0
numpy >= 1.23.0
matplotlib >= 3.7.0
scanpy >= 1.9.0
anndata >= 0.9.0
pandas >= 2.0.0
This framework demonstrates that Amortized Variational Inference provides a scalable, mathematically rigorous alternative to traditional tensor decomposition methods. By leveraging neural networks for parameter estimation, we achieve a significant reduction in computational latency while preserving the probabilistic integrity required for clinical transcriptomic discovery.
If you use this framework in your research, please cite:
@article{Mboya2024Amortized,
title={Amortized Variational Inference for Scalable Bayesian Tensor Factorization},
author={Mboya, Grold Otieno},
year={2026}
}This project is licensed under the MIT License