我用 C++ 制作了我的第一个一维分类神经网络,其逻辑如下:
- 如果输入 <= 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
“Class”是它应该是的,而“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;
}
如果你能回答这个问题,那么如果你想给我意见,我会跟进(尽管它更接近对话问题)。我是实现神经网络的新手,我很好奇你们都认为对于平均反向传播、固定拓扑 ANN 以及动态神经网络(可能用于神经进化)的最佳数据结构。是否有一个好的神经网络实现约定可以通过?