devtronic/legendary-mind

易于使用的PHP编写的神经网络

v2.1 2017-06-11 07:14 UTC

This package is auto-updated.

Last update: 2024-09-21 23:48:05 UTC


README

GitHub tag Packagist Travis Packagist

Legendary Mind

Legendary Mind是一个易于使用的PHP编写的神经网络

  • 用于处理网络的包装类
  • 存档和恢复以文本文件序列化的神经网络

安装

composer require devtronic/legendary-mind

使用方法

独立网络

<?php

use Devtronic\Layerless\Activator\TanHActivator;
use Devtronic\LegendaryMind\Mind;

require_once 'vendor/autoload.php';

// Create the topology for the net
$topology = [2, 3, 1];

// Set the activator
$activator = new TanHActivator();

// Instantiate the Mind
$mind = new Mind($topology, $activator);

// Setup XOR Lessons
$lessons = [
    [
        [0, 0], # Inputs
        [0]     # Outputs
    ],
    [
        [0, 1], # Inputs
        [1]     # Outputs
    ],
    [
        [1, 0], # Inputs
        [1]     # Outputs
    ],
    [
        [1, 1], # Inputs
        [0]     # Outputs
    ],
];

// Train the lessons
$mind->train($lessons);

// Setup the check lesson
$test = [1, 0];
$expected = [1];

// Propagate the check lesson
$mind->predict($test);

// Print the Output
print_r($mind->getOutput());

// Backpropagate
$mind->backPropagate($expected);

使用包装(推荐)

<?php

use Devtronic\Layerless\Activator\TanHActivator;
use Devtronic\LegendaryMind\Wrapper;

require_once 'vendor/autoload.php';

// Set the activator function
$activator = new TanHActivator();

$wrapper = new Wrapper($hiddenNeurons = 3, $hiddenLayers = 1);

// Possible input values
$properties = [
    'color' => ['red', 'pink', 'blue', 'green'],
    'hair_length' => ['short', 'long']
];

$outputs = [
    'gender' => ['male', 'female']
];

// Setup the wrapper
$wrapper->initialize($properties, $outputs, $activator);

// Setup the lessons
$lessons = [
    [
        'input' => [
            'color' => 'red',
            'hair_length' => 'long',
        ],
        'output' => [
            'gender' => 'female'
        ],
    ],
    [
        'input' => [
            'color' => 'blue',
            'hair_length' => 'short',
        ],
        'output' => [
            'gender' => 'male'
        ],
    ],
    [
        'input' => [
            'color' => 'red',
            'hair_length' => 'short',
        ],
        'output' => [
            'gender' => 'male'
        ],
    ],
];

// Train the lessons
$wrapper->train($lessons);

// Setup the check lesson
$test_lesson = [
    'input' => [
        'color' => ['pink', 'green'],
        'hair_length' => 'long',
    ],
    'output' => [
        'gender' => 'female'
    ]
];

// Propagate the check lesson
$wrapper->predict($test_lesson);

// Print the Output
print_r($wrapper->getResult());

// Backpropagate
$wrapper->backPropagate($test_lesson);

存档和恢复网络(仅限包装,第16行和第67行)

<?php

use Devtronic\Layerless\Activator\TanHActivator;
use Devtronic\LegendaryMind\Wrapper;

require_once 'vendor/autoload.php';

// Set the activation function
$activator = new TanHActivator();
$wrapper = new Wrapper($hiddenNeurons = 3, $hiddenLayers = 1);

$network_file = 'network.txt';

if (is_file($network_file)) {
    // Restore the Network
    $wrapper->restore($network_file);
} else {

    // Possible input values
    $properties = [
        'color' => ['red', 'pink', 'blue', 'green'],
        'hair_length' => ['short', 'long']
    ];

    $outputs = [
        'gender' => ['male', 'female']
    ];

    // Setup the wrapper
    $wrapper->initialize($properties, $outputs, $activator);

    // Setup the lessons
    $lessons = [
        [
            'input' => [
                'color' => 'red',
                'hair_length' => 'long',
            ],
            'output' => [
                'gender' => 'female'
            ],
        ],
        [
            'input' => [
                'color' => 'blue',
                'hair_length' => 'short',
            ],
            'output' => [
                'gender' => 'male'
            ],
        ],
        [
            'input' => [
                'color' => 'red',
                'hair_length' => 'short',
            ],
            'output' => [
                'gender' => 'male'
            ],
        ],
    ];

    // Train the lessons
    $wrapper->train($lessons);

    // Archive the Network
    $wrapper->archive($network_file);
}

// Setup the check lesson
$test_lesson = [
    'input' => [
        'color' => ['pink', 'green'],
        'hair_length' => 'long',
    ],
    'output' => [
        'gender' => 'female'
    ]
];

// Propagate the check lesson
$wrapper->predict($test_lesson);

// Print the Output
print_r($wrapper->getResult());

// Backpropagate
$wrapper->backPropagate($test_lesson);