blu*_*sky 2 machine-learning neural-network deep-learning loss-function
来自 deeplearning.ai :
\n\n\n\n\n构建神经网络的一般方法是:
\n\n\n
\n- 定义神经网络结构(输入单元数、隐藏单元数等)。
\n- 初始化模型参数
\n- 循环:\n\n
\n\n
- 实现前向传播
\n- 计算损失
\n- 实现反向传播以获得梯度
\n- 更新参数(梯度下降)
\n
损失函数如何影响网络的学习方式?
\n\n例如,这是我对前向和反向传播的实现,我认为它是正确的,因为我可以使用以下代码训练模型以获得可接受的结果:
\n\n\n\nfor i in range(number_iterations):\n\n\n # forward propagation\n\n\n Z1 = np.dot(weight_layer_1, xtrain) + bias_1\n a_1 = sigmoid(Z1)\n\n Z2 = np.dot(weight_layer_2, a_1) + bias_2\n a_2 = sigmoid(Z2)\n\n mse_cost = np.sum(cost_all_examples)\n 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)))\n\n# Back propagation and gradient descent\n d_Z2 = np.multiply((a_2 - xtrain), d_sigmoid(a_2))\n d_weight_2 = np.dot(d_Z2, a_1.T)\n d_bias_2 = np.asarray(list(map(lambda x : [sum(x)] , d_Z2)))\n # perform a parameter update in the negative gradient direction to decrease the loss\n weight_layer_2 = weight_layer_2 + np.multiply(- learning_rate , d_weight_2)\n bias_2 = bias_2 + np.multiply(- learning_rate , d_bias_2)\n\n d_a_1 = np.dot(weight_layer_2.T, d_Z2)\n d_Z1 = np.multiply(d_a_1, d_sigmoid(a_1))\n d_weight_1 = np.dot(d_Z1, xtrain.T)\n d_bias_1 = np.asarray(list(map(lambda x : [sum(x)] , d_Z1)))\n weight_layer_1 = weight_layer_1 + np.multiply(- learning_rate , d_weight_1)\n bias_1 = bias_1 + np.multiply(- learning_rate , d_bias_1)\n
Run Code Online (Sandbox Code Playgroud)\n\n注意以下几行:
\n\nmse_cost = np.sum(cost_all_examples)\ncost_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)))\n
Run Code Online (Sandbox Code Playgroud)\n\n我可以使用 mse 损失或交叉熵损失来了解系统的学习情况。但这仅供参考,成本函数的选择不会影响网络的学习方式。我相信我不理解深度学习文献中经常提到的一些基本知识,即损失函数的选择是深度学习的重要一步?但如上面的代码所示,我可以选择交叉熵或 mse 损失,并且不会影响网络的学习方式,交叉熵或 mse 损失仅供参考?
\n\n更新 :
\n\n例如,下面是来自 deeplearning.ai 的一段计算成本的代码片段:
\n\n# GRADED FUNCTION: compute_cost\n\ndef compute_cost(A2, Y, parameters):\n """\n Computes the cross-entropy cost given in equation (13)\n\n Arguments:\n A2 -- The sigmoid output of the second activation, of shape (1, number of examples)\n Y -- "true" labels vector of shape (1, number of examples)\n parameters -- python dictionary containing your parameters W1, b1, W2 and b2\n\n Returns:\n cost -- cross-entropy cost given equation (13)\n """\n\n m = Y.shape[1] # number of example\n\n # Retrieve W1 and W2 from parameters\n ### START CODE HERE ### (\xe2\x89\x88 2 lines of code)\n W1 = parameters[\'W1\']\n W2 = parameters[\'W2\']\n ### END CODE HERE ###\n\n # Compute the cross-entropy cost\n ### START CODE HERE ### (\xe2\x89\x88 2 lines of code)\n logprobs = np.multiply(np.log(A2), Y) + np.multiply((1 - Y), np.log(1 - A2))\n cost = - np.sum(logprobs) / m\n ### END CODE HERE ###\n\n cost = np.squeeze(cost) # makes sure cost is the dimension we expect. \n # E.g., turns [[17]] into 17 \n assert(isinstance(cost, float))\n\n return cost\n
Run Code Online (Sandbox Code Playgroud)\n\n该代码按预期运行并实现了高精度/低成本。除了向机器学习工程师提供有关网络学习情况的信息之外,在此实现中不使用成本值。这让我质疑成本函数的选择如何影响神经网络的学习方式?
\n好吧,这只是一个粗略的高层尝试,旨在回答 SO 可能是一个偏离主题的问题(因为我原则上理解您的困惑)。
除了向机器学习工程师提供有关网络学习情况的信息之外,在此实现中不使用成本值。
这实际上是正确的;仔细阅读 Andrew Ng 的 Jupyter 笔记本,了解compute_cost
您发布的功能,您会看到:
5 - 成本函数
现在您将实现前向和后向传播。您需要计算成本,因为您想检查您的模型是否真正在学习。
从字面上看,这是在代码中显式计算成本函数实际值的唯一原因。
但这仅供参考,成本函数的选择不会影响网络的学习方式。
没那么快!这是(通常看不见的)陷阱:
成本函数的选择决定了用于计算dw
和db
数量的精确方程,从而决定了学习过程。
请注意,这里我谈论的是函数本身,而不是它的值。
换句话说,像你这样的计算
d_weight_2 = np.dot(d_Z2, a_1.T)
Run Code Online (Sandbox Code Playgroud)
和
d_weight_1 = np.dot(d_Z1, xtrain.T)
Run Code Online (Sandbox Code Playgroud)
不是从天上掉下来的,但它们是应用于特定成本函数的反向传播数学的结果。
以下是 Andrew 在 Coursera 上的入门课程中的一些相关高级幻灯片:
希望这可以帮助; 我们如何准确地从成本函数的导数开始计算特定形式的具体细节dw
超出db
了本文的范围,但您可以在网上找到一些关于反向传播的好教程(这里是一个)。
最后,对于当我们选择错误的成本函数(多类分类的二元交叉熵,而不是正确的分类交叉熵)时可能发生的情况的(非常)高级描述,您可以看看我的答案Keras binary_crossentropy 与 categorical_crossentropy 性能对比?。
归档时间: |
|
查看次数: |
1565 次 |
最近记录: |