SGDClassifier<T>

Models & Types · AiDotNet.Classification.Linear

Stochastic Gradient Descent classifier for large-scale learning.

Inheritance: ClassifierBase<T> → ProbabilisticClassifierBase<T> → LinearClassifierBase<T> → SGDClassifier<T>

For Beginners

Instead of computing gradients over the entire dataset, SGD:

  1. Picks one training sample
  2. Computes how wrong the prediction is
  3. Updates weights to reduce that error
  4. Repeats for all samples (one epoch)
  5. Repeats for multiple epochs

Benefits:

  • Very fast for large datasets
  • Can handle streaming data
  • Often finds good solutions quickly

Trade-offs:

  • Noisy updates (not always improving)
  • Requires tuning learning rate
  • May oscillate near optimal solution

How It Works

SGD is an optimization technique that updates weights using one sample at a time. This makes it very efficient for large datasets that don’t fit in memory.

Example

// Create SGD classifier for large-scale online learning
var options = new LinearClassifierOptions<double>();
            
// Prepare training data
var features = new Matrix<double>(new double[,] { { 1.0, 1.1 }, { 1.2, 0.9 }, { 0.8, 1.0 }, { 5.0, 5.1 }, { 5.2, 4.9 }, { 4.8, 5.0 } });
var labels = new Vector<double>(new double[] { 0, 0, 0, 1, 1, 1 });
            
// Train by updating weights one sample at a time
var result = new AiModelBuilder<double, Matrix<double>, Vector<double>>()
    .ConfigureModel(new SGDClassifier<double>(options))
    .Build(features, labels);
            
// Predict using learned linear decision boundary
var newSample = new Matrix<double>(new double[,] { { 1.1, 1.0 } });
var prediction = result.Predict(newSample);
// prediction[0] contains the predicted class

Constructors

Constructor Summary
SGDClassifier(LinearClassifierOptions<T> options, IRegularization<T, Matrix<T>, Vector<T>> regularization) Initializes a new instance of the SGDClassifier class.

Methods

Method Summary
void Train(Matrix<T> x, Vector<T> y) Returns the model type identifier for this classifier.