📜  使用Adaline网络实现ANDNOT Gate

📅  最后修改于: 2021-05-31 21:01:08             🧑  作者: Mango

ADALINE(自适应线性神经元或以后的自适应线性元素)是早期的单层人工神经网络,是实现该网络的物理设备的名称。这里的问题是使用Adaline网络实现AND-NOT。在这里,我们执行5个时期的训练,并在每种情况下计算总平均误差,总平均误差在每个时期之后减小,之后变得几乎恒定。

The Truth Table for AND-NOT Gate is as follows:
 x1  x2  t
 1   1  -1
 1  -1   1
-1   1  -1
-1  -1  -1
#include 
using namespace std;
int main()
{
    // input array
    int arr[4][2] = { { 1, 1 },
        { 1, -1 },
        { -1, 1 },
        { -1, -1 }
    };
      
    // target array
    int t[4] = { -1, 1, -1, -1 }, i, j;
    float yi, dif, dw1, dw2, db, w1 = 0.2, w2 = 0.2, b = 0.2, err[4];
  
    // taking bias in each case as 1
    // Calculation upto 5 epochs
    // consider learning rate = 0.2
  
    for (i = 0; i < 5; i++)
    {
        float avg = 0;
          
        cout << "EPOCH " << i + 1 << " Errors" << endl
             << endl;
        for (j = 0; j < 4; j++)
        {
            // calculating net input
            yi = arr[j][0] * w1 + arr[j][1] * w2 + 1 * b;
            dif = t[j] - yi;
              
            // updated weights
            w1 += 0.2 * dif * arr[j][0];
            w2 += 0.2 * dif * arr[j][1];
            b += 0.2 * dif * 1;
            err[j] = dif * dif;
            cout << err[j] << " ";
            avg += err[j];
        }
        cout << endl
             << "Total Mean Error :" << avg << endl
             << endl
             << endl;
    }
    return 0;
}
输出:
EPOCH 1 Errors

2.56 1.2544 0.430336 1.47088 
Total Mean Error :5.71562


EPOCH 2 Errors

0.951327 0.569168 0.106353 0.803357 
Total Mean Error :2.43021


EPOCH 3 Errors

0.617033 0.494715 0.369035 0.604961 
Total Mean Error :2.08574


EPOCH 4 Errors

0.535726 0.496723 0.470452 0.541166 
Total Mean Error :2.04407


EPOCH 5 Errors

0.515577 0.503857 0.499932 0.520188 
Total Mean Error :2.03955