【发布时间】:2021-10-23 10:31:24
【问题描述】:
我正在尝试使用二元交叉熵损失函数从头开始实现逻辑回归。下面实现的损失函数是基于以下公式创建的。
def binary_crossentropy(y, yhat):
no_of_samples = len(y)
numerator_1 = y*np.log(yhat)
numerator_2 = (1-y) * np.log(1-yhat)
loss = -(np.sum(numerator_1 + numerator_2) / no_of_samples)
return loss
下面是我如何使用梯度下降实现训练。
L = 0.01
epochs = 40000
no_of_samples = len(x)
# Keeping track of the loss
loss = []
for _ in range(epochs):
yhat = sigmoid(x*weight + bias)
# Finding out the loss of each iteration
loss.append(binary_crossentropy(y, yhat))
d_weight = np.sum(x *(yhat-y)) / no_of_samples
d_bias = np.sum(yhat-y) / no_of_samples
weight = weight - L*d_weight
bias = bias - L*d_bias
由于权重和偏差得到了适当的调整,上述训练进行得很好。但我的问题是,为什么损失图看起来波动很大?
我的逻辑回归实现中有什么不正确的地方吗?如果我的实现已经正确,为什么会这样波动?
【问题讨论】:
标签: python machine-learning linear-regression logistic-regression