【发布时间】:2018-12-27 20:34:02
【问题描述】:
来自 deeplearning.ai:
构建神经网络的一般方法是:
- 定义神经网络结构(输入单元数、隐藏单元数等)。
- 初始化模型的参数
- 循环:
- 实现前向传播
- 计算损失
- 实施反向传播以获得梯度
- 更新参数(梯度下降)
损失函数如何影响网络的学习方式?
例如,这是我认为是正确的前向和反向传播的实现,因为我可以使用以下代码训练模型以获得可接受的结果:
for i in range(number_iterations):
# forward propagation
Z1 = np.dot(weight_layer_1, xtrain) + bias_1
a_1 = sigmoid(Z1)
Z2 = np.dot(weight_layer_2, a_1) + bias_2
a_2 = sigmoid(Z2)
mse_cost = np.sum(cost_all_examples)
cost_cross_entropy = -(1.0/len(X_train) * (np.dot(np.log(a_2), Y_train.T) + np.dot(np.log(1-a_2), (1-Y_train).T)))
# Back propagation and gradient descent
d_Z2 = np.multiply((a_2 - xtrain), d_sigmoid(a_2))
d_weight_2 = np.dot(d_Z2, a_1.T)
d_bias_2 = np.asarray(list(map(lambda x : [sum(x)] , d_Z2)))
# perform a parameter update in the negative gradient direction to decrease the loss
weight_layer_2 = weight_layer_2 + np.multiply(- learning_rate , d_weight_2)
bias_2 = bias_2 + np.multiply(- learning_rate , d_bias_2)
d_a_1 = np.dot(weight_layer_2.T, d_Z2)
d_Z1 = np.multiply(d_a_1, d_sigmoid(a_1))
d_weight_1 = np.dot(d_Z1, xtrain.T)
d_bias_1 = np.asarray(list(map(lambda x : [sum(x)] , d_Z1)))
weight_layer_1 = weight_layer_1 + np.multiply(- learning_rate , d_weight_1)
bias_1 = bias_1 + np.multiply(- learning_rate , d_bias_1)
注意以下几行:
mse_cost = np.sum(cost_all_examples)
cost_cross_entropy = -(1.0/len(X_train) * (np.dot(np.log(a_2), Y_train.T) + np.dot(np.log(1-a_2), (1-Y_train).T)))
我可以使用 mse 损失或交叉熵损失来了解系统的学习情况。但这仅供参考,成本函数的选择不会影响网络的学习方式。我相信我没有像深度学习文献中所说的那样理解基本的东西,损失函数的选择是深度学习的重要一步?但如我上面的代码所示,我可以选择交叉熵或 mse 损失,并且不会影响网络的学习方式,交叉熵或 mse 损失仅用于信息目的?
更新:
例如,这里是一个来自 deeplearning.ai 的计算成本的 sn-p 代码:
# GRADED FUNCTION: compute_cost
def compute_cost(A2, Y, parameters):
"""
Computes the cross-entropy cost given in equation (13)
Arguments:
A2 -- The sigmoid output of the second activation, of shape (1, number of examples)
Y -- "true" labels vector of shape (1, number of examples)
parameters -- python dictionary containing your parameters W1, b1, W2 and b2
Returns:
cost -- cross-entropy cost given equation (13)
"""
m = Y.shape[1] # number of example
# Retrieve W1 and W2 from parameters
### START CODE HERE ### (≈ 2 lines of code)
W1 = parameters['W1']
W2 = parameters['W2']
### END CODE HERE ###
# Compute the cross-entropy cost
### START CODE HERE ### (≈ 2 lines of code)
logprobs = np.multiply(np.log(A2), Y) + np.multiply((1 - Y), np.log(1 - A2))
cost = - np.sum(logprobs) / m
### END CODE HERE ###
cost = np.squeeze(cost) # makes sure cost is the dimension we expect.
# E.g., turns [[17]] into 17
assert(isinstance(cost, float))
return cost
此代码按预期运行并实现高精度/低成本。除了向机器学习工程师提供有关网络学习情况的信息外,此实现中不使用成本值。这让我质疑成本函数的选择如何影响神经网络的学习方式?
【问题讨论】:
-
我投票结束这个问题,因为这个问题是关于人工神经网络理论的。但简短的回答。损失函数是影响网络如何以及是否学习的非常重要的因素。我真的很喜欢这个教程。 neuralnetworksanddeeplearning.com
-
@Framester 我也喜欢这个教程并认为答案在neuralnetworksanddeeplearning.com/chap3.html 部分,也许我的误解是如果改变成本函数那么激活函数也必须改变?在我上面的示例中,虽然可以更改损失函数值,但它不会产生影响,因为也不会更改激活函数。损失函数的梯度是否等于sigmoid函数的梯度?
-
您是否了解损失函数的一般机制:它如何影响参数更新?我读到您的问题是询问损失函数的选择,而不是 any 损失函数的效果。
-
@Prune 我了解成本函数衡量网络的训练效果。但我不明白它如何影响参数更新。正如我最初的问题一样,如果我使用 MSE 成本而不是交叉熵,它对网络的学习方式没有影响。也许成本函数的选择会影响激活函数的选择?换句话说,如果我改变成本函数,我上面提到的网络的另一部分是否也需要改变,以纳入成本函数的变化。我也更新了问题。谢谢。
-
感谢您的澄清;我看到它给你的答案比我给出的更完整。
标签: machine-learning neural-network deep-learning loss-function