다층 퍼셉트론(Multilayer Perceptron)을 PHP로 구현 (GPT) > 코딩 스토리

다층 퍼셉트론(Multilayer Perceptron)을 PHP로 구현 (GPT)

페이지 정보

작성자 익명이름으로 검색 (192.♡.0.1) 작성일 23-07-31 23:04 조회 8,068 댓글 1

본문

AND와 OR 연산을 위한 간단한 다층 퍼셉트론을 PHP로 구현한 코드


[code]

<?php


// 활성화 함수 (Sigmoid Function)

function sigmoid($x) {

    return 1 / (1 + exp(-$x));

}


// 다층 퍼셉트론 함수

function mlp($inputs, $weights1, $bias1, $weights2, $bias2) {

    $hidden_layer = 0;

    foreach ($inputs as $i => $input) {

        $hidden_layer += $input * $weights1[$i];

    }

    $hidden_layer += $bias1;

    $hidden_layer = sigmoid($hidden_layer);


    $result = 0;

    foreach ($hidden_layer as $i => $output) {

        $result += $output * $weights2[$i];

    }

    $result += $bias2;

    return sigmoid($result);

}


// AND 연산 학습

$and_inputs = [

    [0, 0],

    [0, 1],

    [1, 0],

    [1, 1]

];

$and_weights1 = [0.5, 0.5];

$and_bias1 = -0.7;

$and_weights2 = [0.5, 0.5];

$and_bias2 = -0.9;


// OR 연산 학습

$or_inputs = [

    [0, 0],

    [0, 1],

    [1, 0],

    [1, 1]

];

$or_weights1 = [0.5, 0.5];

$or_bias1 = -0.2;

$or_weights2 = [0.5, 0.5];

$or_bias2 = -0.1;


// 결과 출력 함수

function printResults($inputs, $weights1, $bias1, $weights2, $bias2, $operation) {

    echo "$operation 연산 결과:\n";

    foreach ($inputs as $input) {

        $output = mlp($input, $weights1, $bias1, $weights2, $bias2);

        echo implode(" $operation ", $input) . " => " . round($output) . "\n";

    }

}


// 결과 출력

printResults($and_inputs, $and_weights1, $and_bias1, $and_weights2, $and_bias2, 'AND');

printResults($or_inputs, $or_weights1, $or_bias1, $or_weights2, $or_bias2, 'OR');


?>

[/code]

좋아요90 이 글을 좋아요하셨습니다
url 복사 카카오톡 공유 라인 공유 페이스북 공유 트위터 공유

카테고리 분류 학습 시스템 (총 0개 학습됨)

예측 카테고리: 문화-예술 (랜덤 - 학습 데이터 없음)

이 분류가 맞나요? 학습시켜주세요!

2kat님의 댓글

no_profile 2kat쪽지보내기 자기소개 아이디로 검색 전체게시물 아이피 (220.♡.000.000) 작성일

최고입니다.

😶
❤️
😂
😅
😮
😡
🥵
  • RSS
  • _  글쓰기 글쓰기
전체 302건
게시물 검색

접속자집계

오늘
2,588
어제
3,897
최대
42,418
전체
940,816