【ゼロから作るDeep Learning】2.5.2 XORゲートの実装(多層パーセプトロン)
<?php
//echo perceptron_xor(1, 1);
/**
* Created by PhpStorm.
* User: kure
* Date: 11/14/17
* Time: 1:37 PM
*/
/**
* @param $type and or nand or or
* @param $x1
* @param $x2
* @return int
*/
function perceptron(string $type, int $x1, int $x2){
switch($type){
case "and":
$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;
default:
echo "Error";
}
$w = $x1*$w1 + $x2*$w2;
return($w > $theta) ? 1 : 0;
}
function perceptron_xor($x1, $x2){
$s1 = perceptron("nand", $x1, $x2);
$s2 = perceptron("or", $x1, $x2);
return perceptron("and", $s1, $s2);
}