KRSVM 实现了机器学习的 SVM。
platform :ios, '7.0'
pod "KRSVM", "~> 1.0.0"
#import "KRSVM.h"
KRSMO *smo = [[KRSVM sharedSVM] useSMO];
[smo.kernel useLinear];
sigma 可以根据您的意愿进行定制,默认值为 2.0,但有些论文提到 0.5、1.0、3.0、5.0 都可以用于训练。无论如何,请尝试,别无他法。
[smo.kernel useRBF];
smo.kernel.sigma = 2.0f;
切线的 alpha 可以根据您的意愿进行定制,默认值是 1.0,但在某些情况下,使用 2.0 可能更好,在此示例中,我们使用了 0.8 来进行回归。
[smo.kernel useTangent];
smo.kernel.alpha = 0.8f;
smo.toleranceError = 0.001f;
smo.maxIteration = 1000;
smo.constValue = 1;
[smo.kernel useLinear];
[smo addPatterns:@[@0.0f, @0.0f] target:-1.0f]; // x1
[smo addPatterns:@[@2.0f, @2.0f] target:-1.0f]; // x2
[smo addPatterns:@[@2.0f, @0.0f] target:1.0f]; // x3
[smo addPatterns:@[@3.0f, @0.0f] target:1.0f]; // x4
// One bias likes a net of neural network
[smo addBias:@0.0f];
// One input value by one weight that likes inputs & weights of neural network
[smo addWeights:@[@0.0f, @0.0f]];
// Setup the groups of classification and the target-value of group
[smo addGroupOfTarget:-1.0f];
[smo addGroupOfTarget:1.0f];
[smo classifyWithPerIteration:^(NSInteger iteration, NSArray *weights, NSArray *biases) {
//NSLog(@"%li Iteration weights : %@", iteration, weights);
//NSLog(@"%li Iteration biases : %@", iteration, biases);
} completion:^(BOOL success, NSArray *weights, NSArray *biases, NSDictionary *groups, NSInteger totalIterations) {
NSLog(@"===============================================");
NSLog(@"%li Completion weights : %@", totalIterations, weights);
NSLog(@"%li Completion biases : %@", totalIterations, biases);
NSLog(@"%li Completion groups : %@", totalIterations, groups);
NSLog(@"===============================================");
// Verify & Directly Output
[smo classifyPatterns:@[@[@2.0f, @2.0f], @[@3.0f, @0.0f]] completion:^(NSArray *weights, NSArray *biases, NSArray *results, NSDictionary *allGroups) {
for( KRSVMPattern *pattern in results )
{
NSLog(@"direct classify to target %@", pattern.classifiedTarget);
}
NSLog(@"all groups : %@", allGroups);
}];
}];
V1.0.0
MIT。