Exercise: one-hot encoding DNA
Goal
Convert a batch of integer-encoded DNA sequences (A=0, C=1, G=2, T=3) of shape [N, L] into a one-hot tensor [a tensor where the integer category becomes a 1 in a dedicated channel, all other channels 0] of shape [N, 4, L]. This is the universal input shape for every sequence model — DeepBind, Basenji, Enformer all start here.
By the time cargo test passes you will have used Int tensors, one_hot, swap_dims, and float().
Background
The standard encoding for sequence ML:
[4, L] block; the column sum is 1 at every position.For a batch of N sequences the shape becomes [N, 4, L] — N is the batch axis, 4 is the channel axis (one channel per base), L is the position axis. This matches the [batch, channels, length] convention every 1-D convolution layer expects.
The PyTorch one-liner:
import torch.nn.functional as F
seqs = torch.tensor([[0, 1, 2, 3, 0]]) # [N, L] = [1, 5]
oh = F.one_hot(seqs, num_classes=4).float() # [N, L, 4]
oh = oh.transpose(1, 2) # [N, 4, L]The Burn version of the same three steps:
let seqs: Tensor<B, 2, Int> = ...; // [N, L]
let oh = seqs.one_hot(4); // [N, L, 4], still Int
let oh = oh.float(); // [N, L, 4], now Float
let oh = oh.swap_dims(1, 2); // [N, 4, L]Tensor<B, D, Int> is the integer-typed tensor (Burn defaults to floats; the Int marker switches to integers). Sequences are integers; their one-hot encoding is floats — .float() casts.
The exercise
Open the starter project:
cd day11/ex-one-hotsrc/main.rs contains:
- A function
seq_to_ints(seq: &str) -> Vec<i64>that mapsA/C/G/Tto0/1/2/3(already written for you). - A function
one_hot_dna<B: Backend>(seqs: Tensor<B, 2, Int>) -> Tensor<B, 3>with a// TODObody — this is what you implement. - A
mainthat builds an[N, L]integer tensor from some sample sequences and prints the resulting[N, 4, L]one-hot. - Four unit tests.
You may not change the function signature or the tests.
How to run
cd day11/ex-one-hot
cargo test
cargo runWhen the body is right you will see:
test result: ok. 4 passed; 0 failed; 0 ignored; 0 measured
Hints
one_hot on an Int tensor
one_hot(num_classes) is a method on the integer-typed tensor. It adds a new last axis of size num_classes and puts a 1 in the slot named by the integer, zeros elsewhere:
let s: Tensor<B, 2, Int> = ...; // [N, L]
let oh = s.one_hot(4); // [N, L, 4]Note the channel axis ends up last, not in the middle. The next step rearranges that.
swap_dims(d1, d2) reorders axes without copying data (it just changes the stride metadata). To go from [N, L, 4] to [N, 4, L]:
let oh = oh.swap_dims(1, 2); // [N, 4, L]PyTorch equivalent: oh.transpose(1, 2).
The integer one-hot is technically a tensor of 0s and 1s — perfectly representable as ints. But every conv layer and matmul expects floats. Convert with .float():
let oh = s.one_hot(4).float();Do this before swap_dims or after — it does not matter, both axis orders are valid.
Solution
fn one_hot_dna<B: Backend>(seqs: Tensor<B, 2, Int>) -> Tensor<B, 3> {
seqs.one_hot(4) // [N, L, 4] Int
.float() // [N, L, 4] Float
.swap_dims(1, 2) // [N, 4, L]
}The full reference is in day11/ex-one-hot-solution/src/main.rs.
Where this leads
- Exercise 3 (GC content) uses the integer form directly — no one-hot needed for reductions like “fraction of bases equal to G or C”.
- Exercise 6 (motif scanner) consumes the
[N, 4, L]float tensor you produced here as input to a 1-D conv layer. - The
[N, channels, L]convention is universal — protein models use[N, 20, L], RNA models with secondary-structure annotations use[N, 5, L], etc.