Skip to content

Commit e721592

Browse files
committed
fix a bug wit the mini-batch stochastich gradient descent and preprocessing
1 parent 872c356 commit e721592

File tree

3 files changed

+22
-21
lines changed

3 files changed

+22
-21
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "intricate"
3-
version = "0.6.1"
3+
version = "0.6.2"
44
edition = "2021"
55
license = "MIT"
66
authors = ["Gabriel Miranda"]

src/model.rs

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use opencl3::{
1515
memory::{Buffer, ClMem, CL_MEM_READ_WRITE},
1616
};
1717
use opencl3::{error_codes::cl_int, kernel::ExecuteKernel};
18+
use rand::prelude::*;
1819
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
1920
use savefile_derive::Savefile;
2021
use std::mem;
@@ -425,25 +426,29 @@ impl<'a> Model<'a> {
425426
let mut losses: Vec<f32> = Vec::with_capacity(training_options.epochs * steps_amount);
426427
let mut accuracies: Vec<f32> = Vec::with_capacity(training_options.epochs * steps_amount);
427428

428-
let mut per_step_input_batches: Vec<&[InputType]> = Vec::with_capacity(steps_amount);
429-
let mut per_step_output_batches: Vec<&[OutputType]> = Vec::with_capacity(steps_amount);
429+
let mut per_step_sample_batches: Vec<(&[InputType], &[OutputType])> =
430+
Vec::with_capacity(steps_amount);
430431

431432
for i_batch in 0..steps_amount {
432433
let count;
433434
let origin;
434435
if i_batch == steps_amount - 1 && samples_amount % training_options.batch_size != 0 {
435436
count = samples_amount % training_options.batch_size;
436-
origin = steps_amount - 1;
437+
origin = (steps_amount - 1) * training_options.batch_size - count;
437438
} else {
438439
count = training_options.batch_size;
439440
origin = i_batch * count;
440441
}
441442

442-
let batch_inputs = &training_input_samples[origin..(origin + count)];
443-
let batch_outputs = &training_expected_output_samples[origin..(origin + count)];
443+
let interval = origin..(origin + count);
444444

445-
per_step_input_batches.push(batch_inputs);
446-
per_step_output_batches.push(batch_outputs);
445+
let batch_inputs = &training_input_samples[interval.clone()];
446+
let batch_outputs = &training_expected_output_samples[interval.clone()];
447+
448+
per_step_sample_batches.push((
449+
batch_inputs,
450+
batch_outputs,
451+
));
447452
}
448453

449454
for epoch_index in 0..training_options.epochs {
@@ -475,36 +480,32 @@ impl<'a> Model<'a> {
475480
let steps_amount =
476481
(samples_amount as f32 / training_options.batch_size as f32).ceil() as usize;
477482

483+
let mut rng = thread_rng();
478484
for i_batch in 0..steps_amount {
485+
per_step_sample_batches.shuffle(&mut rng);
486+
479487
let batch_inputs: Buffer<cl_float> =
480-
(training_options.from_inputs_to_vectors)(per_step_input_batches[i_batch])?
488+
(training_options.from_inputs_to_vectors)(per_step_sample_batches[i_batch].0)?
481489
.par_iter()
482490
.flatten()
483491
.map(|x| *x)
484492
.collect::<Vec<f32>>()
485493
.to_buffer(false, state)?;
494+
486495
let batch_outputs: Buffer<cl_float> = (training_options
487496
.from_expected_outputs_to_vectors)(
488-
per_step_output_batches[i_batch]
497+
per_step_sample_batches[i_batch].1
489498
)?
490499
.par_iter()
491500
.flatten()
492501
.map(|x| *x)
493502
.collect::<Vec<f32>>()
494503
.to_buffer(false, state)?;
495504

496-
let local_batch_size;
497-
if i_batch == steps_amount - 1 && samples_amount % training_options.batch_size != 0
498-
{
499-
local_batch_size = samples_amount % training_options.batch_size;
500-
} else {
501-
local_batch_size = training_options.batch_size;
502-
}
503-
504505
let (optional_loss, optional_accuracy) = self.do_training_step(
505506
&batch_inputs,
506507
&batch_outputs,
507-
local_batch_size,
508+
per_step_sample_batches[i_batch].0.len(),
508509
training_options,
509510
)?;
510511

@@ -754,4 +755,4 @@ impl<'a> Model<'a> {
754755

755756
Ok(gradients)
756757
}
757-
}
758+
}

0 commit comments

Comments
 (0)