python - 为什么我的 L1 正则化实现效果不佳?

标签 python neural-network regularized

我正在学习有关神经网络的在线教程,neuralnetworksanddeeplearning.com作为本教程的一部分,作者 Nielsen 在代码中实现了 L2 正则化。现在他要求我们修改代码,使用 L1 正则化而不是 L2。这个link将带您直接进入我正在谈论的教程部分。

使用随机梯度下降的 L2 正则化的权重更新规则如下: Weight update-rule with L2-regularization

尼尔森在 python 中实现了它,如下所示:

self.weights = [(1-eta*(lmbda/n))*w-(eta/len(mini_batch))*nw
                for w, nw in zip(self.weights, nabla_w)]

L1 正则化的更新规则变为:

Weight update-rule with L1- regularization

我尝试按如下方式实现它:

self.weights = [(w - eta* (lmbda/len(mini_batch)) * np.sign(w) - (eta/len(mini_batch)) * nw)
                 for w, nw in zip(self.weights, nabla_w)]        

突然间,我的神经网络的分类精度为 +- 机会... 怎么会这样? 我在实现 L1 正则化时犯了错误吗?我有一个包含 30 个隐藏神经元的神经网络,学习率为 0.5,lambda = 5.0。当我使用 L2 正则化时,一切都很好。

为了您的方便,请在此处找到完整的更新功能:

def update_mini_batch(self, mini_batch, eta, lmbda, n):
    """Update the network's weights and biases by applying gradient
    descent using backpropagation to a single mini batch.  The
    ``mini_batch`` is a list of tuples ``(x, y)``, ``eta`` is the
    learning rate, ``lmbda`` is the regularization parameter, and
    ``n`` is the total size of the training data set.

    """
    nabla_b = [np.zeros(b.shape) for b in self.biases]
    nabla_w = [np.zeros(w.shape) for w in self.weights]
    for x, y in mini_batch:
        delta_nabla_b, delta_nabla_w = self.backprop(x, y)
        nabla_b = [nb+dnb for nb, dnb in zip(nabla_b, delta_nabla_b)]
        nabla_w = [nw+dnw for nw, dnw in zip(nabla_w, delta_nabla_w)]
    self.weights = [(1-eta*(lmbda/n))*w-(eta/len(mini_batch))*nw      
                    for w, nw in zip(self.weights, nabla_w)]
    self.biases = [b-(eta/len(mini_batch))*nb
                   for b, nb in zip(self.biases, nabla_b)]

最佳答案

你的数学计算错了。您要实现的公式的代码翻译为:

self.weights = [
    (w - eta * (lmbda / n) * np.sign(w) - eta * nabla_b[0])
    for w in self.weights]

两个必需的修改是:

  • 消除对小批量大小的依赖
  • 仅使用第一个nabla系数

关于python - 为什么我的 L1 正则化实现效果不佳?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57127373/

相关文章:

python - 在 Python 中使用 egms 评估大地水准面高度

python - 如何在读取文件内容时写入文本文件的中间部分?

matlab - 为什么神经网络的准确性不好?

python - 神经网络收敛到零输出

python - 如何在 python 中添加 L1 规范化?

machine-learning - L1-正则化 : Where to use penalized cost function?

python - 查找列表中的字典项

python - Flask:使用 unittest 进行测试 - 如何从响应中获取 .post json

neural-network - 使用相同的数据集重复训练

python - 如何在 CNTK 中应用自定义正则化(使用 python)?