这篇文章主要讲解了“torch.nn.init.constant_(tensor, val)如何使用”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“torch.nn.init.constant_(tensor, val)如何使用”吧!
功能:使用val的值来填充输入的Tensor
代码实验展示:
Microsoft Windows [版本 10.0.18363.1256](c) 2019 Microsoft Corporation。保留所有权利。 C:/Users/chenxuqi>conda activate ssd4pytorch2_2_0(ssd4pytorch2_2_0) C:/Users/chenxuqi>python Python 3.7.7 (default, May 6 2020, 11:45:54) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32 Type "help", "copyright", "credits" or "license" for more information.>>> import torch>>> import torch.nn as nn>>> w = torch.empty(3, 2)>>> w tensor([[1.0381e+23, 4.4141e-43],[1.0381e+23, 4.4141e-43],[1.0381e+23, 4.4141e-43]])>>> nn.init.constant_(w, 20200910.0)tensor([[20200910., 20200910.],[20200910., 20200910.],[20200910., 20200910.]])>>>>>>>>> weight = nn.Parameter(torch.Tensor(3))>>> weight Parameter containing:tensor([0.0000e+00, 0.0000e+00, 2.9066e+38], requires_grad=True)>>>>>> nn.init.constant_(weight, 20200910.0)Parameter containing:tensor([20200910., 20200910., 20200910.], requires_grad=True)>>> weight Parameter containing:tensor([20200910., 20200910., 20200910.], requires_grad=True)>>>>>>>>>
感谢各位的阅读,以上就是“torch.nn.init.constant_(tensor, val)如何使用”的内容了,经过本文的学习后,相信大家对torch.nn.init.constant_(tensor, val)如何使用这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
原创文章,作者:6024010,如若转载,请注明出处:https://blog.ytso.com/229616.html