dim的理解、初始化函数、cat()、empty()、Adam优化器、optim.lr_scheduler、CrossEntropyLoss和LabelSmoothing详解、KL散度

目录

1 Pytorch中dim的理解

2 Pytorch中的初始化函数

3 torch.cat()函数

4 torch.empty()函数

5 Adam优化器详解

6 使用torch.optim.lr_scheduler调整学习率

7 CrossEntropyLoss和LabelSmoothing详解

8 KL散度的两种调用方法和输入要求


1 Pytorch中dim的理解

总结:

(1) dim表示维度,每一个dim的size看其下一个dim中有几个并列的括号对,例如:

x = torch.randn(2, 3, 3)

print(x)
print(x.size())
print(x.dim())

输出:

tensor([[[-1.6943, -2.1487,  1.2332],
         [-0.2261, -0.1596,  1.5513],
         [ 2.0383, -0.6982, -2.1481]],

        [[ 0.4201, -2.7373,  0.2424],
         [-1.1152,  1.3682, -1.8322],
         [ 0.1957, -0.2920,  0.1845]]])
torch.Size([2, 3, 3])
3

这里,第一个dim的size是2,是因为第二个dim中有两个并列的括号对,即:

x = torch.randn(2, 3, 3)
print(x)

for i in x:
    print(i)
    print(i.size())

输出:

tensor([[[-1.4251, -0.8321,  1.0230],
         [ 0.2008,  0.5929, -0.7696],
         [-0.3721, -1.0837, -0.6642]],

        [[-0.5337,  0.7808,  0.4419],
         [-0.4683,  0.3847,  0.0747],
         [ 1.0156, -0.4933,  1.5340]]])


tensor(
    [
        [-1.4251, -0.8321,  1.0230],
        [ 0.2008,  0.5929, -0.7696],
        [-0.3721, -1.0837, -0.6642]
    ]
)
torch.Size([3, 3])

tensor(
    [
        [-0.5337,  0.7808,  0.4419],
        [-0.4683,  0.3847,  0.0747],
        [ 1.0156, -0.4933,  1.5340]
    ]
)
torch.Size([3, 3])

(2)使用函数给定dim时,表示的是其他维度不变,对该维度进行操作,并去除该维度或使得该维度变化,例如:

import torch

x = torch.randn(2,3)

print(x)

y = torch.argmax(x, dim=0)

print(y)
print(y.size())

输出:

tensor(
    [
        [ 0.0251, -0.3640,  0.1965],
        [ 0.6902,  0.9846,  0.2035]
    ]
)

tensor([1, 1, 1])
torch.Size([3])

去掉dim = 0,比较的就是 [ 0.0251, -0.3640, 0.1965] 和 [ 0.6902, 0.9846, 0.2035]

dim = (2, 3) -> (3)

Pytorch中的初始化函数

torch.cat()函数

注意:按照1中dim的规律,在cat()函数中设置哪个dim,对应的dim的size会变化(增加)

4 torch.empty()函数

总结: 返回填充有未初始化数据的张量。 张量的形状由可变的参数大小定义

Adam优化器详解

使用torch.optim.lr_scheduler调整学习率

注意:一般是调整优化器中的学习率 

CrossEntropyLoss和LabelSmoothing详解

总结:

(1)调用CrossEntropyLoss(x, y),其中x为预测结果(直接的预测结果,不经过softmax层),y为实际的标签值(整数值,而非one-hot值)

(2)CrossEntropyLoss的快速计算方法: 

\operatorname{loss}(x, \text { class })=-\log \left(\frac{\exp (x[\text { class }])}{\sum_j \exp (x[j])}\right)=-x[\text { class }]+\log \left(\sum_j \exp (x[j])\right)

其中,class为实际的标签值(整数值,而非one-hot值)

(3)LabelSmoothing的理论分析

KL散度的两种调用方法和输入要求

总结:

(1)KL散度的定义、基本性质、应用以及常见分布KL散度的计算

注意:

1)上文的倒数第二段推导存在问题,因为在P_1的分布下,\frac{1}{n}\left(\mathbf{x}-\mathbf{u}_1\right)\left(\mathbf{x}-\mathbf{u}_1\right)^T=\Sigma_1,所以可以得到\left(\mathbf{x}-\mathbf{u}_1\right)\left(\mathbf{x}-\mathbf{u}_1\right)^T=n\Sigma_1,故原先的推导变为:

\begin{aligned} & \mathbb{E}_{P_1}\left[\left(\mathbf{x}-\mathbf{u}_2\right)^T \Sigma_2^{-1}\left(\mathbf{x}-\mathbf{u}_2\right)-\left(\mathbf{x}-\mathbf{u}_1\right)^T \Sigma_1^{-1}\left(\mathbf{x}-\mathbf{u}_1\right)\right] \\ & =\mathbb{E}_{P_1}\left[\operatorname{Tr}\left(\Sigma_2^{-1}\left(\mathbf{x}-\mathbf{u}_1+\left(\mathbf{u}_1-\mathbf{u}_2\right)\right)\left(\mathbf{x}-\mathbf{u}_1+\left(\mathbf{u}_1-\mathbf{u}_2\right)\right)^T\right)-n\right] \\ & =\mathbb{E}_{P_1}\left[\operatorname{Tr}\left(\Sigma_2^{-1}\left(\Sigma_1+2\left(\mathbf{x}-\mathbf{u}_1\right)\left(\mathbf{u}_1-\mathbf{u}_2\right)^T+\left(\mathbf{u}_1-\mathbf{u}_2\right)\left(\mathbf{u}_1-\mathbf{u}_2\right)^T\right)\right)-n\right] \\ & =\operatorname{Tr}\left(\Sigma_2^{-1} \Sigma_1\right)+\left(\mathbf{u}_1-\mathbf{u}_2\right)^T \Sigma_2^{-1}\left(\mathbf{u}_1-\mathbf{u}_2\right)-n \end{aligned}

2)一些结论: 

D\left(P_1 \| P_2\right)=\mathbb{E}_{P_1}\left[\log \left(P_1\right)-\log \left(P_2\right)\right]

\mathbb{E}_{P_1}\left[-\log \left(P_1\right)\right]=\frac{n}{2}(1+\log 2 \pi)+\frac{1}{2} \log \operatorname{det}(\Sigma_1)

\mathbb{E}_{P_1}\left[-\log \left(P_2\right)\right]=\frac{1}{2}\left[n\log 2 \pi+\log \operatorname{det}(\Sigma_2)+\operatorname{Tr}\left(\Sigma_2^{-1} \Sigma_1\right)+\left(\mathbf{u}_1-\mathbf{u}_2\right)^T \Sigma_2^{-1}\left(\mathbf{u}_1-\mathbf{u}_2\right)\right]

\begin{aligned} & D\left(P_1 \| P_2\right)=\frac{1}{2}\left[\log \left(\frac{\operatorname{det} \Sigma_2}{\operatorname{det} \Sigma_1}\right)+\operatorname{Tr}\left(\Sigma_2^{-1} \Sigma_1\right)+\left(\mathbf{u}_1-\mathbf{u}_2\right)^T \Sigma_2^{-1}\left(\mathbf{u}_1-\mathbf{u}_2\right)-n\right] \end{aligned}

P_2为标准正太分布时,结果简化为:

\begin{aligned} & D\left(P_1 \| P_2\right)=\frac{1}{2} \left[-\log \left(\operatorname{det} \Sigma_1\right)+\operatorname{Tr}\left(\Sigma_1\right)+\left\|\mathbf{u}_1\right\|^2-n\right] \end{aligned}

(2)torch.nn.KLDivLoss(x, y)和torch.nn.functional.kl_div(x, y)实现的计算为:

l(x, y)=L=\left\{l_1, \ldots, l_N\right\}, \quad l_n=y_n \cdot\left(\log y_n-x_n\right)

(3)在使用时,x为经过softmax和log后的预测结果,y是实际标签值的概率分布

(4)需要注意的是,一般只有LabelSmoothing之后才会将y由原来的整数标签值(或one-hot型标签值)转换成标签值的概率分布,即KL散度一般和LabelSmoothing一起使用

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值