【问题标题】:Logistic Regression Returning Wrong Prediction逻辑回归返回错误的预测
【发布时间】:2019-05-21 09:12:27
【问题描述】:

我正在尝试在 C++ 中实现逻辑回归,但我得到的预测与我的预期并不接近。我不确定我对逻辑回归或代码的理解是否有错误。

我复习过算法,把学习率搞砸了,但结果很不一致。

double theta[4] = {0,0,0,0};

double x[2][3] = {
    {1,1,1},
    {9,9,9},
    };

double y[2] = {0,1};

//prediction data
double test_x[1][3] = {
        {9,9,9},
        };

int test_m = sizeof(test_x) / sizeof(test_x[0]);

int m = sizeof(x) / sizeof(x[0]);

int n = sizeof(theta) / sizeof(theta[0]);

int xn = n - 1;

struct Logistic
{
    double sigmoid(double total)
    {
        double e = 2.71828;
    double sigmoid_x = 1 / (1 + pow(e, -total));
    return sigmoid_x;
}

double h(int x_row)
{
    double total = theta[0] * 1;

    for(int c1 = 0; c1 < xn; ++c1)
    {
        total += theta[c1 + 1] * x[x_row][c1];
    }

    double final_total = sigmoid(total);
    //cout << "final total: " << final_total;
    return final_total;
}


double cost()
{
    double hyp;
    double temp_y;
    double error;

    for(int c1 = 0; c1 < m; ++c1)
    {
        //passes row of x to h to calculate sigmoid(xi * thetai)
        hyp = h(c1);
        temp_y = y[c1];
        error += temp_y * log(hyp) + (1 - temp_y) * log(1 - hyp);
    }// 1 / m
    double final_error = -.5 * error;
    return final_error;
}

void gradient_descent()
{
    double alpha = .01;

        for(int c1 = 0; c1 < n; ++c1)
        {
            double error = cost();
            cout << "final error: " << error << "\n";
            theta[c1] = theta[c1] - alpha * error;
            cout << "theta: " << c1 << " " << theta[c1] << "\n";
        }   
}

void train()
{
    for(int epoch = 0; epoch <= 10; ++epoch)
    {
        gradient_descent(); 
        cout << "epoch: " << epoch << "\n";
    }   
}

vector<double> predict()
{
    double temp_total;
    double total;
    vector<double> final_total;

    //hypothesis equivalent function
    temp_total = theta[0] * 1;

    for(int c1 = 0; c1 < test_m; ++c1)
    {
        for(int c2 = 0; c2 < xn; ++c2)
        {
            temp_total += theta[c2 + 1] * test_x[c1][c2];
        }

        total = sigmoid(temp_total);
        //cout << "final total: " << final_total;
        final_total.push_back(total);
    }
    return final_total;
}

};

int main()
{
    Logistic test;
    test.train();
    vector<double> prediction = test.predict();
    for(int c1 = 0; c1 < test_m; ++c1)
    {
        cout << "prediction: " << prediction[c1] << "\n";
    }
}

【问题讨论】:

  • 您没有在double cost() 中初始化error。你的编译器应该告诉你的。
  • 谢谢。我初始化了错误,但它没有改变预测值。

标签: c++ machine-learning logistic-regression


【解决方案1】:

从一个非常小的学习率开始,尝试更大的迭代次数。还没有测试你的代码。但我猜成本/错误/能量会从一个峰跳到另一个峰。

【讨论】:

  • 以非常小的学习率,它给了我接近 0.5。如果仅在我的代码中的少量数据集上进行训练,该算法是否能够预测 x=9、9、9 的 y=1?
  • 可以使用逻辑回归得到 y ≈ 1。在您当前的代码中,我不这么认为(无法运行它来调试)。我之前使用 Keras 逻辑回归测试过类似的东西,您可以按照towardsdatascience.com/… 进行问题设置和测试。
【解决方案2】:

与您的问题有些无关,但不是使用 pow 计算 e^-total,而是使用 exp(它快得多!)。此外,无需将 sigmoid 函数设为成员函数、使其成为静态函数或只是普通的 C 函数(它不需要结构中的任何成员变量)。

static double sigmoid(double total)
{
    return 1.0 / (1.0 + exp(-total));
}

【讨论】:

  • 谢谢,我一定会做出这些改变
猜你喜欢
  • 2019-03-05
  • 2019-06-07
  • 1970-01-01
  • 2021-02-27
  • 2022-11-27
  • 2021-04-30
  • 1970-01-01
  • 2021-01-23
  • 2021-04-03
相关资源
最近更新 更多