来自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中计算成本的代码片段:
# 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该代码按预期运行,实现了高精度/低成本。在此实现中,除了向机器学习工程师提供关于网络学习情况的信息外,成本的价值并不是使用的。这让我质疑成本函数的选择如何影响神经网络的学习?
发布于 2018-07-20 11:51:37
嗯,这只是一个粗略的高层次的尝试,以回答什么可能是一个非主题的问题,所以(正如我理解你的困惑在原则上)。
在此实现中,除了向机器学习工程师提供关于网络学习情况的信息外,成本的价值并不是使用的。
这实际上是正确的;仔细阅读Andrew的木星笔记本,看看您发布的compute_cost函数,您会看到:
5-成本函数 现在,您将实现前向和后向传播。你需要计算成本,因为你想要检查你的模型是否真的在学习。
从字面上讲,这是显式计算代码中成本函数的实际值的唯一原因。
但这仅仅是为了信息目的,成本函数的选择并不影响网络学习的方式。
还没那么快!以下是(通常是不可见的)陷阱:
成本函数的选择决定了计算dw db 和的精确方程,从而给出了学习过程。
注意,这里我讨论的是函数本身,而不是它的值。
换句话说,像你这样的计算
d_weight_2 = np.dot(d_Z2, a_1.T)和
d_weight_1 = np.dot(d_Z1, xtrain.T)没有从天而降,但它们是应用于特定成本函数的反向传播数学的结果。
以下是安德鲁在古瑟拉的入门课程中的一些相关的高级幻灯片:



希望这会有所帮助;从成本函数的导数开始,我们如何准确地到达dw和db计算的特定形式的具体细节超出了本文的范围,但是您可以在反向传播在线上找到几个很好的教程(这里就是其中之一)。
最后,对于当我们选择错误的代价函数(多类分类的二进制交叉熵,而不是正确的分类交叉熵)时会发生什么(非常)高层次的描述,您可以看看我在交叉熵性能?上的答案。
https://stackoverflow.com/questions/51427144
复制相似问题