二值推理人工智能网络

1.3.0 2020-03-29 02:39 UTC

This package is auto-updated.

Last update: 2024-09-05 12:43:49 UTC


README

二值推理人工智能网络 - 一个自包含的预测/外推系统。

本文档是对该系统的简单介绍。更多详细信息,请参阅BRAIN手册

安装

BRAIN现在在Packagist中,因此可以使用Composer安装:

composer require blondie101010/brain

使用

实例化

$brain = new \Blondie101010\Brain\Brain($name, $site, $brainPath = "./data");

参数

  • $name: 给BRAIN起的名字,用于其文件名
  • $site: 定义实例运行的字符串...它用于定义主链名称,但主要为了未来的用途,例如导入和导出支持;尽管它没有强制执行,并且目前不会有影响,但建议只使用字母数字字符
  • $brainPath: 读取和写入BRAIN导出文件的目录。

BRAIN的文件名将是$brainPath/Brain-$name.dat

学习

单个记录

您可以使用以下方法学习单个记录:

$brain->getAnswer(array $dataRecord, float $result = null);

参数

  • $dataRecord: 字段数组。有关更多详细信息,请参阅BRAIN手册:输入特征
  • $result: -1和+1之间的值。它在三种状态下进行评估:接近-1、接近0和接近+1。通常,0表示平均或未知。有关更多详细信息,请参阅BRAIN手册:结果描述

请注意,结果也可以包含在$dataRecord['_result']特殊字段中,该字段被精确地处理为$result参数,但如果$result存在且非空,则$result具有优先级。

使用Feeder类进行批量处理

以下是一个将数据文件馈送到BRAIN的简单脚本

require "vendor/autoload.php";

use \Blondie101010\Brain\Common;
use \Blondie101010\Brain\Brain;
use \Blondie101010\Brain\Feeder;

Common::$debug = Common::DEBUG_INFO;  // make useful messages visible to see how it progresses

$inputFile = "demo.dat";
$brainName = "demoBrain";
$site = "A";  // site identifier, for future use
$brainPath = "./data";
$skip = 0;
$sample = false;
$batchSize = 25000;

$brain = new Brain($brainName, $site, $brainPath);
$feeder = new Feeder($brain, Feeder::MODE_TEST_AND_LEARN, $skip, $batchSize, $sample);

echo "Processing $inputFile.  Send SIGTERM to end cleanly (and therefore store new learnings).\n";
$feeder->processFile($inputFile);

为了演示BRAIN的工作原理并提供一个示例脚本,以下是用以生成测试数据的代码

$outputFile = "demo.dat";

/*
 * Data set builder for a simple potential murderer profiler.
 *
 * In order to validate the BRAIN's operations, we need to make sure the criteria used can not be misleading,
 * so we'll make rules to define a potential murderer: green race with (blue eyes or big ears) and >= 15 years old
 *
 * The result will be based on the most intensive crime they did which is on a scale of 1-10 where 5 is a fairly small
 * misdemeanor and 7 is a severe agression which could be fatal.
 *
 * Note that we DO NOT believe that the race or ear size have any impact on the criminality level, and some green guys are really nice.
 */

// Use ridiculous criteria to evaluate the risk of someone being a murderer in order to test the learning algorithm in a   consistant fashion.
function rateCriminality(array $person) {
    $result = rand(1, 2);  // base crime level

    if ($person['race'] == 2 && ($person['eyeColor'] == 1 || $person['earSize'] >= 6) && $person['age'] >= 15) {
       $result += rand(6, 7);
    }
    elseif ($person['race'] == 3 && $person['earSize'] > 4) {  // arbitrary rules to fill the middle result (close to 0)
       $result = 5.0;
    }

    // adjust result to be between -1 and +1 for the Brain
    return $result * 0.2 - 1;
}


$colors = ['red', 'blue', 'green', 'purple'];
$data = "";

for ($i = 0; $i < 50000; $i ++) {
   $result = 0;

   $person= ['race' => array_rand($colors), 'eyeColor' => array_rand($colors),
             'earSize' => rand(3, 9), 'age' => rand(1, 60)];

   $person['_result'] = rateCriminality($person);

   $data .= serialize($person) . "\n";
}

file_put_contents($outputFile, $data);

要测试BRAIN的演示,您基本上运行数据生成器,然后在该数据文件上运行馈送脚本。上面的代码将使用./demo.dat作为演示输入数据文件(数据生成器将其写入)。

当您运行馈送脚本时,您会注意到许多痕迹,表明它正在处理数据。它基本上运行请求的批量大小,然后处理未正确学习的记录,直到达到一定水平,重新测试整个批次(B测试),如果结果可接受,则移动到下一个批次。演示以25K记录的批次进行,而数据文件中有50K记录,因此当您运行它时,它执行两个批次。

如您所见,随着它的发展,不同的测试结果越来越高且越来越稳定。

获取答案

获取答案的方式与学习的方式完全相同。要从数据记录中获取答案,您只需不提供实际结果。因此,您只需要不带第二个参数调用getAnswer(),并且$dataRecord不包含'_result'字段。

$answer = $brain->getAnswer($dataRecord);

支持

除github之外,尚未定义支持渠道,但如果有需求,我们计划在IRC上开通支持渠道。有兴趣的人可以在FreeNode IRC上的昵称blondie101010Gitter上找到我。

如果您发现任何问题或提出建议,请放心,所有建设性的评论都受到欢迎。

根据需求,可以进行商业开发和支持安排。