글 삭제

작성자 본인만 삭제할 수 있습니다

← 홈으로
익명 · 2023.07.31 · 조회 9,384
다층 퍼셉트론(Multilayer Perceptron)을 PHP로 구현 (GPT)
<p>AND와 OR 연산을 위한 간단한 다층 퍼셉트론을 PHP로 구현한 코드</p><p><br></p><p>[code]</p><p>&lt;?php</p><p><br></p><p>// 활성화 함수 (Sigmoid Function)</p><p>function sigmoid($x) {</p><p>&nbsp; &nbsp; return 1 / (1 + exp(-$x));</p><p>}</p><p><br></p><p>// 다층 퍼셉트론 함수</p><p>function mlp($inputs, $weights1, $bias1, $weights2, $bias2) {</p><p>&nbsp; &nbsp; $hidden_layer = 0;</p><p>&nbsp; &nbsp; foreach ($inputs as $i =&gt; $input) {</p><p>&nbsp; &nbsp; &nbsp; &nbsp; $hidden_layer += $input * $weights1[$i];</p><p>&nbsp; &nbsp; }</p><p>&nbsp; &nbsp; $hidden_layer += $bias1;</p><p>&nbsp; &nbsp; $hidden_layer = sigmoid($hidden_layer);</p><p><br></p><p>&nbsp; &nbsp; $result = 0;</p><p>&nbsp; &nbsp; foreach ($hidden_layer as $i =&gt; $output) {</p><p>&nbsp; &nbsp; &nbsp; &nbsp; $result += $output * $weights2[$i];</p><p>&nbsp; &nbsp; }</p><p>&nbsp; &nbsp; $result += $bias2;</p><p>&nbsp; &nbsp; return sigmoid($result);</p><p>}</p><p><br></p><p>// AND 연산 학습</p><p>$and_inputs = [</p><p>&nbsp; &nbsp; [0, 0],</p><p>&nbsp; &nbsp; [0, 1],</p><p>&nbsp; &nbsp; [1, 0],</p><p>&nbsp; &nbsp; [1, 1]</p><p>];</p><p>$and_weights1 = [0.5, 0.5];</p><p>$and_bias1 = -0.7;</p><p>$and_weights2 = [0.5, 0.5];</p><p>$and_bias2 = -0.9;</p><p><br></p><p>// OR 연산 학습</p><p>$or_inputs = [</p><p>&nbsp; &nbsp; [0, 0],</p><p>&nbsp; &nbsp; [0, 1],</p><p>&nbsp; &nbsp; [1, 0],</p><p>&nbsp; &nbsp; [1, 1]</p><p>];</p><p>$or_weights1 = [0.5, 0.5];</p><p>$or_bias1 = -0.2;</p><p>$or_weights2 = [0.5, 0.5];</p><p>$or_bias2 = -0.1;</p><p><br></p><p>// 결과 출력 함수</p><p>function printResults($inputs, $weights1, $bias1, $weights2, $bias2, $operation) {</p><p>&nbsp; &nbsp; echo "$operation 연산 결과:\n";</p><p>&nbsp; &nbsp; foreach ($inputs as $input) {</p><p>&nbsp; &nbsp; &nbsp; &nbsp; $output = mlp($input, $weights1, $bias1, $weights2, $bias2);</p><p>&nbsp; &nbsp; &nbsp; &nbsp; echo implode(" $operation ", $input) . " =&gt; " . round($output) . "\n";</p><p>&nbsp; &nbsp; }</p><p>}</p><p><br></p><p>// 결과 출력</p><p>printResults($and_inputs, $and_weights1, $and_bias1, $and_weights2, $and_bias2, 'AND');</p><p>printResults($or_inputs, $or_weights1, $or_bias1, $or_weights2, $or_bias2, 'OR');</p><p><br></p><p>?&gt;</p><p>[/code]</p>
삭제된 게시글은 복구할 수 없습니다