-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d99d925
commit ac1b57b
Showing
5 changed files
with
115 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
use burn::data::dataset::Dataset; | ||
use rand::{prelude::SliceRandom, rngs::StdRng, SeedableRng}; | ||
use std::marker::PhantomData; | ||
|
||
pub struct BatchShuffledDataset<D, I> { | ||
dataset: D, | ||
indices: Vec<usize>, | ||
input: PhantomData<I>, | ||
} | ||
|
||
impl<D, I> BatchShuffledDataset<D, I> | ||
where | ||
D: Dataset<I>, | ||
{ | ||
/// Creates a new shuffled dataset. | ||
pub fn new(dataset: D, batch_size: usize, rng: &mut StdRng) -> Self { | ||
let len = dataset.len(); | ||
|
||
// 计算批数 | ||
let num_batches = (len + batch_size - 1) / batch_size; | ||
|
||
// 创建一个批数索引的向量并打乱 | ||
let mut batch_indices: Vec<usize> = (0..num_batches).collect(); | ||
batch_indices.shuffle(rng); | ||
|
||
// 为每个打乱的批次生成相应的元素索引 | ||
let mut indices: Vec<usize> = Vec::new(); | ||
for &batch_index in &batch_indices { | ||
let start_index = batch_index * batch_size; | ||
let end_index = std::cmp::min(start_index + batch_size, len); | ||
indices.extend(start_index..end_index); | ||
} | ||
|
||
Self { | ||
dataset, | ||
indices, | ||
input: PhantomData, | ||
} | ||
} | ||
|
||
/// Creates a new shuffled dataset with a fixed seed. | ||
pub fn with_seed(dataset: D, batch_size: usize, seed: u64) -> Self { | ||
let mut rng = StdRng::seed_from_u64(seed); | ||
Self::new(dataset, batch_size, &mut rng) | ||
} | ||
} | ||
|
||
impl<D, I> Dataset<I> for BatchShuffledDataset<D, I> | ||
where | ||
D: Dataset<I>, | ||
I: Clone + Send + Sync, | ||
{ | ||
fn get(&self, index: usize) -> Option<I> { | ||
let index = match self.indices.get(index) { | ||
Some(index) => index, | ||
None => return None, | ||
}; | ||
self.dataset.get(*index) | ||
} | ||
|
||
fn len(&self) -> usize { | ||
self.dataset.len() | ||
} | ||
} | ||
|
||
|
||
#[test] | ||
fn test_batch_shuffle() { | ||
use crate::dataset::FSRSDataset; | ||
let dataset = FSRSDataset::train(); | ||
let batch_size = 10; | ||
let seed = 42; | ||
let batch_shuffled_dataset: BatchShuffledDataset<FSRSDataset, crate::dataset::FSRSItem> = BatchShuffledDataset::with_seed(dataset, batch_size, seed); | ||
for i in 0..batch_shuffled_dataset.len() { | ||
println!("{:?}", batch_shuffled_dataset.get(i).unwrap()); | ||
if i > batch_size { | ||
break; | ||
} | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_item_shuffle() { | ||
use crate::dataset::FSRSDataset; | ||
use burn::data::dataset::transform::ShuffledDataset; | ||
let dataset = FSRSDataset::train(); | ||
let seed = 42; | ||
let shuffled_dataset: ShuffledDataset<FSRSDataset, crate::dataset::FSRSItem> = ShuffledDataset::with_seed(dataset, seed); | ||
for i in 0..shuffled_dataset.len() { | ||
println!("{:?}", shuffled_dataset.get(i).unwrap()); | ||
if i > 10 { | ||
break; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,5 @@ pub mod dataset; | |
pub mod model; | ||
pub mod training; | ||
mod weight_clipper; | ||
|
||
mod dataloader; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters