我正在实现一个单层感知器,在代码中找不到问题。问题是,当我运行一个程序时,我的第二个数据集是X(0,0,1),权重系数是W(0,0,0.01),因此我的激活应该等于0。01。虽然我得到的输出为0。下面是我的代码:
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
using namespace std;
//Helper function to generate a random number between 0 and 1
double weight_coef() {
//((double)rand() / RAND_MAX);
srand(time(NULL));
double random = static_cast<double>(rand() / RAND_MAX);
random = round(random * 100) / 100.0; //to 2 dec points
return random;
}
// Function to calculate the dot product of two vectors
double activation(vector<int> x, vector<double> w) {
double result = 0.0;
for (int i = 0; i < x.size(); i++) {
result += static_cast<double>(x[i] * w[i]);
}
return result;
}
int main() {
// Set up the data
vector<vector<int>> X = { {0, 0, 0}, {0, 0, 1}, {0, 1, 0}, {0, 1, 1},
{1, 0, 0}, {1, 0, 1}, {1, 1, 0}, {1, 1, 1} };
vector<int> T = { 0, 1, 1, 1, 0, 1, 1, 1 };
// Set up the variables
vector<double> w = { weight_coef(), weight_coef(), weight_coef()};
double theta = 0.5;
//double nu = 0.3;
double nu = 0.01;
//double nu = 0.02;
int num_iterations = 0;
bool all_errors_zero = false;
// Loop until all errors are zero for all data points
while (!all_errors_zero) {
all_errors_zero = true;
num_iterations++;
// Loop through all data points
for (int i = 0; i < X.size(); i++) {
// Calculate the sum and output Y
double sum = activation(X[i], w);
int Y = (sum > theta) ? 1 : 0;
// Calculate the error and delta
double error = nu * (T[i] - Y);
vector<double> delta = {
error * X[i][0],
error * X[i][1],
error * X[i][2]};
double deltaTheta = error * theta;
// Update the weights
for (int j = 0; j < w.size(); j++) {
w[j] += delta[j];
}
// Check if the error is zero
if (error != 0) {
all_errors_zero = false;
}
// Output the results
cout << "Weights: [" << w[0] << ", " << w[1] << ", " << w[2] << "]\n";
cout << "Data set " << i + 1 << ":\n";
cout << "Activation: " << sum << "\n";
cout << "Y: " << Y << "\n";
cout << "T: " << T[i] << "\n";
cout << "Delta: " << error << "\n";
cout << "Delta * Weights: [" << delta[0] << ", " << delta[1] << ", " << delta[2] << "]\n";
cout << "Delta * Theta: " << deltaTheta << "\n\n";
}
}
// Output the final results
cout << "Final weights: [" << w[0] << ", " << w[1] << ", " << w[2] << "]\n";
cout << "Number of iterations: " << num_iterations << "\n";
return 0;
}
如果你能告诉我出了什么问题,我会很感激的。
1条答案
按热度按时间iqxoj9l91#
问题是
std::rand
返回一个保证在0和RAND_MAX
之间的整数。因此权重系数代码将始终返回0(实际上它将 * 几乎 * 始终为0,并且将以概率
1/RAND_MAX
为1)。一个解决方案是取随机整数,将其除以100,然后将其解释为两位小数的概率。但请注意,这将是稍微不均匀的,因为RAND_MAX
可能不能被100整除(可能是2^31 - 1 = 2147483647)。如果你想制造一个均匀随机变量,你可以做类似的事情
我注意到在每次函数调用时重复地重新初始化种子也不是一个好主意。相反,设置种子一次--可能是在main中(就像我发布的代码片段中隐式地完成的那样)。