神经网络学习小记录31——l1、l2标准化及其python代码实现

神经网络学习小记录31——l1、l2标准化及其python代码实现

学习前言

最近看了一下facenet,发现自己居然连l2标准化都不知道,写篇文章水一下。
在这里插入图片描述

什么是l1、l2标准化

1、范数

我们需要知道的范数主要是两种:

1-范数:
∣ ∣ x ∣ ∣ 1 = ∑ i = 1 N ∣ x i ∣ ||x||_1 = \sum_{i=1}^N|x_i| x1=i=1Nxi,即向量元素绝对值之和 。

2-范数:
∣ ∣ x ∣ ∣ 2 = ∑ i = 1 N x i 2 ||\textbf{x}||_2 =\sqrt{\sum_{i=1}^Nx_i^2} x2=i=1Nxi2 ,Euclid范数(欧几里得范数,常用计算向量长度),即向量元素绝对值的平方和再开方。

2、l1标准化和l2标准化

其实概念并不复杂,实际上是:

L1标准化:每个元素/L1范数;
L2标准化:每个元素/L2范数;

python代码实现

import numpy as np
#---------------------------------#
#   l1标准化
#---------------------------------#
def l1_normalize(x, epsilon=1e-10):
    output = x / np.maximum(np.sum(np.abs(x)),epsilon)
    return output
#---------------------------------#
#   l2标准化
#---------------------------------#
def l2_normalize(x, axis=-1, epsilon=1e-10):
    output = x / np.sqrt(np.maximum(np.sum(np.square(x), axis=axis, keepdims=True),epsilon))
    return output

a = np.arange(12).reshape([1,3,4])
l1 = l1_normalize(a)
l2 = l2_normalize(a,(1,2))
print(a,"\n")
print(l1,"\n")
print(l2,"\n")

结果如下:

[[[ 0  1  2  3]
  [ 4  5  6  7]
  [ 8  9 10 11]]] 

[[[0.         0.01515152 0.03030303 0.04545455]
  [0.06060606 0.07575758 0.09090909 0.10606061]
  [0.12121212 0.13636364 0.15151515 0.16666667]]]

[[[0.         0.04445542 0.08891084 0.13336627]
  [0.17782169 0.22227711 0.26673253 0.31118796]
  [0.35564338 0.4000988  0.44455422 0.48900965]]]
  • 12
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
下面是用梯度下降算法实现Logistic回归的L1正则化和L2正则化的Python代码: ```python import numpy as np class LogisticRegression: def __init__(self, lr=0.1, num_iter=1000, fit_intercept=True, regularization=None, lambda_=0.1): self.lr = lr self.num_iter = num_iter self.fit_intercept = fit_intercept self.regularization = regularization self.lambda_ = lambda_ def __add_intercept(self, X): intercept = np.ones((X.shape[0], 1)) return np.concatenate((intercept, X), axis=1) def __sigmoid(self, z): return 1 / (1 + np.exp(-z)) def __loss(self, h, y): return (-y * np.log(h) - (1 - y) * np.log(1 - h)).mean() def __l1_regularization(self, w): return self.lambda_ * np.abs(w[1:]).sum() def __l2_regularization(self, w): return self.lambda_ * np.sum(w[1:] ** 2) def fit(self, X, y): if self.fit_intercept: X = self.__add_intercept(X) self.theta = np.zeros(X.shape[1]) for i in range(self.num_iter): z = np.dot(X, self.theta) h = self.__sigmoid(z) if self.regularization == 'l1': # L1正则化 grad = np.dot(X.T, (h - y)) / y.size + self.lambda_ * np.sign(self.theta) elif self.regularization == 'l2': # L2正则化 grad = np.dot(X.T, (h - y)) / y.size + self.lambda_ * self.theta else: grad = np.dot(X.T, (h - y)) / y.size self.theta -= self.lr * grad def predict_prob(self, X): if self.fit_intercept: X = self.__add_intercept(X) return self.__sigmoid(np.dot(X, self.theta)) def predict(self, X, threshold=0.5): return self.predict_prob(X) >= threshold ``` 其中,lr是学习率,num_iter是迭代次数,fit_intercept表示是否拟合截距,regularization表示正则化方法,lambda_是正则化系数。在fit方法中,通过判断regularization的取值,来实现L1正则化和L2正则化。在L1正则化中,使用np.sign函数计算符号函数,而在L2正则化中,直接对参数的平方和进行惩罚。在predict_prob方法中,对X进行截距拟合和sigmoid变换,返回预测概率。在predict方法中,对预测概率进行阈值处理,返回预测结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Bubbliiiing

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值