use crate::tensor::Tensor;
use crate::Device;
#[derive(Debug, Clone, PartialEq)]
pub struct Sample {
pub data: Vec<f32>,
pub shape: Vec<usize>,
}
impl Sample {
pub fn new(data: Vec<f32>, shape: Vec<usize>) -> Self {
assert_eq!(
shape.iter().product::<usize>(),
data.len(),
"sample shape {shape:?} does not match {} values",
data.len()
);
Self { data, shape }
}
}
pub trait Dataset {
fn len(&self) -> usize;
fn get(&self, index: usize) -> (Sample, Sample);
fn is_empty(&self) -> bool {
self.len() == 0
}
}
#[derive(Debug, Clone, Default)]
pub struct TensorDataset {
inputs: Vec<Sample>,
targets: Vec<Sample>,
}
impl TensorDataset {
pub fn new(inputs: Vec<Sample>, targets: Vec<Sample>) -> Self {
assert_eq!(
inputs.len(),
targets.len(),
"inputs and targets differ in length"
);
Self { inputs, targets }
}
}
impl Dataset for TensorDataset {
fn len(&self) -> usize {
self.inputs.len()
}
fn get(&self, index: usize) -> (Sample, Sample) {
(self.inputs[index].clone(), self.targets[index].clone())
}
}
pub struct DataLoader<D: Dataset> {
dataset: D,
batch_size: usize,
shuffle: bool,
drop_last: bool,
seed: u64,
device: Device,
}
impl<D: Dataset> DataLoader<D> {
pub fn new(dataset: D, batch_size: usize, shuffle: bool, device: Device) -> Self {
assert!(batch_size > 0, "batch_size must be at least 1");
Self {
dataset,
batch_size,
shuffle,
drop_last: false,
seed: 0x5eed,
device,
}
}
pub fn drop_last(mut self, drop_last: bool) -> Self {
self.drop_last = drop_last;
self
}
pub fn seed(mut self, seed: u64) -> Self {
self.seed = seed;
self
}
pub fn num_batches(&self) -> usize {
batch_count(self.dataset.len(), self.batch_size, self.drop_last)
}
pub fn indices(&self, epoch: u64) -> Vec<usize> {
sample_order(self.dataset.len(), self.shuffle, self.seed, epoch)
}
pub fn epoch(&self, epoch: u64) -> impl Iterator<Item = (Tensor, Tensor)> + '_ {
let order = self.indices(epoch);
let batches = self.num_batches();
(0..batches).map(move |b| {
let start = b * self.batch_size;
let end = (start + self.batch_size).min(order.len());
self.stack(&order[start..end])
})
}
fn stack(&self, idx: &[usize]) -> (Tensor, Tensor) {
let (mut xs, mut ys) = (Vec::new(), Vec::new());
let (mut x_shape, mut y_shape) = (Vec::new(), Vec::new());
for &i in idx {
let (x, y) = self.dataset.get(i);
if x_shape.is_empty() {
x_shape = x.shape.clone();
y_shape = y.shape.clone();
}
assert_eq!(x.shape, x_shape, "inputs in a batch must share a shape");
assert_eq!(y.shape, y_shape, "targets in a batch must share a shape");
xs.extend(x.data);
ys.extend(y.data);
}
let with_batch = |s: &[usize]| [&[idx.len()][..], s].concat();
(
Tensor::from_data(&xs, &with_batch(&x_shape), &self.device),
Tensor::from_data(&ys, &with_batch(&y_shape), &self.device),
)
}
}
fn batch_count(n: usize, batch_size: usize, drop_last: bool) -> usize {
if drop_last {
n / batch_size
} else {
n.div_ceil(batch_size)
}
}
fn sample_order(n: usize, shuffle: bool, seed: u64, epoch: u64) -> Vec<usize> {
let mut order: Vec<usize> = (0..n).collect();
if shuffle {
let mut state = (seed ^ epoch.wrapping_mul(0x9e37_79b9_7f4a_7c15)) | 1;
for i in (1..n).rev() {
state ^= state >> 12;
state ^= state << 25;
state ^= state >> 27;
let r = state.wrapping_mul(0x2545_f491_4f6c_dd1d);
order.swap(i, (r % (i as u64 + 1)) as usize);
}
}
order
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn batch_count_respects_drop_last() {
assert_eq!(batch_count(10, 3, false), 4);
assert_eq!(batch_count(10, 3, true), 3);
assert_eq!(batch_count(0, 3, false), 0);
}
#[test]
fn shuffle_is_a_seeded_permutation() {
let mut a = sample_order(50, true, 7, 0);
assert_ne!(a, (0..50).collect::<Vec<_>>());
assert_eq!(a, sample_order(50, true, 7, 0));
assert_ne!(a, sample_order(50, true, 7, 1));
a.sort_unstable();
assert_eq!(a, (0..50).collect::<Vec<_>>());
}
#[test]
fn unshuffled_order_is_identity() {
assert_eq!(sample_order(5, false, 7, 3), vec![0, 1, 2, 3, 4]);
}
#[test]
fn tensor_dataset_returns_pairs() {
let ds = TensorDataset::new(
vec![Sample::new(vec![1.0, 2.0], vec![2])],
vec![Sample::new(vec![3.0], vec![1])],
);
assert_eq!(ds.len(), 1);
assert_eq!(ds.get(0).1.data, vec![3.0]);
}
}