@@ -15,6 +15,7 @@ use opencl3::{
1515 memory:: { Buffer , ClMem , CL_MEM_READ_WRITE } ,
1616} ;
1717use opencl3:: { error_codes:: cl_int, kernel:: ExecuteKernel } ;
18+ use rand:: prelude:: * ;
1819use rayon:: iter:: { IntoParallelRefIterator , ParallelIterator } ;
1920use savefile_derive:: Savefile ;
2021use 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