我正在使用Python和Boston Housing数据集从头开始实现线性回归模型。我创建了一个函数假设
def hypothesis(W, X, b):
return np.dot(W, X) + b
和一个将权重和b初始化为0的函数
def initialize(X):
n = X.shape[0]
W = np.zeros((1, n))
b = 0
return W, b
我的成本函数是:
def cost(Y, Y_hat):
m = Y.shape[1]
error = (1 / m) * np.sum((Y_hat - Y) ** 2)
return error
我的梯度函数是:
def gradient(Y, Y_hat, X):
m = Y.shape[1]
dW = (1 / (2 * m)) * np.sum((Y_hat - Y) * X)
db = (1 / (2 * m)) * np.sum(Y_hat - Y)
#print('db' + str(db))
#print('dW: ' + str(dW))
return dW, db
,它返回dW和db,稍后将用于梯度下降。
我创建了一个名为模型的函数,该函数可以完成上述所有操作。
def model(x_train, y_train, learning_rate=0.001, epochs=1000):
W, b = initialize(x_train)
costs = []
epo = []
n, m = x_train.shape
for i in range(0, epochs):
y_hat = hypothesis(W, x_train, b)
)
c = cost(y_train, y_hat)
dW, db = gradient(y_train, y_hat, x_train)
# Update
for j in range(0, n):
W[0, j] = W[0, j] - learning_rate * dW
b = b - learning_rate * db
if(i % 100 == 0):
print('Cost on epoch ' + str(i) + ': ' + str(c))
costs.append(c)
epo.append(i)
plt.plot(costs, epo)
print('Test accuracy')
correct = 0
for i in range(0, m):
if y_hat[0, i] == y_train[0, i]:
correct += 1
print('correct: ' + str(correct) + '/' + str(m))
但是,它输出我在第0阶段的学习率是760000,并且下降了,但是几乎是恒定的。时代100的下一个成本将比以前的成本小几百个,这确实是一个很高的成本。我检查了另一个数据集,发现的问题似乎相似,因此得出结论,问题可能不在数据集中,而在我的编码上。我的代码中可能有什么错误?谢谢。