kuredev
11/13/2017 - 4:52 AM

【ゼロから作るDeep Learning】2.3.1 パーセプトロンの簡単な実装

【ゼロから作るDeep Learning】2.3.1 パーセプトロンの簡単な実装

<?php

/**
 * ex: echo perceptron("nand", 0, 1);
 * @param $type "and" or "nand" or "or"
 * @param $x1
 * @param $x2
 * @return int
 */
function perceptron(string $type, int $x1, int $x2){
    switch($type){
        case "add":
            $w1 = 0.5;
            $w2 = 0.5;
            $theta = 0.7;
            break;
        case "or":
            $w1 = 1;
            $w2 = 1;
            $theta = 0.5;
            break;
        case "nand":
            $w1 = -0.5;
            $w2 = -0.5;
            $theta = -0.7;
            break;
    }

	$w = $x1*$w1 + $x2*$w2;
	return($w > $theta) ?  1 : 0;
}