简体   繁体   English

线性回归的神经网络

[英]Neural network for linear regression

I found this great source that matched the exact model I needed: http://ufldl.stanford.edu/tutorial/supervised/LinearRegression/ 我找到了与我所需的确切模型相匹配的出色资源: http : //ufldl.stanford.edu/tutorial/supervised/LinearRegression/

The important bits go like this. 重要的部分是这样的。

You have a plot x->y. 您有一个x-> y图。 Each x-value is the sum of "features" or how I'll denote them, z . 每个x值都是“特征”的总和或z表示方式。

So a regression line for the x->y plot would go h(SUM(z(subscript-i)) where h(x) is the regression line (function) 因此,x-> y图的回归线将变为h(SUM(z(subscript-i)) ,其中h(x)是回归线(函数)

In this NN the idea is that each z-value gets assigned a weight in a way that minimizes the least squared error. 在此NN中,想法是为每个z-value分配权重,以最小化最小平方误差。

The gradient function is used to update weights to minimize error. 梯度函数用于更新权重以最小化误差。 I believe I may be back propagating incorrectly -- where I update the weights. 我相信我可能会错误地向后传播-在这里更新权重。

So I wrote some code, but my weights aren't being correctly updated. 所以我写了一些代码,但是我的权重没有正确更新。

I may have simply misunderstood a spec from that Stanford post, so that's where I need your help. 我可能只是误解了斯坦福大学那篇文章中的规范,所以这就是我需要您帮助的地方。 Can anyone verify I have correctly implemented this NN? 谁能证明我已经正确实现了该NN?

My h(x) function was a simple linear regression on the initial data. 我的h(x)函数是对初始数据的简单线性回归。 In other words, the idea is that the NN will adjust weights so that all data points shift closer to this linear regression. 换句话说,想法是NN将调整权重,以便所有数据点都更接近于线性回归。

for (epoch = 0; epoch < 10000; epoch++){

    //loop number of games
    for (game = 1; game < 39; game++){
      sum = 0;
      int temp1 = 0;
      int temp2 = 0;
      //loop number of inputs
      for (i = 0; i < 10; i++){
        //compute sum = x
        temp1 += inputs[game][i] * weights[i];
      }

      for (i = 10; i < 20; i++){
        temp2 += inputs[game][i] * weights[i];
      }

      sum = temp1 - temp2;

      //compute error
      error += .5 * (5.1136 * (sum) + 1.7238 - targets[game]) * (5.1136 * (sum) + 1.7238 - targets[game]);
      printf("error = %G\n", error);
      //backpropogate
      for (i = 0; i < 20; i++){
        weights[i] = sum * (5.1136 * (sum) + 1.7238 - targets[game]); //POSSIBLE ERROR HERE
      }

    }

    printf("Epoch = %d\n", epoch);
    printf("Error = %G\n", error);


  }

Please check out Andrew Ng's Coursera . 请查看吴彦祖的Coursera He is the professor of Machine Learning at Stanford and can explain the concept of Linear Regression to you better than any pretty much anyone else. 他是斯坦福大学的机器学习教授,可以比其他任何人更好地向您解释线性回归的概念。 You can learn the essentials for linear regression in the first lesson. 您可以在第一课中学习线性回归的基本知识。

For linear regression, you are trying to minimize the cost function, which in this case is the sum of squared errors (predicted value - actual value)^2 and is achieved by gradient descent. 对于线性回归,您正在尝试最小化成本函数,在这种情况下,该函数为平方误差的总和(预测值-实际值)^ 2,可通过梯度下降来实现。 Solving a problem like this does not require a Neural Network and using one would be rather inefficient. 解决这样的问题不需要神经网络,而使用神经网络则效率很低。

For this problem, only two values are needed. 对于此问题,仅需要两个值。 If you think back to the equation for a line, y = mx + b, there are really only two aspects of a line that you need: The slope and the y-intercept. 如果回想一下方程式y = mx + b,则实际上只需要两条线:斜率和y轴截距。 In linear regression you are looking for the slope and y-intercept that best fits the data. 在线性回归中,您正在寻找最适合数据的斜率和y截距。

In this problem, the two values can be represented by theta0 and theta1. 在此问题中,两个值可以用theta0和theta1表示。 theta0 is the y-intercept and theta1 is the slope. theta0是y截距,theta1是斜率。

This is the update function for Linear Regression: 这是线性回归的更新函数:

在此处输入图片说明

Here, theta is a 2 x 1 dimensional vector with theta0 and theta1 inside of it. 在此,theta是2 x 1维向量,其中有theta0和theta1。 What you are doing is taking theta and subtracting the mean of the sum of errors multiplied by a learning rate alpha (usually small, like 0.1). 您正在执行的操作是取theta并减去误差总和的平均值乘以学习率alpha(通常很小,例如0.1)。

Let's say the real perfect fit for the line is at y = 2x + 3, but our current slope and y-intercept are both at 0. Therefore, the sum of errors will be negative , and when theta is subtracted from a negative number, theta will increase, moving your prediction closer to the correct value. 假设这条线的真正完美拟合是y = 2x + 3,但是我们当前的斜率和y截距都为0。因此,误差总和为 ,当从负数减去theta时, theta会增加,使您的预测更接近正确的值。 And vice versa for positive numbers. 反之亦然。 This is a basic example of gradient descent, where you are descending down a slope to minimize the cost (or error) of the model. 这是梯度下降的一个基本示例,在该过程中您将沿某个斜率下降以最大程度地降低模型的成本(或误差)。

This is the type of model you should be trying to implement in your model instead of a Neural Network, which is more complex. 这是您应该尝试在模型中实现的模型类型,而不是更复杂的神经网络。 Try to gain an understanding of linear and logistic regression with gradient descent before moving on to Neural Networks. 在继续使用神经网络之前,请尝试了解具有梯度下降的线性和逻辑回归。

Implementing a linear regression algorithm in C can be rather challenging, especially without vectorization . 在C中实现线性回归算法可能非常具有挑战性,尤其是在没有向量化的情况下 If you are looking to learn about how a linear regression algorithm works and aren't specifically looking to use C to make it, I recommend using something like MatLab or Octave (a free alternative) to implement it instead. 如果您想了解线性回归算法的工作原理,而不是特别想使用C来实现它,我建议使用MatLab或Octave之类的东西(免费的替代品)来实现它。 After all, the examples from the post you found use the same format. 毕竟,您发现的帖子中的示例使用相同的格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM