我的分类神经网络存在逻辑问题

时间:2012-09-01 00:38:43

标签: c++ machine-learning neural-network

我在C ++中制作了我的第一个1维分类神经网络,其逻辑如下:

  • 如果输入< = 1:f(x)= 1
  • 如果输入> 1:f(x)= -1

我起初非常草率,但是现在我知道它有效,我将代码更改为输入数量更通用,所以当我感觉如此倾向时,我可以轻松地改变[x]的大小。然而,新代码没有得到正确的逻辑;它为所有输入提供了完全错误的答案和类似的权重。 *请记住,我的变量名称是愚蠢的,变量“输出”实际上是训练集。

这是我对该计划的最终结果:

Input: 1 ; Class: 1 ; Eval = -1
Weight for node 1: -2
Input: 2 ; Class: -1 ; Eval = -1
Weight for node 2: -2
Input: 3 ; Class: -1 ; Eval = -1
Weight for node 3: -1

“类”应该是它,“Eval”就是它实际上是什么。请注意,第一个输入与其训练元素不匹配。


原始代码:

#include <iostream>

using namespace std;

double weights[2] = {0.0};

double classify(double);

int main() {
   double inputs[2] = {1.0, 2.0};
   double outputs[2] = {1.0, -1.0};
   int index = 0;
   bool trained = false;

   while(!trained) {
      trained = true;

       cout << inputs[0] << " , " << outputs[0] << " eval = " << classify(inputs[0]) << endl;
       cout << inputs[1] << " , " << outputs[1] << " eval = " << classify(inputs[1]) << endl;
       cout << "Weights = " << weights[0] << " , " << weights[1] << endl << endl;

       index = 0;

       while(index < 2) {
           double input = inputs[index];
           double output = outputs[index];
           double dClass = classify(input);

           if (dClass != output) {
               weights[0] += output * input;
               weights[1] += output * 1.0;
               trained = false;
           }
           index++;
       }
   }

   return 0;
}

double classify(double input){
double products[2];
double sum = 0;
double threshhold;

// Sumation of inputs
products[0] = input * weights[0];
products[1] = 1.0 * weights[1];
sum         = products[0] + products[1];

// Threshold function
if (sum >= 0.0)
    threshhold = 1.0;
else
    threshhold = -1.0;

return threshhold;
}


修改后的代码:

#include <iostream>
#define NODES 3

using namespace std;

double weights[NODES] = {0.0};

double classify(double);

int main() {
    double inputs[NODES] = {1.0, 2.0, 3.0};
    double outputs[NODES] = {1.0, -1.0, -1.0};
    int index = 0;
    bool trained = false;
    index = 0;

    // While the classifications are incrorrect
    while(!trained) {
       trained = true;

       while(index < NODES) {
           double input = inputs[index];           // Input nodes
           double output = outputs[index];         // Desired class
           double dClass = classify(input);        // Calculated class

           // If calculated class != desired class:
           // adjust the weights
           if (dClass != output) {
               for(int i = 0; i < NODES - 1; i++)
                  weights[i] += output * input;

               // Bias weight
               weights[NODES-1] += output * 1.0;

               trained = false;
           }
           index++;

           // Debugging
           for(int i = 0; i < NODES; i++){
              cout << "Input: " << inputs[i] << " ; Class: " << outputs[i] << " ; Eval = "      << dClass << endl;
              cout << "Weight for node " << i + 1 << ": " << weights[i] << endl;
           }
           cout << endl;
           }

    }

    return 0;
}

double classify(double input){
   double products[NODES];
   double sum = 0;
   double threshhold;

   // Attach weights to nodes
   for(int i = 0; i < NODES - 1; i++)
       products[i] = input * weights[i];

   // Last node with bias
   products[NODES-1] = 1.0 * weights[NODES-1];

   // Sumation of inputs
   for(int i = 0; i < NODES; i++)
       sum += products[i];

   // Threshold function
   if (sum >= 0.0)
       threshhold = 1.0;
   else
       threshhold = -1.0;

   return threshhold;
}

如果你能回答这个问题,那么如果你想给我一些意见,我会有一个跟进(尽管它更接近一个会话问题)。我是实现神经网络的新手,我很好奇你们都认为平均反向传播,固定拓扑神经网络的静态数据结构,以及动态神经网络(可能用于神经演化)。是否有一个良好的神经网络实施惯例?

1 个答案:

答案 0 :(得分:1)

看来我们有一个谜。在我们深入研究它之前,尝试对main()进行这一小改动(只是更改它打印的内容)并告诉我们你得到了什么。不要只是说它给出了错误的答案或者没有达到预期的结果,告诉我们它实际产生了什么

已删除已废除的代码

修改:
你有两个问题。首先,你对这段代码的作用有点困惑; 节点的数量和训练集中元素的数量基本上是独立的,但是你将它们视为相同并且对于你正在迭代哪一个感到困惑。其次,你忽略了在内循环结束时重置index。改变这个:

  // Debugging
  for(int i = 0; i < NODES; i++){
    cout << "Input: " << inputs[i] << " ; Class: " << outputs[i] << " ; Eval = "      << dClass << endl;
    cout << "Weight for node " << i + 1 << ": " << weights[i] << endl;
  }
  cout << endl;
}

到此:

  // Debugging
  cout << "Input: " << input << " ; Class: " << output << " ; Eval = " << dClass << endl;
  cout << "Weights:";
  for(int i = 0; i < NODES; i++){
    cout << " " << weights[i];
  }
  cout << endl;
}
cout << endl;
index = 0;

现在可行。