2017-04-22 333 views
1

我在Accord.net中使用SVM進行時間序列建模。我用可用的數據訓練一次(比如5000)。之後,我每秒都會收到新數據,我想每秒鐘使用一個數據以增量方式更新我的SVM機器,這可能嗎?accord.net svm增量訓練

回答

1

儘管現在可以在Accord.NET中逐步學習SVM,但不幸的是,該功能僅適用於線性機器。由於您正在處理時間序列,因此我假設您正在使用Dynamic Time Warping內核來處理內核SVM。

如果您能夠從序列中提取固定長度的特徵,那麼如果將這些特徵提供給線性機器並使用隨機漸變下降或Averaged Stochastic Gradient Descent進行訓練,您可能會問如下所示:

// In this example, we will learn a multi-class SVM using the one-vs-one (OvO) 
// approach. The OvO approacbh can decompose decision problems involving multiple 
// classes into a series of binary ones, which can then be solved using SVMs. 

// Ensure we have reproducible results 
Accord.Math.Random.Generator.Seed = 0; 

// We will try to learn a classifier 
// for the Fisher Iris Flower dataset 
var iris = new Iris(); 
double[][] inputs = iris.Instances; // get the flower characteristics 
int[] outputs = iris.ClassLabels; // get the expected flower classes 

// We will use mini-batches of size 32 to learn a SVM using SGD 
var batches = MiniBatches.Create(batchSize: 32, maxIterations: 1000, 
    shuffle: ShuffleMethod.EveryEpoch, input: inputs, output: outputs); 

// Now, we can create a multi-class teaching algorithm for the SVMs 
var teacher = new MulticlassSupportVectorLearning<Linear, double[]> 
{ 
    // We will use SGD to learn each of the binary problems in the multi-class problem 
    Learner = (p) => new StochasticGradientDescent<Linear, double[], LogisticLoss>() 
    { 
     LearningRate = 1e-3, 
     MaxIterations = 1 // so the gradient is only updated once after each mini-batch 
    } 
}; 

// The following line is only needed to ensure reproducible results. Please remove it to enable full parallelization 
teacher.ParallelOptions.MaxDegreeOfParallelism = 1; // (Remove, comment, or change this line to enable full parallelism) 

// Now, we can start training the model on mini-batches: 
foreach (var batch in batches) 
{ 
    teacher.Learn(batch.Inputs, batch.Outputs); 
} 

// Get the final model: 
var svm = teacher.Model; 

// Now, we should be able to use the model to predict 
// the classes of all flowers in Fisher's Iris dataset: 
int[] prediction = svm.Decide(inputs); 

// And from those predictions, we can compute the model accuracy: 
var cm = new GeneralConfusionMatrix(expected: outputs, predicted: prediction); 
double accuracy = cm.Accuracy; // should be approximately 0.973