Inferensys

Glossary

One-Hot Encoding

A numerical representation of DNA sequence where each nucleotide (A, C, G, T) is mapped to a binary vector of length four with a single high bit, converting a character string into a sparse matrix for neural network input.
Engineer reviewing vector database search results on laptop, embeddings visualization on screen, home office coding session.
SEQUENCE REPRESENTATION

What is One-Hot Encoding?

One-hot encoding is the foundational numerical representation that converts a categorical DNA sequence string into a sparse binary matrix suitable for input into neural networks.

One-hot encoding is a process that maps each nucleotide in a DNA sequence—adenine (A), cytosine (C), guanine (G), and thymine (T)—to a unique binary vector of length four. In this sparse matrix representation, a single 'high' bit (1) is set at the index corresponding to the nucleotide, while all other positions are 'low' (0), effectively converting a character string into a structured, machine-readable numerical format.

This transformation is a critical preprocessing step for convolutional neural networks in genomics, as it eliminates artificial ordinal relationships between nucleotides that do not exist biologically. The resulting [sequence_length, 4] tensor allows the model's first convolutional layer to learn distinct filter weights for each base, enabling the detection of sequence motifs like transcription factor binding sites.

FOUNDATIONAL REPRESENTATION

Key Features of One-Hot Encoding in Genomics

One-hot encoding is the critical preprocessing bridge that converts a string of nucleotides into a numerical tensor a neural network can consume. The following cards detail the structural properties, biological constraints, and computational implications of this sparse representation.

01

The 4-Channel Binary Vector

Each nucleotide is mapped to a unique binary vector of length four: A = [1,0,0,0], C = [0,1,0,0], G = [0,0,1,0], and T = [0,0,0,1]. This creates an orthogonal basis where all bases are equidistant in vector space.

  • No ordinal relationships: The encoding prevents the model from falsely assuming 'A' is closer to 'C' than 'G', a critical failure mode of integer encoding.
  • Sparse input tensor: A 1,000-base-pair sequence becomes a [4, 1000] matrix, where exactly one bit is 'hot' per column.
  • Ambiguity handling: IUPAC ambiguity codes (e.g., 'N' for any base) are represented as [0.25, 0.25, 0.25, 0.25], distributing probability mass uniformly.
02

Strand Symmetry via Reverse Complement

DNA is double-stranded and binding events are strand-agnostic. One-hot encoding enables a precise mathematical operation to enforce this symmetry.

  • Reverse complement operation: The input tensor is flipped along the sequence axis, and the channel order is permuted: A↔T and C↔G.
  • Data augmentation: Training samples are randomly reverse-complemented to teach the model strand invariance without doubling the dataset size.
  • Prediction averaging: At inference, predictions from the forward and reverse-complement sequences are averaged to produce a strand-symmetric output, a standard practice in tools like DeepBind and BPNet.
03

Convolutional Filter Interpretation

The one-hot structure allows the first-layer convolutional filters to be directly interpreted as Position Weight Matrices (PWMs).

  • Filter visualization: A learned [4, filter_width] weight matrix can be plotted as a sequence logo, revealing the binding motif a neuron has learned to detect.
  • Activation maps: Scanning the filter across the one-hot input produces an activation track that corresponds to a motif scanning score, functionally equivalent to a PWM scan.
  • TF-MoDISco: This interpretability tool clusters high-attribution subsequences from one-hot inputs to extract consolidated, non-redundant motifs learned by the network.
04

Sparsity and Memory Footprint

The representation is highly sparse (75% zeros), which has significant implications for storage and compute.

  • Memory inefficiency: Storing a full 32-bit float tensor for a 3-billion-base-pair human genome requires approximately 48 GB of memory, which is prohibitive.
  • Embedding layer optimization: In practice, a trainable embedding layer maps integer-encoded bases (A=0, C=1, G=2, T=3) to dense vectors, learning a continuous representation rather than using fixed one-hot vectors.
  • Mixed precision: When one-hot is used directly, storing the tensor in float16 or bfloat16 halves the memory footprint without impacting model accuracy.
05

In Silico Mutagenesis

One-hot encoding enables a systematic computational perturbation assay to identify causal regulatory variants.

  • Saturation mutagenesis: Every position in the input sequence is systematically mutated to all three alternative bases, and the change in the model's prediction is recorded.
  • Attribution scores: The difference between the reference prediction and the mutated prediction quantifies the functional impact of a single nucleotide variant.
  • Integrated Gradients: This interpretability method computes feature attributions by interpolating between a zero baseline and the one-hot input, accumulating gradients along the path to identify bases critical for binding.
06

Multi-Task Input Architecture

One-hot encoded sequence serves as the primary input, but it is often concatenated with additional channels representing epigenomic data.

  • Multi-channel input: Beyond the 4 nucleotide channels, additional tracks for chromatin accessibility, histone marks, or evolutionary conservation scores are stacked to create a [C, L] tensor.
  • Unified representation: Models like DeepSEA and Enformer consume this multi-channel input to simultaneously predict hundreds of epigenetic assays from raw sequence.
  • Distance encoding: For models predicting 3D genome folding, one-hot sequence is combined with a genomic distance channel to provide the linear separation between loci.
SEQUENCE REPRESENTATION COMPARISON

One-Hot Encoding vs. Alternative Genomic Representations

Comparison of numerical encoding strategies for converting raw nucleotide sequences into machine-readable input tensors for deep learning models.

FeatureOne-Hot EncodingLearned Embeddingk-mer Frequency

Dimensionality per nucleotide

4

64–256 (configurable)

4^k (e.g., 256 for 4-mers)

Sparsity

High (75% zeros)

Dense

Variable

Learnable parameters

Captures nucleotide similarity

Preserves positional information

Risk of overfitting

Low

Moderate

High with large k

Interpretability

High

Low

Moderate

Typical use case

Convolutional models (DeepBind, BPNet)

Transformer models (DNABERT, Enformer)

Classical ML (SVM, random forest)

ONE-HOT ENCODING FOR GENOMICS

Frequently Asked Questions

Clear, technical answers to the most common questions about representing DNA sequences as numerical matrices for deep learning models.

One-hot encoding is a numerical representation scheme that converts a DNA character string into a binary matrix where each nucleotide (A, C, G, T) is mapped to a vector of length four with a single high bit (1) and three low bits (0). Specifically, A = [1,0,0,0], C = [0,1,0,0], G = [0,0,1,0], and T = [0,0,0,1]. This transformation converts a sequence of length L into an L × 4 sparse binary matrix suitable as input for convolutional neural networks. The encoding preserves the categorical nature of nucleotides without imposing an artificial ordinal relationship—unlike integer encoding (A=1, C=2, G=3, T=4), which would incorrectly imply that G is 'greater than' A in a mathematical sense. This representation is the foundational input layer for architectures like DeepBind, DeepSEA, and BPNet.

Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.