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

问题描述 投票:0回答:0

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

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

OutPut Window 如果图片没有加载,这里是文本格式

               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.

这两张图片是整个节目的。

Top of Program Bottom of Program

如果图片没有加载,这里是文本格式的代码。

#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++ machine-learning perceptron
© www.soinside.com 2019 - 2024. All rights reserved.