📜  使用Perceptron网络的OR门

📅  最后修改于: 2021-05-30 10:56:59             🧑  作者: Mango

感知器网络属于单层前馈网络,也称为简单感知器。感知器网络由三个单元组成,即感觉单元(输入单元),关联单元(隐藏单元),响应单元(输出单元)。感觉单元连接到关联单元,关联单元的权重固定为1、0或-1,这些权重是随机分配的。
问题是使用使用C++代码的感知器网络来实现或门控。

#include
using namespace std;
int main()
{
    //Array for Binary Input
    int arr[4][2] = { {0,0},
        {0,1},
        {1,0},
        {1,1}
    };
  
    //Target array for Binary Input
    int t[4] = {0,1,1,1};
  
    // Considering learning rate=1
    int alp = 1;
  
    // yi = input
    // yo = output
    int w1 = 0, w2 = 0, b = 0, count = 0, i, yi, yo;
    int dw1,dw2,db;
  
    while(1)
    {
        cout<<"x1"<<" "<<"x2"<<" "<<"b"<<" "<<"yi"<<" "<<
            "yo"<<" "<<"t"<<" "<<"dw1"<<" "<<"dw2"<<" "<<"db"<<
            " "<<"w1"<<" "<<"w2"<<" "<<"b"<= 0)
            {
                yo = 1;
            }
            else
            {
                yo = 0;
            }
            if(t[i] == yo)
            {
                count++;
                dw1 = 0;
                dw2 = 0;
                db = 0;
            }
            // Calaulating Change in Weight
            else
            {
                dw1 = alp*(t[i] - yo) * arr[i][0];
                dw2 = alp*(t[i] - yo) * arr[i][1];
                db = alp*(t[i] - yo);
            }
            w1 = w1 + dw1;
            w2 = w2 + dw2;
            b = b + db;
            cout<

输出 :

x1 x2 b yi yo t dw1 dw2 db w1 w2 b
0 0 1 0 1     0 0 0 -1 0 0 -1
0 1 1 -1 0     1 0 1 1 0 1 0
1 0 1 0 1     1 0 0 0 0 1 0
1 1 1 1 1     1 0 0 0 0 1 0

x1 x2 b yi yo t dw1 dw2 db w1 w2 b
0 0 1 0 1     0 0 0 -1 0 1 -1
0 1 1 0 1     1 0 0 0 0 1 -1
1 0 1 -1 0     1 1 0 1 1 1 0
1 1 1 2 1     1 0 0 0 1 1 0

x1 x2 b yi yo t dw1 dw2 db w1 w2 b
0 0 1 0 1     0 0 0 -1 1 1 -1
0 1 1 0 1     1 0 0 0 1 1 -1
1 0 1 0 1     1 0 0 0 1 1 -1
1 1 1 1 1     1 0 0 0 1 1 -1

x1 x2 b yi yo t dw1 dw2 db w1 w2 b
0 0 1 -1 0     0 0 0 0 1 1 -1
0 1 1 0 1     1 0 0 0 1 1 -1
1 0 1 0 1     1 0 0 0 1 1 -1
1 1 1 1 1     1 0 0 0 1 1 -1