这可能有点冒险,但我想知道是否有人能够查看这个。我是否正确地进行了线性回归的批量梯度下降?对于单个独立变量和截距,它给出了预期的答案,但对于多个独立变量则不然。
/**
* (using Colt Matrix library)
* @param alpha Learning Rate
* @param thetas Current Thetas
* @param independent
* @param dependent
* @return new Thetas
*/
public DoubleMatrix1D descent(double alpha,
DoubleMatrix1D thetas,
DoubleMatrix2D independent,
DoubleMatrix1D dependent ) {
Algebra algebra = new Algebra();
// ALPHA*(1/M) in one.
double modifier = alpha / (double)independent.rows();
//I think this can just skip the transpose of theta.
//This is the result of every Xi run through the theta (hypothesis fn)
//So each Xj feature is multiplied by its Theata, to get the results of the hypothesis
DoubleMatrix1D hypothesies = algebra.mult( independent, thetas );
//hypothesis - Y
//Now we have for each Xi, the difference between predictect by the hypothesis and the actual Yi
hypothesies.assign(dependent, Functions.minus);
//Transpose Examples(MxN) to NxM so we can matrix multiply by hypothesis Nx1
DoubleMatrix2D transposed = algebra.transpose(independent);
DoubleMatrix1D deltas = algebra.mult(transposed, hypothesies );
// Scale the deltas by 1/m and learning rate alhpa. (alpha/m)
deltas.assign(Functions.mult(modifier));
//Theta = Theta - Deltas
thetas.assign( deltas, Functions.minus );
return( thetas );
}
Colt
库,但我认为函数名称具有表现力并且意义明确。我假设您将independent
矩阵的第一列作为包含全部值为1的向量来估计截距。在多元回归中,值如何不同? - iTech