You’re an expert programmer with a expert understanding of best coding practices and numerical computing. You always review the code I give to you before you answer to check if it would need improvements before proceeding with your answer.  this is a class library for neural networks for real world applications, the library must be able to handle small networks like simple classifiers and large networks like LLMs . so you must always consider for best possible answer with best performance, optimizations and least memory footprint,  use Tensor<T>.ParallelizationThreshold static property when preferred or required for best practices. you will produce fully complete and working code. 
When I ask you for code improvements or bug fix, you will only generate new code or code changes, don't re-generate unchanged or old code, also with the changes you provide code copy paste explanation so I can avoid any copy paste errors and to avoid human confusion.
Please note this class library is on multiply file and large code base, so If you want to know about certain implementation to provide complete answer then assume it is already implemented and well optimized, also you can ask for code snippets and I will provide to you my current up-to-date implementation.

Programming language : c#
Application Type: class library

 private async Task SpiralDataset()
 {
     var (inputs, targets) = GenerateSpiralDataset(1000);
     int totalSamples = inputs.Shape[0];
     int trainSamples = (int)(totalSamples * 0.8);
     int validationSamples = totalSamples - trainSamples;

     // Define activation functions
     var reluActivation = ActivationFunctionFactory<float>.Create(ActivationFunctionType.Relu);
     var softActivation = ActivationFunctionFactory<float>.Create(ActivationFunctionType.Softmax);

     // Define initializers
     var weightInitializer = new HeInitializer<float>();
     var biasInitializer = new ZeroInitializer<float>();

     // Training data
     var trainingData = inputs.Slice(0, trainSamples);
     var trainingLabels = targets.Slice(0, trainSamples);

     // Validation data
     var validationData = inputs.Slice(trainSamples, validationSamples);
     var validationLabels = targets.Slice(trainSamples, validationSamples);

     StringBuilder result = new StringBuilder();

     var neuralNetwork = new NeuralNetwork<float>(
         logger: new ConsoleLogger<float>(),
         visualizer: new SimpleVisualizer<float>()
     );

     // Add layers to the network
     neuralNetwork.AddLayer(new DenseLayer<float>(
         inputSize: 2,
         outputSize: 64,
         activation: reluActivation,
         weightInitializer: weightInitializer,
         biasInitializer: biasInitializer,
         name: "HiddenLayer1",
         logger: neuralNetwork.Logger
     ));

     neuralNetwork.AddLayer(new DenseLayer<float>(
         inputSize: 64,
         outputSize: 64,
         activation: reluActivation,
         weightInitializer: weightInitializer,
         biasInitializer: biasInitializer,
         name: "HiddenLayer2",
         logger: neuralNetwork.Logger
     ));

     neuralNetwork.AddLayer(new DenseLayer<float>(
         inputSize: 64,
         outputSize: 3, // Assuming 3 classes in the spiral dataset
         activation: softActivation,
         weightInitializer: weightInitializer,
          biasInitializer: biasInitializer,
         name: "OutputLayer",
         logger: neuralNetwork.Logger
     ));

     var optimizer = new SGDOptimizer<float>(learningRate: 0.01f);
     var lossFunction = new CrossEntropyLoss<float>();
     var logger = neuralNetwork.Logger; // Use the same logger

     // Initialize the network
     neuralNetwork.InitializeWeights();


     var batchSize = 32;
     var epochs = 100;

     // Calculate total batches
     int totalBatches = (trainSamples + batchSize - 1) / batchSize;

     var trainer = new Trainer<float>(
         network: neuralNetwork,
         lossFunction: lossFunction,
         optimizer: optimizer,
         logger: logger,
         epochs: epochs,
         batchSize: batchSize//,
                             //  regularization: regularization,
                             //  learningRateScheduler: learningRateScheduler
     );

     trainer.GradientClippingThreshold = 1.0f; // Or another appropriate value

     // Configure early stopping and checkpointing
     trainer.UseEarlyStopping = true;
     trainer.EarlyStoppingPatience = 10;
     trainer.EnableCheckpointing = true;
     trainer.CheckpointDirectory = "checkpoints";
     trainer.CheckpointInterval = 1;

     // Subscribe to events
     // Set maximum to the total number of epochs and batches
     prgEpochs.Maximum = epochs;          // Total number of epochs
     prgBatchs.Maximum = totalBatches;    // Total number of batches per epoch

     // Update progress bars inside event handlers
     trainer.BatchCompleted += (epoch, batch, loss) =>
     {
         this.Invoke((MethodInvoker)(() =>
         {
             prgBatchs.Value = batch;
             // Optionally update other UI elements
         }));
     };

     trainer.EpochCompleted += (epoch, totalEpochs, validationLoss) =>
     {
         this.Invoke((MethodInvoker)(() =>
         {
             prgEpochs.Value = epoch;
             txtLog.AppendText($"Epoch {epoch}/{totalEpochs}: Validation Loss = {validationLoss}{Environment.NewLine}");
             prgBatchs.Value = 0; // Reset batch progress bar for the next epoch
             txtResult.AppendText($"{trainer.Metrics.GetSummary()}{Environment.NewLine}");
         }));
     };

     // Before training
     CheckData(trainingData, "Training Data");
     CheckData(trainingLabels, "Training Labels");
     CheckData(validationData, "Validation Data");
     CheckData(validationLabels, "Validation Labels");


     // Start training
     await trainer.TrainAsync(
         trainingData: trainingData,
         trainingLabels: trainingLabels,
         validationData: validationData,
         validationLabels: validationLabels,
         cancellationToken: CancellationToken.None
     );

     // Evaluate the model on validation data
     var evaluator = new Evaluator<float>(neuralNetwork, logger);
     var finalValidationLoss = evaluator.Evaluate(validationData, validationLabels, lossFunction);

     // Optionally, compute accuracy
     int correct = 0;
     var predictions = neuralNetwork.Forward(validationData);
     for (int i = 0; i < predictions.Shape[0]; i++)
     {
         float predictedValue = predictions[i, 0];
         int predictedClass = predictedValue >= 0.5f ? 1 : 0;
         result.AppendLine($"(validationData[{i}, 0],    validationData[{i}, 1]): Input: {validationData[i, 0]}, {validationData[i, 1]} - Predicted: {predictedClass}, Predicted Value: {predictedValue}, Probability: {predictedValue:F4}");
     }

     for (int i = 0; i < validationData.Shape[0]; i++)
     {
         // Extract the ith prediction vector (1D tensor of class probabilities)
         var predictedVector = predictions.Slice(i, 1).Squeeze(axis: 0);

         // Use ArgMax on the predicted vector
         var predictedClass = ArgMax(predictedVector);

         // Extract the true class from one-hot encoded targets
         var targetVector = validationLabels.Slice(i, 1).Squeeze(axis: 0);
         var trueClass = ArgMax(targetVector);

         if (predictedClass == trueClass)
         {
             correct++;
         }
     }



     float accuracy = (float)correct / validationData.Shape[0];
     txtResult.AppendText(result.ToString());
     txtResult.AppendText($"Validation Accuracy: {accuracy * 100:F2}%{Environment.NewLine}");
 }

 static (Tensor<float> Inputs, Tensor<float> Targets) GenerateSpiralDataset(int samplesPerClass)
 {
     int numClasses = 3; // Changed from 2 to 3
     int dimensions = 2;
     int totalSamples = samplesPerClass * numClasses;

     var inputs = new Tensor<float>(totalSamples, dimensions);
     var targets = new Tensor<float>(totalSamples, numClasses);

     Random rand = new Random();

     for (int classNumber = 0; classNumber < numClasses; classNumber++)
     {
         for (int i = 0; i < samplesPerClass; i++)
         {
             float r = (float)i / samplesPerClass;
             float theta = (float)(classNumber * Math.PI + r * 4 * Math.PI + rand.NextDouble() * 0.2);

             float x = r * (float)Math.Sin(theta);
             float y = r * (float)Math.Cos(theta);

             int index = classNumber * samplesPerClass + i;
             inputs[index, 0] = x;
             inputs[index, 1] = y;

             targets[index, classNumber] = 1.0f; // One-hot encoding
         }
     }

     return (inputs, targets);
 }
this example test run fine before, now it give wrong predictions after i made changes in Tensor<T> class, 
here is result:
Epoch: 0, AverageError: 1.081343, MinError: 1.0202947, MaxError: 1.1487558, StandardDeviation: 0.023827476, Variance: 0.0005677487, SumError: 81.10072, EpochDuration: 00:00:00.
Epoch: 1, AverageError: 1.08035, MinError: 1.0308167, MaxError: 1.1327838, StandardDeviation: 0.022845788, Variance: 0.00052193, SumError: 81.02624, EpochDuration: 00:00:02.0772809.
Epoch: 2, AverageError: 1.0635324, MinError: 0.99975634, MaxError: 1.1226579, StandardDeviation: 0.026679074, Variance: 0.000711773, SumError: 79.76493, EpochDuration: 00:00:01.8115141.
Epoch: 3, AverageError: 1.0710468, MinError: 0.985069, MaxError: 1.1220236, StandardDeviation: 0.02563048, Variance: 0.00065692153, SumError: 80.32853, EpochDuration: 00:00:01.8504577.
Epoch: 4, AverageError: 1.0574627, MinError: 0.99643594, MaxError: 1.1095222, StandardDeviation: 0.023450518, Variance: 0.00054992683, SumError: 79.30974, EpochDuration: 00:00:01.9327105.
Epoch: 5, AverageError: 1.0554012, MinError: 0.96851796, MaxError: 1.123224, StandardDeviation: 0.030304004, Variance: 0.00091833266, SumError: 79.155106, EpochDuration: 00:00:01.7621959.
Epoch: 6, AverageError: 1.053703, MinError: 0.99409777, MaxError: 1.1376574, StandardDeviation: 0.03159711, Variance: 0.0009983773, SumError: 79.02775, EpochDuration: 00:00:01.7756945.
Epoch: 7, AverageError: 1.0517278, MinError: 0.981243, MaxError: 1.1513139, StandardDeviation: 0.030231694, Variance: 0.0009139553, SumError: 78.87957, EpochDuration: 00:00:01.7749262.
Epoch: 8, AverageError: 1.0505785, MinError: 0.992251, MaxError: 1.1364422, StandardDeviation: 0.026594164, Variance: 0.00070724956, SumError: 78.7934, EpochDuration: 00:00:01.7779641.
Epoch: 9, AverageError: 1.049625, MinError: 1.0016767, MaxError: 1.1584978, StandardDeviation: 0.028100546, Variance: 0.00078964076, SumError: 78.72189, EpochDuration: 00:00:01.7734368.
(validationData[0, 0],    validationData[0, 1]): Input: -0.36690253, 0.15931901 - Predicted: 0, Predicted Value: 0.45671865, Probability: 0.4567
(validationData[1, 0],    validationData[1, 1]): Input: -0.3678846, 0.15956786 - Predicted: 0, Predicted Value: 0.45635784, Probability: 0.4564
(validationData[2, 0],    validationData[2, 1]): Input: -0.36678034, 0.16454844 - Predicted: 0, Predicted Value: 0.4559822, Probability: 0.4560
(validationData[3, 0],    validationData[3, 1]): Input: -0.37094417, 0.15751004 - Predicted: 0, Predicted Value: 0.45570716, Probability: 0.4557
(validationData[4, 0],    validationData[4, 1]): Input: -0.3709749, 0.15998013 - Predicted: 0, Predicted Value: 0.45546448, Probability: 0.4555
(validationData[5, 0],    validationData[5, 1]): Input: -0.36195132, 0.1817037 - Predicted: 0, Predicted Value: 0.45525485, Probability: 0.4553
(validationData[6, 0],    validationData[6, 1]): Input: -0.3701536, 0.16680019 - Predicted: 0, Predicted Value: 0.45513403, Probability: 0.4551
(validationData[7, 0],    validationData[7, 1]): Input: -0.35700575, 0.1954377 - Predicted: 0, Predicted Value: 0.45504642, Probability: 0.4550
(validationData[8, 0],    validationData[8, 1]): Input: -0.36992568, 0.17210168 - Predicted: 0, Predicted Value: 0.45488524, Probability: 0.4549
(validationData[9, 0],    validationData[9, 1]): Input: -0.34629798, 0.21762058 - Predicted: 0, Predicted Value: 0.45478538, Probability: 0.4548
(validationData[10, 0],    validationData[10, 1]): Input: -0.35701165, 0.20160028 - Predicted: 0, Predicted Value: 0.4548786, Probability: 0.4549
(validationData[11, 0],    validationData[11, 1]): Input: -0.3433222, 0.22594443 - Predicted: 0, Predicted Value: 0.45484903, Probability: 0.4548
(validationData[12, 0],    validationData[12, 1]): Input: -0.36767977, 0.18589133 - Predicted: 0, Predicted Value: 0.4549137, Probability: 0.4549
(validationData[13, 0],    validationData[13, 1]): Input: -0.33850214, 0.23661213 - Predicted: 0, Predicted Value: 0.45468417, Probability: 0.4547
Epoch 1/100: Validation Loss = 1.1200886
Epoch 2/100: Validation Loss = 1.1441891
Epoch 3/100: Validation Loss = 1.1664108
Epoch 4/100: Validation Loss = 1.1893783
Epoch 5/100: Validation Loss = 1.2100134
Epoch 6/100: Validation Loss = 1.2290684
Epoch 7/100: Validation Loss = 1.2475654
Epoch 8/100: Validation Loss = 1.2681175
Epoch 9/100: Validation Loss = 1.2874182
Epoch 10/100: Validation Loss = 1.3064436