定制 cylab-be/wowa-training 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

cylab-be/wowa-training

Composer 安装命令:

composer require cylab-be/wowa-training

包简介

README 文档

README

pipeline status coverage report

The WOWA operator (Torra) is a powerfull aggregation operator that allows to combine multiple input values into a single score. This is particulary interesting for detection and ranking systems that rely on multiple heuristics. The system can use WOWA to produce a single meaningfull score.

A PHP implementation of WOWA is available at https://github.com/tdebatty/php-aggregation-operators

The WOWA operator requires two sets of parameters: p weights and w weights. In this project we use a genetic algorithm to compute the best values for p and w weights. For the training, the algorithm uses a dataset of input vectors together with the expected aggregated score of each vector.

Installation

composer require cylab-be/wowa-training

Usage

Example

require __DIR__ . "/vendor/autoload.php"

use RUCD\Training\Trainer;
use RUCD\Training\TrainerParameters;

use Monolog\Logger;
use Monolog\Handler\StreamHandler;

$populationSize = 100;
$crossoverRate = 60;
$mutationRate = 3;
$selectionMethod = TrainerParemeters::SELECTION_METHOD_RWS;
$maxGeneration = 100;
$populationInitializationMethod = TrainerParameters::INITIAL_POPULATION_GENERATION_RANDOM;

// For logging you can use any implementation of PSR Logger
$logger = new Logger('wowa-training-test');
$logger->pushHandler(new StreamHandler('php://stdout', Logger::DEBUG));

$parameters = new TrainerParameters(
    $logger, $populationSize, $crossoverRate, $mutationRate, $selectionMethod, $maxGeneration, $populationInitilizationMethod);
$trainer = new Trainer($parameters);

// Input data
$data = [
  [0.1, 0.2, 0.3, 0.4],
  [0.1, 0.8, 0.3, 0.4],
  [0.2, 0.6, 0.3, 0.4],
  [0.1, 0.2, 0.5, 0.8],
  [0.5, 0.1, 0.2, 0.3],
  [0.1, 0.1, 0.1, 0.1],
];

// expected aggregated value for each data vector
$expected = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6];

var_dump($trainer->run($data, $expected));

The example above will produce something like:

class RUCD\Training\Solution#56 (3) {
  public $weights_w =>
  array(4) {
    [0] =>
    double(0.31568310640557)
    [1] =>
    double(0.37517587135019)
    [2] =>
    double(0.23165073663557)
    [3] =>
    double(0.077490285608666)
  }
  public $weights_p =>
  array(4) {
    [0] =>
    double(0.67852325915809)
    [1] =>
    double(0.0083157109614166)
    [2] =>
    double(0.082353710617992)
    [3] =>
    double(0.2308073192625)
  }
  public $distance =>
  double(0.51636277259465)
}

The run method returns a solution object, consisting of p weights and w weights to use with the WOWA operator, plus the total distance between the expected aggregated values that were given as parameter, and the aggregated values computed by WOWA using these weights.

Parameters description

  • populationSize : size of the population in the algorithm. Suggested value : 100
  • crossoverRate : defines the percentage of population generated by crossover. Must be between 1 and 100. Suggested value : 60
  • mutationRate : define the number of random element change in the population. Must be between 1 and 100. Suggested value : 15
  • selectionMethod : Determine the method used to select element in the population (for generate the next generation). SELECTION_METHOD_RWS for Roulette Wheel Selection and SELECTION_METHOD_TOS for Tournament Selection.
  • maxGeneration : Determine the maximum number of iteration of the algorithm.
  • populationInitilizationMethod: Determine the method used to generate the initial population. INITIAL_POPULATION_GENERATION_RANDOM for a random generation, INITIAL_POPULATION_GENERATION_QUASI_RANDOM for a "quasi"-random generation.
  • solutionType: Specify the class of the Solution object. The solution objects must extend the SolutionAbstract class. The class of Solution object defines the criterion used to evaluate the performance of individuals in the population. SolutionDistance impmements a distance criterion while SolutionAUC uses a criterion based on the Area Under the Curve (AUC) computation. Note that SolutionAUC is designed for binary classification, the expected vector can only contains 0 or 1 values.

Cross validation

Example

require __DIR__ . "/vendor/autoload.php";

use RUCD\Training\Trainer;
use RUCD\Training\TrainerParameters;
use RUCD\Training\SolutionDistance;
use RUCD\Training\SolutionAUC;

use Monolog\Logger;
use Monolog\Handler\StreamHandler;

$populationSize = 100;
$crossoverRate = 60;
$mutationRate = 3;
$selectionMethod = TrainerParameters::SELECTION_METHOD_RWS;
$maxGeneration = 100;
$populationInitializationMethod = TrainerParameters::INITIAL_POPULATION_GENERATION_RANDOM;
$solutionType = new SolutionDistance();

// For logging you can use any implementation of PSR Logger
$logger = new Logger('wowa-training-test');
$logger->pushHandler(new StreamHandler('php://stdout', Logger::WARNING));

$parameters = new TrainerParameters(
    $logger, $populationSize, $crossoverRate, $mutationRate, $selectionMethod, $maxGeneration, $populationInitializationMethod);
$trainer = new Trainer($parameters, $solutionType);

// Input data
$data = [
  [0.1, 0.2, 0.3, 0.4],
  [0.1, 0.8, 0.3, 0.4],
  [0.2, 0.6, 0.3, 0.4],
  [0.1, 0.2, 0.5, 0.8],
  [0.5, 0.1, 0.2, 0.3],
  [0.1, 0.1, 0.1, 0.1],
  [0.1, 0.2, 0.3, 0.4],
  [0.1, 0.8, 0.3, 0.4],
  [0.2, 0.6, 0.3, 0.4],
  [0.1, 0.2, 0.5, 0.8],
  [0.5, 0.1, 0.2, 0.3],
  [0.1, 0.1, 0.1, 0.1],
];

// expected aggregated value for each data vector
$expected = [1,0,0,1,0,1,0,0,0,1,0,0];

var_dump($trainer->runKFold($data, $expected, 3));

The method runKFold runs a k folds cross-validation. Concretely, it separates the dataset in k folds. For each folds, a single fold is retained as the validation data for testing the model, and the remaining k − 1 folds are used as training data. The cross-validation process is then repeated k times, with each of the k folds used exactly once as the validation data. The k results can then be averaged to produce a single estimation. For each tested fold, the Area Under the Curve is also computed to evaluate the classification efficiency (works only expected vector that contains 0 and 1).

As output, the method generates an array that contains the w and p vectors and the AUC value for each fold.

The example above produces result similar to:

array(3) {
  [0]=>
  array(2) {
    ["auc"]=>
    float(0.5)
    ["solution"]=>
    object(RUCD\Training\SolutionDistance)#133 (3) {
      ["weights_w"]=>
      array(4) {
        [0]=>
        float(0.16573697533351)
        [1]=>
        float(0.76165292950897)
        [2]=>
        float(0.024253730247718)
        [3]=>
        float(0.048356364909798)
      }
      ["weights_p"]=>
      array(4) {
        [0]=>
        float(0.20097150002833)
        [1]=>
        float(0.020364990979043)
        [2]=>
        float(0.17636230606784)
        [3]=>
        float(0.60230120292479)
      }
      ["distance"]=>
      float(1.7892117370011)
    }
  }
  [1]=>
  array(2) {
    ["auc"]=>
    float(0)
    ["solution"]=>
    object(RUCD\Training\SolutionDistance)#146 (3) {
      ["weights_w"]=>
      array(4) {
        [0]=>
        float(0.18742088232865)
        [1]=>
        float(0.57233147854378)
        [2]=>
        float(0.22507083815429)
        [3]=>
        float(0.015176800973267)
      }
      ["weights_p"]=>
      array(4) {
        [0]=>
        float(0.076670559592882)
        [1]=>
        float(0.019193144442706)
        [2]=>
        float(0.18316950831007)
        [3]=>
        float(0.72096678765435)
      }
      ["distance"]=>
      float(1.3403524893715)
    }
  }
  [2]=>
  array(2) {
    ["auc"]=>
    float(1)
    ["solution"]=>
    object(RUCD\Training\SolutionDistance)#12 (3) {
      ["weights_w"]=>
      array(4) {
        [0]=>
        float(0.16274887804484)
        [1]=>
        float(0.527446888854)
        [2]=>
        float(0.21225455965351)
        [3]=>
        float(0.097549673447646)
      }
      ["weights_p"]=>
      array(4) {
        [0]=>
        float(0.10891441031576)
        [1]=>
        float(0.023649196569852)
        [2]=>
        float(0.24106562811561)
        [3]=>
        float(0.62637076499877)
      }
      ["distance"]=>
      float(2.0314776184856)
    }
  }
}

References

cylab-be/wowa-training 适用场景与选型建议

cylab-be/wowa-training 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 805 次下载、GitHub Stars 达 0, 最近一次更新时间为 2018 年 09 月 14 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 cylab-be/wowa-training 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 cylab-be/wowa-training 我们能提供哪些服务?
定制开发 / 二次开发

基于 cylab-be/wowa-training 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 805
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 2
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-09-14