Open CyrilZhao-sudo opened 3 years ago
void coefficients_sgd(double ** dataset, int col, double *coef, double l_rate, int n_epoch, int train_size) { int i; for (i = 0; i < n_epoch; i++) { int j = 0;//±éÀúÿһÐÐ for (j = 0; j < train_size; j++) { double yhat = predict(col,dataset[j], coef); double err = dataset[j][col - 1] - yhat; coef[0] += l_rate * err * yhat * (1 - yhat); int k; for (k = 0; k < col - 1; k++) { coef[k + 1] += l_rate * err * yhat * (1 - yhat) * dataset[j][k]; } } } for (i = 0; i < col; i++) { //printf("coef[%d]=%f\n",i, coef[i]); } }
void coefficients_sgd(double ** dataset, int col, double *coef, double l_rate, int n_epoch, int train_size) { int i; for (i = 0; i < n_epoch; i++) { int j = 0;//±éÀúÿһÐÐ for (j = 0; j < train_size; j++) { double yhat = predict(col,dataset[j], coef); double err = dataset[j][col - 1] - yhat; coef[0] += l_rate * err * yhat * (1 - yhat); int k; for (k = 0; k < col - 1; k++) { coef[k + 1] += l_rate * err * yhat * (1 - yhat) * dataset[j][k]; } } } for (i = 0; i < col; i++) { //printf("coef[%d]=%f\n",i, coef[i]); }
coef[k + 1] += l_rate err yhat (1 - yhat) dataset[j][k] 上面的梯度更新公式式改进的?
你好,我们写代码的时候参考了这本书《machine learning algorithms from scratch》,书中使用了这个公式 b = b + learning_rate (y-yhat) yhat (1 - yhat) x,其中yhat是预测结果,y是lable。 (书中对应的公式是9.3在P71)
void coefficients_sgd(double ** dataset, int col, double *coef, double l_rate, int n_epoch, int train_size) { int i; for (i = 0; i < n_epoch; i++) { int j = 0;//±éÀúÿһÐÐ for (j = 0; j < train_size; j++) { double yhat = predict(col,dataset[j], coef); double err = dataset[j][col - 1] - yhat; coef[0] += l_rate * err * yhat * (1 - yhat); int k; for (k = 0; k < col - 1; k++) { coef[k + 1] += l_rate * err * yhat * (1 - yhat) * dataset[j][k]; } } } for (i = 0; i < col; i++) { //printf("coef[%d]=%f\n",i, coef[i]); }
}coef[k + 1] += l_rate err yhat (1 - yhat) dataset[j][k] 上面的梯度更新公式式改进的?