Skip to content

Commit ea21400

Browse files
committed
Removed last Span<T>.DangerousCreate call
1 parent e49d8e8 commit ea21400

File tree

1 file changed

+17
-14
lines changed

1 file changed

+17
-14
lines changed

NeuralNetwork.NET/SupervisedLearning/Data/BatchesCollection.cs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ private BatchesCollection([NotNull] SamplesBatch[] batches)
167167
/// <exception cref="ArgumentOutOfRangeException">The dataset and result matrices have a different number of rows</exception>
168168
[NotNull]
169169
[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)
171171
{
172172
// Local parameters
173173
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)
183183
nBatchMod = samples % size;
184184
bool oddBatchPresent = nBatchMod > 0;
185185
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)
187187
{
188-
if (oddBatchPresent && i == batches.Length - 1)
188+
for (int i = 0; i < batches.Length; i++)
189189
{
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+
}
201204
}
202205
}
203206
return new BatchesCollection(batches);

0 commit comments

Comments
 (0)