# How to onehot encode batches of sequences?

**URL:** <https://discourse.julialang.org/t/how-to-onehot-encode-batches-of-sequences/33237>\
**Category:** General Usage\
**Tags:** array, linearalgebra, flux, machine-learning\
**Created:** [January 11, 2020, 2:49pm UTC](https://discourse.julialang.org/t/how-to-onehot-encode-batches-of-sequences/33237 "2020-01-11T14:49:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [January 11, 2020, 2:49pm UTC](https://discourse.julialang.org/t/how-to-onehot-encode-batches-of-sequences/33237/1 "2020-01-11T14:49:13Z")

</div>

Is there a recommended way to one-hot encode a batch of sequences?

More precisely, I am modelling sequences from an alphabet with `q` letters. A sequence of length `N` can be one-hot encoded as a `q x N` one-hot matrix (e.g., using `Flux.OneHotMatrix`). Then it seems that to encode a batch of `B` sequences I would need a `q x N x B` “one-hot tensor”.

What’s the recommended approach?

---

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [January 11, 2020, 3:05pm UTC](https://discourse.julialang.org/t/how-to-onehot-encode-batches-of-sequences/33237/2 "2020-01-11T15:05:48Z")

</div>

One possibility is to put the data into a `q x (N * B)` one-hot matrix, and then reshape this into `q x N x B` for downstream processing. But this is slow.

---

<div class="post-metadata">

**Author:** ![mantzaris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mantzaris/32/3852_2.png) [@mantzaris](https://discourse.julialang.org/u/mantzaris)\
**Post date:** [January 17, 2023, 12:37am UTC](https://discourse.julialang.org/t/how-to-onehot-encode-batches-of-sequences/33237/3 "2023-01-17T00:37:40Z")

</div>

I currently put each one hot matrix as a batch and then that matrix into a vector so that the batch is each 1 hot matrix where the N are the sequence steps, and in the loop I restack the data via

```julia
x_batch = [Flux.stack([Float32.(x[ii][:,tt]) for ii in 1:length(x)],dims=2) for tt in 1:length(x[1][1,:]) ]

```

I agree that it is suboptimal and am about to ask a question with more detail provided to get a best practice response hopefully.
