KRBPN 2.0.6

KRBPN 2.0.6

测试经过测试
语言语言 Obj-CObjective C
许可证 MIT
发布时间上次发布2015 年 10 月

Kalvar Lin 维护。



KRBPN 2.0.6

ios-BPN-NeuralNetwork

本项目实现了三个层次(输入层、隐藏层和输出层)神经网络(ニューラルネットワーク),并实现了反向传播神经网络(BPN),QuickProp 理论和 Kecman 的理论(EDBD)。KRBPN 可以用于产品推荐(おすすめの商品)、用户行为分析(ユーザーの行動分析)、数据挖掘(データマイニング)和数据分析(データ分析)。

本项目旨在快速为移动设备进行基本数据分析。如果您需要了解如何使用这个网络,请通过电子邮件向我咨询。

Podfile

platform :ios, '7.0'
pod "KRBPN", "~> 2.0.6"

如何使用

导入 & 创建
#import "KRBPN.h"
通用设置
// Use singleton or [[KRBPN alloc] init]
_krBPN = [KRBPN sharedNetwork];

// Learning rate
_krBPN.learningRate     = 0.8f;

// Convergence error, 收斂誤差值 ( Normally is 10^-3 or 10^-6 )
_krBPN.convergenceError = 0.001f;

// Limit iterations
_krBPN.limitIteration   = 1000;

// If you wanna up to your wishing to decide that initital hidden-net counts, you could use this : 
_krBPN.hiddenNets       = 5;

// If you wanna use enhanced theory, and to setup that customized learning rate, you could use this as below :
_krBPN.quickPropFixedRate = 0.5f;

// If you wanna directly use QuickProp method to enhance that network training :
_krBPN.learningMode = KRBPNLearningModeByQuickPropDynamic;

// If you wanna use Kecman's theory to enhance that network training, please use this :
_krBPN.learningMode = KRBPNLearningModeByQuickPropFixed;

// If you wanna use Fahlman's QuickProp combined Kecman's Fixed Rate. Yes, in my experiences this usage method is better than others.
_krBPN.learningMode = KRBPNLearningModeByQuickPropSmartHybrid;

// Per iteration-training block
[_krBPN setEachIteration:^(NSInteger times, NSDictionary *trainedInfo){
    NSLog(@"Iterations : %i", times);
    NSLog(@"Result per Iteration : %@", [trainedInfo objectForKey:KRBPNTrainedOutputResults]);
}];
示例 1

使用 Sigmoid 激活函数并自定义所有权重与偏差。

// Sigmoid is that values range in [0.0, 1.0] and the outputs too.
_krBPN.activeFunction = KRBPNActivationBySigmoid;

// Add the 3 patterns, the weights connect with hidden layer, the output targets
[_krBPN addPatterns:@[@1, @0.1, @0.5, @0.2] outputGoals:@[@0.7f, @0.8f]];
[_krBPN addPatterns:@[@0, @1, @0.3, @0.9] outputGoals:@[@0.1f, @0.1f]];  
[_krBPN addPatterns:@[@1, @0.3, @0.1, @0.4] outputGoals:@[@0.95f, @0.9f]];

// Add input to hidden layer that pattern-weights
// Here is 2 hidden nets
[_krBPN addPatternWeights:@[@0.2, @-0.3]];
[_krBPN addPatternWeights:@[@0.4, @0.1]];  
[_krBPN addPatternWeights:@[@-0.5, @0.2]]; 
[_krBPN addPatternWeights:@[@-0.1, @0.3]]; 

// Add hidden layer biases of nets and the output weights
[_krBPN addHiddenLayerNetBias:-0.4f outputWeights:@[@-0.3f, @0.2f]]; 
[_krBPN addHiddenLayerNetBias:0.2f outputWeights:@[@-0.2f, @0.5f]]; 

// Add 2 output nets biases
[_krBPN addOutputBiases:@[@0.0f, @0.1f]];

__block typeof(_krBPN) _weakKrBPN = _krBPN;
[_krBPN setTrainingCompletion:^(BOOL success, NSDictionary *trainedInfo, NSInteger totalTimes){
    if( success )
    {
        NSLog(@"Training done with total times : %i", totalTimes);
        NSLog(@"TrainedInfo : %@", trainedInfo);

        //Start in checking the network is correctly trained.
        NSLog(@"======== Start in Verification ========");

        [_weakKrBPN setTrainingCompletion:^(BOOL success, NSDictionary *trainedInfo, NSInteger totalTimes){
            NSLog(@"Training done with total times : %i", totalTimes);
            NSLog(@"Verified TrainedInfo : %@", trainedInfo);
        }];

        [_weakKrBPN recoverNetwork];
        [_weakKrBPN directOutputAtInputs:@[@1, @0.1, @0.5, @0.2]];
    }
}];

[_krBPN training];
示例 2

此示例使用有界于 [-1.0, 1.0] 的 tanh() 作为激活函数,并直接随机所有设置(权重、偏差、网络计算)。

_krBPN.activeFunction = KRBPNActivationByTanh;

[_krBPN addPatterns:@[@1, @0.1, @0.5, @0.2] outputGoals:@[@0.7f]];    
[_krBPN addPatterns:@[@-0.5, @0.8, @-0.3, @0.9] outputGoals:@[@-0.1f]]; 
[_krBPN addPatterns:@[@1, @0.3, @0.1, @0.4] outputGoals:@[@0.9f]];     

__block typeof(_krBPN) _weakKrBPN = _krBPN;
[_krBPN setTrainingCompletion:^(BOOL success, NSDictionary *trainedInfo, NSInteger totalTimes){
    if( success )
    {
        NSLog(@"Training done with total times : %i", totalTimes);
        NSLog(@"TrainedInfo : %@", trainedInfo);

        //Start in checking the network is correctly trained.
        NSLog(@"======== Start in Verification ========");

        [_weakKrBPN setTrainingCompletion:^(BOOL success, NSDictionary *trainedInfo, NSInteger totalTimes){
            NSLog(@"Training done with total times : %i", totalTimes);
            NSLog(@"Verified TrainedInfo : %@", trainedInfo);
        }];

        [_weakKrBPN recoverNetwork];
        [_weakKrBPN directOutputAtInputs:@[@-0.5, @0.8, @-0.3, @0.9]];
    }
}];

[_krBPN trainingByRandomSettings];
示例 3

识别数字 0 到 9,此训练有 10 个输出(与 10 个分类组相同)。数字 1 为 [1, 0, 0, 0, ..., 0],数字 3 为 [0, 0, 1, 0, ..., 0]。

_krBPN.activeFunction = KRBPNActivationBySigmoid;

// Number 1
[_krBPN addPatterns:@[@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]
        outputGoals:@[@1, @0, @0, @0, @0, @0, @0, @0, @0, @0]];

// Number 2
[_krBPN addPatterns:@[@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]
        outputGoals:@[@0, @1, @0, @0, @0, @0, @0, @0, @0, @0]];

// Number 3
[_krBPN addPatterns:@[@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]
        outputGoals:@[@0, @0, @1, @0, @0, @0, @0, @0, @0, @0]];

// Number 4
[_krBPN addPatterns:@[@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]
        outputGoals:@[@0, @0, @0, @1, @0, @0, @0, @0, @0, @0]];

// Number 5
[_krBPN addPatterns:@[@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]
        outputGoals:@[@0, @0, @0, @0, @1, @0, @0, @0, @0, @0]];

// Number 6
[_krBPN addPatterns:@[@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]
        outputGoals:@[@0, @0, @0, @0, @0, @1, @0, @0, @0, @0]];

// Number 7
[_krBPN addPatterns:@[@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]
        outputGoals:@[@0, @0, @0, @0, @0, @0, @1, @0, @0, @0]];

// Number 8
[_krBPN addPatterns:@[@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]
        outputGoals:@[@0, @0, @0, @0, @0, @0, @0, @1, @0, @0]];

// Number 9
[_krBPN addPatterns:@[@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]
        outputGoals:@[@0, @0, @0, @0, @0, @0, @0, @0, @1, @0]];

// Number 0
[_krBPN addPatterns:@[@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]
        outputGoals:@[@0, @0, @0, @0, @0, @0, @0, @0, @0, @1]];

__block typeof(_krBPN) _weakKrBPN = _krBPN;
[_krBPN setTrainingCompletion:^(BOOL success, NSDictionary *trainedInfo, NSInteger totalTimes){
    if( success )
    {
        NSLog(@"Training done with total times : %i", totalTimes);
        NSLog(@"TrainedInfo : %@", trainedInfo);

        //Start in checking the network is correctly trained.
        NSLog(@"======== Start in Verification ========");

        [_weakKrBPN setTrainingCompletion:^(BOOL success, NSDictionary *trainedInfo, NSInteger totalTimes){
            NSLog(@"Training done with total times : %i", totalTimes);
            NSLog(@"Verified TrainedInfo : %@", trainedInfo);
        }];

        [_weakKrBPN recoverNetwork];

        //Verified number " 7 ", and it has some defects.
        [_weakKrBPN directOutputAtInputs:@[@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]];

    }
}];

[_krBPN trainingByRandomSettings];
示例 4

预测癌症医疗的存活率。

_krBPN.activeFunction = KRBPNActivationBySigmoid;

// ER, PR, Hev-2, K, 治療與否, 每 10 年為 0.1 單位 => 存活率
[_krBPN addPatterns:@[@0.9, @0.9, @0.0, @0.15, @0, @0.1f] outputGoals:@[@0.12f]];
[_krBPN addPatterns:@[@0.9, @0.9, @0.0, @0.15, @1, @0.1f] outputGoals:@[@0.12f]];

[_krBPN addPatterns:@[@0.5, @0.4, @0.0, @0.25, @0, @0.1f] outputGoals:@[@0.23f]];
[_krBPN addPatterns:@[@0.5, @0.4, @0.0, @0.25, @1, @0.1f] outputGoals:@[@0.14f]];

[_krBPN addPatterns:@[@0.0, @0.0, @0.0, @0.3, @0, @0.1f] outputGoals:@[@0.35f]];
[_krBPN addPatterns:@[@0.0, @0.0, @0.0, @0.3, @1, @0.1f] outputGoals:@[@0.22f]];

[_krBPN addPatterns:@[@0.0, @0.0, @1.0, @0.3, @0, @0.1f] outputGoals:@[@0.3f]];
[_krBPN addPatterns:@[@0.0, @0.0, @1.0, @0.3, @1, @0.1f] outputGoals:@[@0.12f]];

__block typeof(_krBPN) _weakKrBPN = _krBPN
[_krBPN setTrainingCompletion:^(BOOL success, NSDictionary *trainedInfo, NSInteger totalTimes){
    if( success )
    {
        NSLog(@"Training done with total times : %i", totalTimes);
        NSLog(@"TrainedInfo : %@", trainedInfo);

        //Start in checking the network is correctly trained.
        NSLog(@"======== Start in Verification ========");

        [_weakKrBPN setTrainingCompletion:^(BOOL success, NSDictionary *trainedInfo, NSInteger totalTimes){
            NSLog(@"Training done with total times : %i", totalTimes);
            NSLog(@"Verified TrainedInfo : %@", trainedInfo);
        }];

        [_weakKrBPN recoverNetwork];

        // To predict how many years could be alive with the patients.
        [_weakKrBPN directOutputAtInputs:@[@0.6, @0.6, @0.0, @0.25, @0.0, @0.1f]];
        [_weakKrBPN directOutputAtInputs:@[@0.6, @0.6, @0.0, @0.25, @1.0, @0.1f]];
    }
}];

[_krBPN trainingByRandomSettings];

版本

V2.0.6

许可证

MIT。