脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - python中torch.nn.identity()方法详解

python中torch.nn.identity()方法详解

2022-11-13 10:36sigmoidAndRELU Python

今天看源码时遇到的这个恒等函数,就如同名字那样占位符,并没有实际操作,下面这篇文章主要给大家介绍了关于python中torch.nn.identity()方法的相关资料,需要的朋友可以参考下

先看代码

?
1
2
3
4
5
6
7
8
9
10
m = nn.Identity(
54,
unused_argument1=0.1,
unused_argument2=False
)
 
input = torch.randn(128, 20)
output = m(input)
>>> print(output.size())
torch.Size([128, 20])

这是官方文档中给出的代码,很明显,没有什么变化,输入的是torch,输出也是,并且给定的参数似乎并没有起到变化的效果。

看源码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Identity(Module):
    r"""A placeholder identity operator that is argument-insensitive.
 
    Args:
        args: any argument (unused)
        kwargs: any keyword argument (unused)
 
    Examples::
 
        >>> m = nn.Identity(54, unused_argument1=0.1, unused_argument2=False)
        >>> input = torch.randn(128, 20)
        >>> output = m(input)
        >>> print(output.size())
        torch.Size([128, 20])
 
    """
    def __init__(self, *args, **kwargs):
        super(Identity, self).__init__()
 
    def forward(self, input: Tensor) -> Tensor:
        return input

这相当的简洁明了啊,输入是啥,直接给输出,不做任何的改变。再看文档中的一句话:A placeholder identity operator that is argument-insensitive.

翻译一下就是:不区分参数的占位符标识运算符。百度翻译,其实意思就是这个网络层的设计是用于占位的,即不干活,只是有这么一个层,放到残差网络里就是在跳过连接的地方用这个层,显得没有那么空虚!

应用

例如此时:如果此时我们使用了se_layer,那么就SELayer(dim),否则就输入什么就输出什么(什么都不做)

python中torch.nn.identity()方法详解

python中torch.nn.identity()方法详解

总结

到此这篇关于python中torch.nn.identity()方法的文章就介绍到这了,更多相关python torch.nn.identity()方法内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/TTLoveYuYu/article/details/118224298

延伸 · 阅读

精彩推荐