@@ -167,7 +167,7 @@ private BatchesCollection([NotNull] SamplesBatch[] batches)
167
167
/// <exception cref="ArgumentOutOfRangeException">The dataset and result matrices have a different number of rows</exception>
168
168
[ NotNull ]
169
169
[ CollectionAccess ( CollectionAccessType . Read ) ]
170
- public static BatchesCollection From ( ( float [ , ] X , float [ , ] Y ) dataset , int size )
170
+ public static unsafe BatchesCollection From ( ( float [ , ] X , float [ , ] Y ) dataset , int size )
171
171
{
172
172
// Local parameters
173
173
if ( size < 10 ) throw new ArgumentOutOfRangeException ( nameof ( size ) , "The batch size can't be smaller than 10" ) ;
@@ -183,21 +183,24 @@ public static BatchesCollection From((float[,] X, float[,] Y) dataset, int size)
183
183
nBatchMod = samples % size ;
184
184
bool oddBatchPresent = nBatchMod > 0 ;
185
185
SamplesBatch [ ] batches = new SamplesBatch [ nBatches + ( oddBatchPresent ? 1 : 0 ) ] ;
186
- for ( int i = 0 ; i < batches . Length ; i ++ )
186
+ fixed ( float * px = dataset . X , py = dataset . Y )
187
187
{
188
- if ( oddBatchPresent && i == batches . Length - 1 )
188
+ for ( int i = 0 ; i < batches . Length ; i ++ )
189
189
{
190
- batches [ i ] = SamplesBatch . From (
191
- Span < float > . DangerousCreate ( dataset . X , ref dataset . X [ i * size , 0 ] , nBatchMod * wx ) ,
192
- Span < float > . DangerousCreate ( dataset . Y , ref dataset . Y [ i * size , 0 ] , nBatchMod * wy ) ,
193
- wx , wy ) ;
194
- }
195
- else
196
- {
197
- batches [ i ] = SamplesBatch . From (
198
- Span < float > . DangerousCreate ( dataset . X , ref dataset . X [ i * size , 0 ] , size * wx ) ,
199
- Span < float > . DangerousCreate ( dataset . Y , ref dataset . Y [ i * size , 0 ] , size * wy ) ,
200
- wx , wy ) ;
190
+ if ( oddBatchPresent && i == batches . Length - 1 )
191
+ {
192
+ batches [ i ] = SamplesBatch . From (
193
+ new Span < float > ( px + i * size * wx , nBatchMod * wx ) ,
194
+ new Span < float > ( py + i * size * wy , nBatchMod * wy ) ,
195
+ wx , wy ) ;
196
+ }
197
+ else
198
+ {
199
+ batches [ i ] = SamplesBatch . From (
200
+ new Span < float > ( px + i * size * wx , size * wx ) ,
201
+ new Span < float > ( py + i * size * wy , size * wy ) ,
202
+ wx , wy ) ;
203
+ }
201
204
}
202
205
}
203
206
return new BatchesCollection ( batches ) ;
0 commit comments