感知器收敛但返回奇怪的结果
Perceptron converging but returning odd results
我在 c++ 中制作了一个简单的感知器来研究 AI,即使按照一本书(pt_br),我也无法让我的感知器返回预期的结果,我试图调试并找到错误,但我没有成功。
我的算法和门结果(A 和 B = Y):
0 && 0 = 0
0 && 1 = 1
1 && 0 = 1
1 && 1 = 1
基本上它作为OR门或随机工作。
我试图跳到彼得·诺文和罗素的书中,但他很快就跳过了这个,并没有解释深度一个感知器训练。
我真的很想学习这些内容的每一寸,所以我不想在没有让简单的感知器工作的情况下跳到多层感知器,你能帮忙吗?
以下代码是操作的最小代码,并提供一些说明:
锐利功能:
int signal(float &sin){
if(sin < 0)
return 0;
if(sin > 1)
return 1;
return round(sin);
}
感知器结构(W 是权重):
struct perceptron{
float w[3];
};
感知器训练:
perceptron startTraining(){
//- Random factory generator
long int t = static_cast<long int>(time(NULL));
std::mt19937 gen;
gen.seed(std::random_device()() + t);
std::uniform_real_distribution<float> dist(0.0, 1.0);
//--
//-- Samples (-1 | x | y)
float t0[][3] = {{-1,0,0},
{-1,0,1},
{-1,1,0},
{-1,1,1}};
//-- Expected result
short d [] = {0,0,0,1};
perceptron per;
per.w[0] = dist(gen);
per.w[1] = dist(gen);
per.w[2] = dist(gen);
//-- print random numbers
cout <<"INIT "<< "W0: " << per.w[0] <<" W1: " << per.w[1] << " W2: " << per.w[2] << endl;
const float n = 0.1; // Lerning rate N
int saida =0; // Output Y
long int epo = 0; // Simple Couter
bool erro = true; // Loop control
while(erro){
erro = false;
for (int amost = 0; amost < 4; ++amost) { // Repeat for the number of samples x0=-1, x1,x2
float u=0; // Variable for the somatory
for (int entrad = 0; entrad < 3; ++entrad) { // repeat for every sinaptic weight W0=θ , W1, W2
u = u + (per.w[entrad] * t0[amost][entrad]);// U <- Weights * Inputs
}
// u=u-per.w[0]; // some references sau to take θ and subtract from U, i tried but without success
saida = signal(u); // returns 1 or 0
cout << d[amost] << " <- esperado | encontrado -> "<< saida<< endl;
if(saida != d[amost]){ // if the output is not equal to the expected value
for (int ajust = 0; ajust < 3; ++ajust) {
per.w[ajust] = per.w[ajust] + n * (d[amost] - saida) * t0[amost][ajust]; // W <- W + ɳ * ((d - y) x) where
erro = true; // W: Weights, ɳ: Learning rate
} // d: Desired outputs, y: outputs
} // x: samples
epo++;
}
}
cout << "Epocas(Loops): " << epo << endl;
return per;
}
主要带测试部分:
int main()
{
perceptron per = startTraining();
cout << "fim" << endl;
cout << "W0: " << per.w[0] <<" W1: " << per.w[1] << " W2: " << per.w[2] << endl;
while(true){
int x,y;
cin >> x >> y;
float u=0;
u = (per.w[1] * x);
u = u + (per.w[2] * y);
//u=u-per.w[0];
cout << signal(u) << endl;
}
return 0;
}
在您的main()
中,重新启用您注释掉的行。或者,您可以这样写它以使其更具启发性:
float u = 0.0f;
u += (per.w[0] * float (-1));
u += (per.w[1] * float (x));
u += (per.w[2] * float (y));
问题是你用三个输入训练感知器,第一个是硬连接到"-1"(使第一个权重w[0]
表现得像一个恒定的"偏差")。因此,在你的训练函数中,你的u
是所有三个权重输入乘积的总和。但是,在您发布的 main() 中,您完全省略了 w[0],从而产生了错误的结果。
相关文章:
- 如何返回一个类的两个对象相加的结果
- 如果我std::dynamic_pointer_cast并且底层dynamic_cast的结果为null,那么返回的sh
- 在没有定义返回类型的函数中返回布尔值,并将结果保存在无错误的char编译中-为什么
- C++Brute Force攻击函数不会返回结果
- 如何从递归函数中完全返回,该函数给出了每个函数结果的累积相加?
- 如何读取 C++ SAFEARRAY**,该 SAFEARRAY** 是 COM 互操作的结果,其中 C# 返回值为
- 为什么我的程序在 O0 和 O2 的优化级别返回不同的结果
- c++ lambda:柯里和函数:使用按值捕获与按引用捕获返回不同的结果
- C++ 获取函数在常量引用中按值返回的结果
- 如何在一个表达式中生成并返回结果?
- 从返回 std::optional of std::vector 的函数中获取结果到调用方
- 三元运算符在返回语句中给出意外的结果
- 为什么函数 tellg() 没有返回好的结果?
- 为什么我可以改变常量对象中的成员变量,这是返回常量对象函数的结果?
- 有没有办法将 for 循环结果返回到像三元运算符这样的函数中?
- 使用std::tie进行类似golang的错误处理,同时返回结果,是否有缺点?(C++11)
- 检查文本文件中的 0 或 1,并根据结果返回
- 如何使用boost::any_range连接多个boost范围并作为函数w/o的结果返回
- 如何从线程的结果返回Qt对象(Qtfutur)
- C++:当在函数内部创建新对象并将其作为结果返回时,我必须使用新运算符来创建对象