C++ 中的感知器表代码 在某些情况下返回不正确的值

Perceptron Table Code in C++ Returning incorrect value in some instances

提问人:thedrooster 提问时间:4/16/2023 最后编辑:thedrooster 更新时间:4/16/2023 访问量:28

问:

我需要帮助。我正在尝试为我的机器学习课程编写 C++ 感知器电缆。我了解表格本身以及与之相关的方程式。

我的问题是,为什么在某些情况下,我的值会以极小的数字返回给我?在第一张图片中,将是我的输出,您会注意到我的一些体重值为 -3E-27。

输出窗口如果图像未加载,则此处为文本格式

               Perceptron acting as an OR Gate
-------------------------------------------------------------
 Epoch   Inputs   Desired   Initial   Actual   Error   Final
-------------------------------------------------------------
   1      0.,0.      0.     0.3,-0.1    0        0.     0.3,-0.1

   1      0.,1.      1.     0.3,-0.1    0       -1.     0.2,-0.2

   1      1.,0.      1.     0.2,-0.2    0       -1.     0.1,-0.3

   1      1.,1.      1.     0.1,-0.3    0       -1. -3.e-17,-0.4

 Epoch   Inputs   Desired   Initial   Actual   Error   Final
-------------------------------------------------------------
   2      0.,0.      0. -3.e-17,-0.4    0       -1. -3.e-17,-0.4

   2      0.,1.      1. -3.e-17,-0.4    0       -1.    -0.1,-0.5

   2      1.,0.      1.    -0.1,-0.5    0       -1.    -0.2,-0.6

   2      1.,1.      1.    -0.2,-0.6    0       -1.    -0.3,-0.7

 Epoch   Inputs   Desired   Initial   Actual   Error   Final
-------------------------------------------------------------
   3      0.,0.      0.    -0.3,-0.7    0       -1.    -0.3,-0.7

   3      0.,1.      1.    -0.3,-0.7    0       -1.    -0.4,-0.8

   3      1.,0.      1.    -0.4,-0.8    0       -1.    -0.5,-0.9

   3      1.,1.      1.    -0.5,-0.9    0       -1.    -0.6,-1.

 Epoch   Inputs   Desired   Initial   Actual   Error   Final
-------------------------------------------------------------
   4      0.,0.      0.    -0.6,-1.    0       -1.    -0.6,-1.

   4      0.,1.      1.    -0.6,-1.    0       -1.    -0.7,-1.

   4      1.,0.      1.    -0.7,-1.    0       -1.    -0.8,-1.

   4      1.,1.      1.    -0.8,-1.    0       -1.    -0.9,-1.

 Epoch   Inputs   Desired   Initial   Actual   Error   Final
-------------------------------------------------------------
   5      0.,0.      0.    -0.9,-1.    0       -1.    -0.9,-1.

   5      0.,1.      1.    -0.9,-1.    0       -1.     -1.,-1.

   5      1.,0.      1.     -1.,-1.    0       -1.     -1.,-2.

   5      1.,1.      1.     -1.,-2.    0       -1.     -1.,-2.

这两张图片是整个程序的图片。

程序顶部 程序底部

如果图像未加载,则此处为文本格式的代码。

#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

int main()
{
    double threshold = .2;
    double learningRate = .1;
    int thresholdResult = 0;

    double x1Inputs[4] = { 0, 0, 1, 1 };
    double x2Inputs[4] = { 0, 1, 0, 1 };
    double DesiredOutputs[4] = { 0,1,1,1 };
    double sum = 0;
    double weightOne = .30;
    double weightTwo = -.10;
    double weightOneFinal = 0;
    double weightTwoFinal = 0;
    double error = 0;
    bool isError = false;

    cout << "               Perceptron acting as an OR Gate               " << endl;
    cout << "-------------------------------------------------------------" << endl;

    for (int j = 0; j < 5; j++) // iterates through each epoch
    {
        for (int i = 0; i < 4; i++) // iterates through each array
        {
            isError = false; // resets the Error check bool to false

            sum = (x1Inputs[i] * weightOne) + (x2Inputs[i] * weightTwo); // Does initial weight and input comparison and sets threshold value for comparison
            if (sum < threshold)
                thresholdResult = 0;
            else if (sum >= threshold)
                thresholdResult = 1;

            if (thresholdResult != DesiredOutputs[i]) // if these don't match caclulate error and set error checker to true
            {
                error = thresholdResult - DesiredOutputs[i];
                isError = true;
            }

            if (isError == true) // !!!THIS IS WHERE THE ERROR OCCURS!!!   calculates new weights if error checker is true otherwise sets the final weights to starting weights
            {
                weightOneFinal = weightOne + (learningRate * error * 1.0);
                weightTwoFinal = weightTwo + (learningRate * error * 1.0);
            }
            else if (isError == false)
            {
                weightOneFinal = weightOne;
                weightTwoFinal = weightTwo;
            }

            if (i == 0)
            {
                cout << " Epoch   Inputs   Desired   Initial   Actual   Error   Final " << endl;
                cout << "-------------------------------------------------------------" << endl;
            }
            cout << setprecision(1) << showpoint << setw(4) << j + 1 << setw(8) << x1Inputs[i] << "," << x2Inputs[i] << setw(8) << DesiredOutputs[i] << setw(8) << weightOne << "," << weightTwo << setw(5) << thresholdResult << setw(10) << error << setw(8) << weightOneFinal << "," << weightTwoFinal << "\n\n";
            
            weightOne = weightOneFinal; // sets main weights to final weights
            weightTwo = weightTwoFinal;
        }
    }
    return 0;
}

我知道这个代码可能不好,我现在正在学习更好的礼仪。任何建议都非常感谢!

我多次尝试单步执行整个程序,但由于某种原因,在嵌套 for 循环的第三次迭代中,WeightFinalOne 的值更改为 -3E-27,我对此没有任何解释。

C++ 机器学习 感知器

评论

0赞 Retired Ninja 4/16/2023
请将您的代码和输出以文本形式添加到问题中。

答: 暂无答案