【八股文-机器学习】dropout

工作原理

图片说明
假如针对上图1的神经网络存在过拟合,dropout通过在训练的过程中随机丢掉部分神经元来减小神经网络的规模从而防止过拟合。

在训练过程中每个iteration,我们随机丢掉部分神经元,针对网络的每一层设置消除神经网络中节点的概率,例如我们设置均为0.5,如下图2所示,并且标记有红色X的的神经元被丢弃,然后删除掉从该节点进出的连线,最后得到一个节点更少,规模更小的网络,那么这次训练过程中的网络结构会简化为图3所示的样子。

图片说明
图片说明

在实施dropout时,我们最常用的方法是inverted dropout(反向dropout),假设采用一个三层网络结构来进行举例说明,对第三层的神经元如何进行dropout。

图片说明

Dropout为什么会reduce overfit

Dropout可以随机删除网络中的神经单元,他为什么可以通过正则化发挥如此大的作用呢?

直观上理解:对于单个神经元来说,不要依赖于任何一个特征,因为该单元的输入可能随时被清除,因此该单元通过这种方式传播下去,并为单元的四个输入增加一点权重,通过传播所有权重,dropout将产生收缩权重的平方范数的效果,和之前讲的正则化类似;实施dropout的结果实它会压缩权重,并完成一些预防过拟合的外层正则化;对不同权重的衰减是不同的,它取决于激活函数倍增的大小。

总结一下,dropout的功能类似于正则化,与正则化不同的是应用方式不同会带来一点点小变化,甚至更适用于不同的输入范围。

在dropout中,我们允许对每一层的神经元dropout可以采用不同的保留概率keep-prob,通常针对可能出现过拟合,且含有诸多参数的层,可以把keep-prob设置成比较小的值,以便应用更强大的dropout,例如如图所示的网络中,第二层隐藏层是参数最多的,最容易发生拟合,因此设置keep-prob=0.5,对于后面的层,keep-prob更大,如果keep-prob=1.0,表示不应用dropout
图片说明

前传和反传

# GRADED FUNCTION: forward_propagation_with_dropout

def forward_propagation_with_dropout(X, parameters, keep_prob = 0.5):
    """
    Implements the forward propagation: LINEAR -> RELU + DROPOUT -> LINEAR -> RELU + DROPOUT -> LINEAR -> SIGMOID.

    Arguments:
    X -- input dataset, of shape (2, number of examples)
    parameters -- python dictionary containing your parameters "W1", "b1", "W2", "b2", "W3", "b3":
                    W1 -- weight matrix of shape (20, 2)
                    b1 -- bias vector of shape (20, 1)
                    W2 -- weight matrix of shape (3, 20)
                    b2 -- bias vector of shape (3, 1)
                    W3 -- weight matrix of shape (1, 3)
                    b3 -- bias vector of shape (1, 1)
    keep_prob - probability of keeping a neuron active during drop-out, scalar

    Returns:
    A3 -- last activation value, output of the forward propagation, of shape (1,1)
    cache -- tuple, information stored for computing the backward propagation
    """

    np.random.seed(1)

    # retrieve parameters
    W1 = parameters["W1"]
    b1 = parameters["b1"]
    W2 = parameters["W2"]
    b2 = parameters["b2"]
    W3 = parameters["W3"]
    b3 = parameters["b3"]

    # LINEAR -> RELU -> LINEAR -> RELU -> LINEAR -> SIGMOID
    Z1 = np.dot(W1, X) + b1
    A1 = relu(Z1)
    ### START CODE HERE ### (approx. 4 lines)         # Steps 1-4 below correspond to the Steps 1-4 described above. 
    D1 = np.random.rand(A1.shape[0],A1.shape[1])      # Step 1: initialize matrix D1 = np.random.rand(..., ...)
    D1 = D1 < keep_prob                               # Step 2: convert entries of D1 to 0 or 1 (using keep_prob as the threshold)
    A1 = np.multiply(A1, D1)                          # Step 3: shut down some neurons of A1
    A1 = A1 / keep_prob                               # Step 4: scale the value of neurons that haven't been shut down
    ### END CODE HERE ###
    Z2 = np.dot(W2, A1) + b2
    A2 = relu(Z2)
    ### START CODE HERE ### (approx. 4 lines)
    D2 = np.random.rand(A2.shape[0],A2.shape[1])       # Step 1: initialize matrix D2 = np.random.rand(..., ...)
    D2 = D2 < keep_prob                                # Step 2: convert entries of D2 to 0 or 1 (using keep_prob as the threshold)
    A2 = np.multiply(A2, D2)                           # Step 3: shut down some neurons of A2
    A2 = A2 / keep_prob                                # Step 4: scale the value of neurons that haven't been shut down
    ### END CODE HERE ###
    Z3 = np.dot(W3, A2) + b3
    A3 = sigmoid(Z3)

    cache = (Z1, D1, A1, W1, b1, Z2, D2, A2, W2, b2, Z3, A3, W3, b3)

    return A3, cache

反传这里分为两步:

1.在前向传播中将dropout向量D应用到A1上,这里在后向传播时需要将向量D应用到d A1上;
2.在前向传播中将 A1/keepprob, 在反向传播时需要d A1/keep_prob

# GRADED FUNCTION: backward_propagation_with_dropout

def backward_propagation_with_dropout(X, Y, cache, keep_prob):
    """
    Implements the backward propagation of our baseline model to which we added dropout.

    Arguments:
    X -- input dataset, of shape (2, number of examples)
    Y -- "true" labels vector, of shape (output size, number of examples)
    cache -- cache output from forward_propagation_with_dropout()
    keep_prob - probability of keeping a neuron active during drop-out, scalar

    Returns:
    gradients -- A dictionary with the gradients with respect to each parameter, activation and pre-activation variables
    """

    m = X.shape[1]
    (Z1, D1, A1, W1, b1, Z2, D2, A2, W2, b2, Z3, A3, W3, b3) = cache

    dZ3 = A3 - Y
    dW3 = 1./m * np.dot(dZ3, A2.T)
    db3 = 1./m * np.sum(dZ3, axis=1, keepdims = True)
    dA2 = np.dot(W3.T, dZ3)
    ### START CODE HERE ### (≈ 2 lines of code)
    dA2 = np.multiply(dA2, D2)         # Step 1: Apply mask D2 to shut down the same neurons as during the forward propagation
    dA2 = dA2 / keep_prob              # Step 2: Scale the value of neurons that haven't been shut down
    ### END CODE HERE ###
    dZ2 = np.multiply(dA2, np.int64(A2 > 0))
    dW2 = 1./m * np.dot(dZ2, A1.T)
    db2 = 1./m * np.sum(dZ2, axis=1, keepdims = True)

    dA1 = np.dot(W2.T, dZ2)
    ### START CODE HERE ### (≈ 2 lines of code)
    dA1 = np.multiply(dA1, D1)         # Step 1: Apply mask D1 to shut down the same neurons as during the forward propagation
    dA1 = dA1 / keep_prob              # Step 2: Scale the value of neurons that haven't been shut down
    ### END CODE HERE ###
    dZ1 = np.multiply(dA1, np.int64(A1 > 0))
    dW1 = 1./m * np.dot(dZ1, X.T)
    db1 = 1./m * np.sum(dZ1, axis=1, keepdims = True)

    gradients = {"dZ3": dZ3, "dW3": dW3, "db3": db3,"dA2": dA2,
                 "dZ2": dZ2, "dW2": dW2, "db2": db2, "dA1": dA1, 
                 "dZ1": dZ1, "dW1": dW1, "db1": db1}

    return gradients
全部评论
采用dropout方法,训练会随机丢弃神经元,为了不影响期望,我们会对该层参与训练的神经元的权重进行rescale从而保证最终的数学期望波动不要过大,这样我们在最终预测inference的阶段就不需要进行rescale操作了,因为inference阶段我们对所有神经元进行保留。
点赞 回复 分享
发布于 2020-12-29 11:57
注意要学会手写反传代码
点赞 回复 分享
发布于 2020-12-28 11:04

相关推荐

机智的大学生这就开摆:有些地方描述有点儿空泛。传感器直接说清是哪款,要不然对方都不知道问啥。然后freertos那里的描述也是,加上freertos就实现了实时性吗?可以说基于freertos的调度,结合xxx优化/xxx技术实现了xxx检测的实时性。同时freertos也能实现异步io,也能实现灵活的并行架构和高并发。 检测时效性的问题要考虑哪里需要时效性,摔倒检测需要通过中断实现,至于温度这种大惯量就不需要,方案细节理清楚。然后freertos那里提升响应速度用的消息队列不是太合适,可以说是用的notify反正和消息队列差不多,或者说指针传参来优化消息队列的值传递进而提升线程间通信效率。 但凡是搞过开发的看一眼你的简历就知道有问题了,需要重新整理技术路线优化一下。首先搞清楚技术路线,描述好软硬框架,体现性能优化/低功耗等。例如后续针对xxx功能进行了xxx的优化,实现了xxx成果(这里可以展开描述你使用的工具链,你是用的串口调试助手还是RTT来打印日志,有没有用systemview来优化任务的优先级大小避免乱序调度,或者使用了Ozone调试定位问题等)。 描述问题上例如视觉项目那里,基于xxx通信接口编写xxx传感器的驱动,实现了xxx功能,并提供了xxx数据处理/稳定性/精确度/异步调用。或者也可以统一描述为基于xxx、xxx、xxx通讯协议编写xxx、xxx、xxx传感器的驱动。后续再单独给这些传感器的数据处理/优化等进行描述。 最重要的一点,实习栏太少了,五六个月不至于只干了这么点,把实习项目描述清楚,把自己会的不管有没有让你做都写进去。你写了实习这栏,内容就不能比剩下的两个项目差多少了。 最后就是看看岗位jd,跟着描述修改你的专业技能以及项目的描述,这样被回复的概率才大。 希望你早日找到理想工作!祝好
点赞 评论 收藏
分享
评论
点赞
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务