本项目中的机器学习(マシンラーニング)实现了多层感知器神经网络(MLP)(ニューラルネットワーク)和反向传播神经网络(BPN)。它设计了无限隐藏层来进行训练任务。此网络可用于产品推荐(おすすめの商品)、用户行为分析(ユーザーの行動分析)、数据挖掘(データマイニング)和数据分析(データ分析)。
platform :ios, '9.0'
pod "KRMLP", "~> 2.2.3"
#import "KRMLP.h"
使用 KRMLPPattern 创建训练模式。
KRMLPPattern *pattern = [[KRMLPPattern alloc] initWithFeatures:@[@1, @0, @0.5f] targets:@[@0, @1]];
例如,以下训练样本用于识别 0 到 9 的数字,它将首先创建模式特征(4 x 9)。
NSArray *features = @[// 0
@[@1, @1, @1, @1,
@1, @1, @1, @1,
@1, @1, @0, @0,
@0, @0, @0, @0,
@0, @1, @1, @0,
@0, @0, @0, @0,
@0, @0, @1, @1,
@1, @1, @1, @1,
@1, @1, @1, @1],
// 1
@[@0, @0, @0, @0,
@0, @0, @0, @0,
@0, @0, @0, @0,
@0, @0, @0, @0,
@0, @0, @0, @0,
@0, @0, @0, @0,
@0, @0, @0, @1,
@1, @1, @1, @1,
@1, @1, @1, @1],
// 2
@[@1, @0, @0, @0,
@1, @1, @1, @1,
@1, @1, @0, @0,
@0, @1, @0, @0,
@0, @1, @1, @0,
@0, @0, @1, @0,
@0, @0, @1, @1,
@1, @1, @1, @1,
@0, @0, @0, @1],
// 3
@[@1, @0, @0, @0,
@1, @0, @0, @0,
@1, @1, @0, @0,
@0, @1, @0, @0,
@0, @1, @1, @0,
@0, @0, @1, @0,
@0, @0, @1, @1,
@1, @1, @1, @1,
@1, @1, @1, @1],
// 4
@[@1, @1, @1, @1,
@1, @0, @0, @0,
@0, @0, @0, @0,
@0, @1, @0, @0,
@0, @0, @0, @0,
@0, @0, @1, @0,
@0, @0, @0, @1,
@1, @1, @1, @1,
@1, @1, @1, @1],
// 5
@[@1, @1, @1, @1,
@1, @0, @0, @0,
@1, @1, @0, @0,
@0, @1, @0, @0,
@0, @1, @1, @0,
@0, @0, @1, @0,
@0, @0, @1, @1,
@0, @0, @0, @1,
@1, @1, @1, @1],
// 6
@[@1, @1, @1, @1,
@1, @1, @1, @1,
@1, @1, @0, @0,
@0, @1, @0, @0,
@0, @1, @1, @0,
@0, @0, @1, @0,
@0, @0, @1, @1,
@0, @0, @0, @1,
@1, @1, @1, @1],
// 7
@[@1, @0, @0, @0,
@0, @0, @0, @0,
@0, @1, @0, @0,
@0, @0, @0, @0,
@0, @0, @1, @0,
@0, @0, @0, @0,
@0, @0, @0, @1,
@1, @1, @1, @1,
@1, @1, @1, @1],
// 8
@[@1, @1, @1, @1,
@1, @1, @1, @1,
@1, @1, @0, @0,
@0, @1, @0, @0,
@0, @1, @1, @0,
@0, @0, @1, @0,
@0, @0, @1, @1,
@1, @1, @1, @1,
@1, @1, @1, @1],
// 9
@[@1, @1, @1, @1,
@1, @0, @0, @0,
@0, @1, @0, @0,
@0, @1, @0, @0,
@0, @0, @1, @0,
@0, @0, @1, @0,
@0, @0, @0, @1,
@1, @1, @1, @1,
@1, @1, @1, @1]
];
第二步,定义 10 个目标(输出)以映射特征。
NSArray *targets = @[// 0
@[@1, @0, @0, @0, @0, @0, @0, @0, @0, @0],
// 1
@[@0, @1, @0, @0, @0, @0, @0, @0, @0, @0],
// 2
@[@0, @0, @1, @0, @0, @0, @0, @0, @0, @0],
// 3
@[@0, @0, @0, @1, @0, @0, @0, @0, @0, @0],
// 4
@[@0, @0, @0, @0, @1, @0, @0, @0, @0, @0],
// 5
@[@0, @0, @0, @0, @0, @1, @0, @0, @0, @0],
// 6
@[@0, @0, @0, @0, @0, @0, @1, @0, @0, @0],
// 7
@[@0, @0, @0, @0, @0, @0, @0, @1, @0, @0],
// 8
@[@0, @0, @0, @0, @0, @0, @0, @0, @1, @0],
// 9
@[@0, @0, @0, @0, @0, @0, @0, @0, @0, @1],
];
第三步,创建模式数组。
NSMutableArray <KRMLPPattern *> *patterns = [NSMutableArray new];
[features enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
KRMLPPattern *pattern = [[KRMLPPattern alloc] initWithFeatures:obj targets:[targets objectAtIndex:idx]];
[patterns addObject:pattern];
}];
正常情况
KRMLP *mlp = [[KRMLP alloc] init];
mlp.maxIteration = 300;
mlp.convergenceError = 0.001f;
mlp.networkActivation = KRMLPNetActivationSigmoid;
mlp.initialMaxWeight = 0.5f;
mlp.initialMinWeight = -0.5f;
[mlp addPatternsFromArray:patterns];
[mlp setupOptimizationMethod:KRMLPOptimizationFixedInertia inertialRate:0.7f];
KRMLPHiddenLayer *hiddenLayer1 = [mlp createHiddenLayerWithAutomaticSetting]; // or [mlp createHiddenLayerWithNetCount:18 inputCount:36];
[mlp addHiddenLayer:hiddenLayer1];
KRMLPHiddenLayer *hiddenLayer2 = [mlp createHiddenLayerWithAutomaticSetting]; // or [mlp createHiddenLayerDependsOnHiddenLayer:hiddenLayer1 netCount:18];
[mlp addHiddenLayer:hiddenLayer2];
KRMLPHiddenLayer *hiddenLayer3 = [mlp createHiddenLayerWithAutomaticSetting]; // or [mlp createHiddenLayerDependsOnHiddenLayer:hiddenLayer2 netCount:16];
[mlp addHiddenLayer:hiddenLayer3];
[mlp setupOutputLayer];
[mlp trainingWithCompletion:^(KRMLP *network) {
[network saveForKey:@"A1"];
// Verifying #7 (the number is something wrong)
NSArray *inputs = @[@1, @1, @1, @0,
@0, @0, @0, @0,
@0, @1, @0, @0,
@0, @0, @0, @0,
@0, @0, @1, @0,
@0, @0, @0, @0,
@0, @0, @0, @1,
@1, @1, @1, @1,
@1, @1, @1, @1];
[network predicateWithFeatures:inputs completion:^(KRMLPNetworkOutput *networkOutput) {
[networkOutput.results enumerateObjectsUsingBlock:^(KRMLPResult * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"Predicated the number [%li] is possible %@%%", obj.outputIndex, obj.probability);
}];
}];
} iteration:^(KRMLP *network) {
NSLog(@"Iteration %li", network.iteration);
} training:^(KRMLPTrainingOutput *trainingOutput) {
NSLog(@"Training outputs of pattern[%li] : %@", trainingOutput.patternIndex, trainingOutput.outputs);
}];
要恢复已保存的网络,直接使用 [mlp recoverForKey:] 调用隐藏层、输出层、网络激活和学习率。因此,您仍然可以设置 KRMLP 的其他参数来训练新网络或直接进行预测。
例如,进行连续训练。
KRMLP *mlp = [[KRMLP alloc] init];
[mlp recoverForKey:@"A1"];
mlp.maxIteration = 50;
mlp.convergenceError = 0.001f;
[mlp addPatternsFromArray:patterns];
[mlp trainingWithCompletion:^(KRMLP *network) {
// If you want, to save to another record.
[network saveForKey:@"B2"];
// Do something since training finished.
// ...
} iteration:^(KRMLP *network) {
//NSLog(@"Iteration %li", network.iteration);
} training:^(KRMLPTrainingOutput *trainingOutput) {
//NSLog(@"Training outputs of pattern[%li] : %@", trainingOutput.patternIndex, trainingOutput.outputs);
}];
如果您想更改任何已回忆的参数/设置,可以在调用 [mlp recoverForKey:] 方法之后设置它。
KRMLP *mlp = [[KRMLP alloc] init];
[mlp recoverForKey:@"A1"];
mlp.maxIteration = 50;
mlp.convergenceError = 0.001f;
mlp.networkActivation = KRMLPNetActivationSigmoid;
[mlp setupOptimizationMethod:KRMLPOptimizationQuickProp]; // Changes from KRMLPOptimizationFixedInertia to KRMLPOptimizationQuickProp.
// ...
固定惯性率
[mlp setupOptimizationMethod:KRMLPOptimizationFixedInertia inertialRate:0.7f];
QuickProp
[mlp setupOptimizationMethod:KRMLPOptimizationQuickProp];
V2.2.3
MIT.