Normalizing rows with Python

机器学习中数据标准化技巧
本文探讨了在机器学习和深度学习中数据标准化的重要性和实施方法。标准化数据能加速梯度下降过程,提升模型性能。文章详细介绍了如何通过计算矩阵各行的范数并进行归一化处理来实现这一目标。

A common technique we use in Machine Learning and Deep Learning is to normalize our data. It often leads to a better performance because gradient descent converges faster after normalization. Here, by normalization we mean changing x to :
在这里插入图片描述
(dividing each row vector of x by its norm).

For example, if (3)x=[034264]x = \begin{bmatrix} 0 & 3 & 4 \\ 2 & 6 & 4 \\ \end{bmatrix}\tag{3}x=[023644](3) then (4)∥x∥=np.linalg.norm(x,axis=1,keepdims=True)=[556]\| x\| = np.linalg.norm(x, axis = 1, keepdims = True) = \begin{bmatrix} 5 \\ \sqrt{56} \\ \end{bmatrix}\tag{4} x=np.linalg.norm(x,axis=1,keepdims=True)=[556](4)and (5)x_normalized=x∥x∥=[03545256656456] x\_normalized = \frac{x}{\| x\|} = \begin{bmatrix} 0 & \frac{3}{5} & \frac{4}{5} \\ \frac{2}{\sqrt{56}} & \frac{6}{\sqrt{56}} & \frac{4}{\sqrt{56}} \\ \end{bmatrix}\tag{5}x_normalized=xx=[05625356654564](5)
Exercise: Implement normalizeRows() to normalize the rows of a matrix. After applying this function to an input matrix x, each row of x should be a vector of unit length (meaning length 1).

implement

# GRADED FUNCTION: normalizeRows

def normalizeRows(x):
    """
    Implement a function that normalizes each row of the matrix x (to have unit length).
    
    Argument:
    x -- A numpy matrix of shape (n, m)
    
    Returns:
    x -- The normalized (by row) numpy matrix. You are allowed to modify x.
    """
    
    ### START CODE HERE ### (≈ 2 lines of code)
    # Compute x_norm as the norm 2 of x. Use np.linalg.norm(..., ord = 2, axis = ..., keepdims = True)
    x_norm = np.linalg.norm(x,axis=1,keepdims=True)
    
    # Divide x by its norm.
    x = x/x_norm
    ### END CODE HERE ###

    return x
x = np.array([
    [0, 3, 4],
    [1, 6, 4]])
print("normalizeRows(x) = "   str(normalizeRows(x)))

output:
在这里插入图片描述

Python中,有几个常用的库可以用来实现归一化流(normalizing flow),以下介绍使用`torch`和`nflows`库实现归一化流的方法。 ### 使用`torch`库实现简单的归一化流 `torch`是一个强大的深度学习库,可以用来构建自定义的归一化流模型。以下是一个简单的示例,展示了如何使用`torch`实现一个简单的平面流(Planar Flow): ```python import torch import torch.nn as nn # 定义平面流层 class PlanarFlow(nn.Module): def __init__(self, dim): super(PlanarFlow, self).__init__() self.u = nn.Parameter(torch.randn(dim)) self.w = nn.Parameter(torch.randn(dim)) self.b = nn.Parameter(torch.randn(1)) def forward(self, z): # 计算平面流变换 uw = torch.dot(self.u, self.w) m_uw = -1 + torch.log(1 + torch.exp(uw)) u_hat = self.u + (m_uw - uw) * self.w / torch.norm(self.w) ** 2 zwb = torch.mv(z, self.w) + self.b h_zwb = torch.tanh(zwb).unsqueeze(1) z_new = z + u_hat * h_zwb # 计算雅可比行列式的对数 psi = (1 - h_zwb ** 2) * self.w det = 1 + torch.mv(psi, u_hat) log_det = torch.log(torch.abs(det)) return z_new, log_det # 示例使用 dim = 2 flow = PlanarFlow(dim) z = torch.randn(10, dim) z_new, log_det = flow(z) print("Transformed z:", z_new) print("Log determinant:", log_det) ``` ### 使用`nflows`库实现归一化流 `nflows`是一个专门用于实现归一化流的Python库,它提供了许多预定义的流层和模型架构,使用起来更加方便。以下是一个使用`nflows`库实现简单归一化流的示例: ```python from nflows.flows.base import Flow from nflows.distributions.normal import StandardNormal from nflows.flows.autoregressive import MaskedAutoregressiveFlow import torch # 定义流的维度 dim = 2 # 定义基础分布 base_dist = StandardNormal(shape=[dim]) # 定义流模型 flow = MaskedAutoregressiveFlow(features=dim) # 生成样本 num_samples = 10 samples = flow.sample(num_samples) print("Generated samples:", samples) ``` 在上述示例中,首先定义了基础分布为标准正态分布,然后使用`MaskedAutoregressiveFlow`构建了一个自回归流模型。最后,使用该模型生成了一些样本。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值