为什么我的批量梯度下降在线性回归中不收敛?

我想编写一个类似于sklearn.linear_model.LinearRegression的LinearRegression模型。首先,我使用sklearn.linear_model.LinearRegression训练了一个标准的线性回归模型:

import numpy as npimport matplotlib.pyplot as pltfrom sklearn import linear_model# 训练数据train_x = np.array([1,2,3,4,5,6], dtype=np.float64).reshape(6,1)train_y = np.array([1,2,3,3.25,3.5,3.8], dtype=np.float64)# 测试数据predict_x = np.arange(0, 7, 0.1)predict_x = predict_x.reshape(predict_x.size, 1)# 简单回归model1 = linear_model.LinearRegression()model1.fit(train_x, train_y);print model1.coef_, model1.intercept_# 二次回归model2 = linear_model.LinearRegression()model2.fit(np.concatenate((train_x, train_x**2), axis=1), train_y);print model2.coef_, model2.intercept_# 五阶多项式回归model5 = linear_model.LinearRegression()model5.fit(np.concatenate((train_x, train_x**2, train_x**3, train_x**4, train_x**5), axis=1), train_y);print model5.coef_, model5.intercept_# 预测predict_y1 = model1.predict(predict_x)predict_y2 = model2.predict(np.concatenate((predict_x, predict_x**2), axis=1))predict_y5 = model5.predict(np.concatenate((predict_x, predict_x**2, predict_x**3, predict_x**4, predict_x**5), axis=1))# 绘图plt.figure(figsize = (10,10))plt.scatter(train_x, train_y, color='black')plt.plot(predict_x, predict_y1, color='blue', label='欠拟合')plt.plot(predict_x, predict_y2, color='green', label='适度拟合')plt.plot(predict_x, predict_y5, color='red', label='过拟合')plt.axis([0,7,0,5])plt.legend(loc=2)plt.show()

然后,我得到了很好的结果:

[ 0.53571429] 0.883333333333

[ 1.34821429 -0.11607143] -0.2

[-8.52333333 7.0625 -2.30833333 0.3375 -0.01833333] 4.45

Result of <code>sklearn.linear_model.LinearRegression</code>“></a></p>
<p>之后,我实现了我的模型<code>MyLinearRegression</code>。首先,我选择批量梯度下降和固定的迭代次数来测试我的代码是否正确。</p>
<pre class=# 中心化数据def center_matrix(X): assert(isinstance(X, np.ndarray)) X_offset = np.average(X, axis=0) return X - X_offset, X_offsetclass MyLinearRegression(object): def __init__(self): self.coef_ = None self.intercept_ = None self.learning_rate = None def fit(self, X, y): n_samples, n_features = X.shape n_samples_, = y.shape assert(n_samples == n_samples_) X, X_offset = center_matrix(X) y, y_offset = center_matrix(y) self.coef_ = np.ones((n_features,), dtype=np.float64) self.learning_rate = -0.0001 error = None # 使用固定的迭代次数 for epoch in np.arange(500000): y_hat = X.dot(self.coef_) error_ = y_hat-y if error is not None and sum(error_**2) > sum(error**2): # 如果平方误差增加,则将学习率减半。 self.learning_rate /= 2. continue error = error_ coef = self.coef_ + self.learning_rate * (X.T.dot(error)) if np.isfinite(coef).all(): # 如果发生溢出,则将学习率减半。 self.coef_ = coef else: self.learning_rate /= 2. self.intercept_ = y_offset - self.coef_.dot(X_offset.T) return self def predict(self, X): n_samples, n_features = X.shape assert(n_features == self.coef_.size) return X.dot(self.coef_) + self.intercept_# 简单回归my_model1 = MyLinearRegression()my_model1.fit(train_x, train_y)print my_model1.coef_, my_model1.intercept_# 二次回归my_model2 = MyLinearRegression()my_model2.fit(np.concatenate((train_x, train_x**2), axis=1), train_y);print my_model2.coef_, my_model2.intercept_# 五阶多项式回归my_model5 = MyLinearRegression()my_model5.fit(np.concatenate((train_x, train_x**2, train_x**3, train_x**4, train_x**5), axis=1), train_y);print my_model5.coef_, my_model5.intercept_# 预测my_predict_y1 = my_model1.predict(predict_x)my_predict_y2 = my_model2.predict(np.concatenate((predict_x, predict_x**2), axis=1))my_predict_y5 = my_model5.predict(np.concatenate((predict_x, predict_x**2, predict_x**3, predict_x**4, predict_x**5), axis=1))# 绘图plt.figure(figsize = (10,10))plt.scatter(train_x, train_y, color='black')plt.plot(predict_x, my_predict_y1, color='blue', label='欠拟合')plt.plot(predict_x, my_predict_y2, color='green', label='适度拟合')plt.plot(predict_x, my_predict_y5, color='red', label='过拟合')plt.axis([0,7,0,5])plt.legend(loc=2)plt.show()

然后,我得到了不好的结果:

[ 0.53571433] 0.883333191266

[ 1.34821275 -0.11607122] -0.199997815791

[ -1.95681250e+00 -2.20847875e+01 -1.48602362e+02 -9.20144807e+02 -5.56577136e+03] 11678151.1386

Result of MyLinearRegression

我可以在MyLinearRegression上的my_model1my_model2中得到好的结果,它们与sklearn.linear_model.LinearRegression上的结果接近。但是,无论我如何调整learning_rate和迭代次数,my_model5就是不收敛。谁能帮我解决这个问题?


回答:

这里需要进行特征缩放。

参见:

Related Posts

Keras Dense层输入未被展平

这是我的测试代码: from keras import…

无法将分类变量输入随机森林

我有10个分类变量和3个数值变量。我在分割后直接将它们…

如何在Keras中对每个输出应用Sigmoid函数?

这是我代码的一部分。 model = Sequenti…

如何选择类概率的最佳阈值?

我的神经网络输出是一个用于多标签分类的预测类概率表: …

在Keras中使用深度学习得到不同的结果

我按照一个教程使用Keras中的深度神经网络进行文本分…

‘MatMul’操作的输入’b’类型为float32,与参数’a’的类型float64不匹配

我写了一个简单的TensorFlow代码,但不断遇到T…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注