torch.nn.**和torch.nn.functional.**的区别
torch.nn.**
torch.nn.**是一个继承了torch.nn.Module的类,使用前必须先构造对象,然后再调用。如果直接使用则会报错
例如
a = torch.randn(3,4)
print(a)
sigmoid = torch.nn.Sigmoid()
a = sigmoid(a)
print(a)
a = torch.nn.Sigmoid(a)
tensor([[ 0.2462, -2.1680, -1.4064, -0.0268],
[-0.4800, -0.4670, 1.7318, 0.3498],
[ 0.0137, -2.1080, -0.0825, -0.1350]])
tensor([[0.5612, 0.1027, 0.1968, 0.4933],
[0.3823, 0.3853, 0.8496, 0.5866],
[0.5034, 0.1083, 0.4794, 0.4663]])
Traceback (most recent call last):
Traceback (most recent call last):
File "C:\文件\Llama\tmp.py", line 8, in <module>
a = torch.nn.Sigmoid(a)
^^^^^^^^^^^^^^^^^^^
File "C:\Users\90929\AppData\Local\conda\conda\envs\lce\Lib\site-packages\torch\nn\modules\module.py", line 485, in __init__
raise TypeError(
TypeError: Sigmoid.__init__() takes 1 positional argument but 2 were given
torch.nn.functional.**
torch.nn.functional.**是一个纯数学函数,可以直接使用
a = torch.randn(3,4)
print(a)
a = torch.nn.functional.sigmoid(a)
print(a)
tensor([[-0.1516, 0.5398, 0.3226, -0.4956],
[-0.2250, 0.6393, 0.4432, 0.4215],
[-0.5741, 0.0689, 0.3078, -1.5994]])
tensor([[0.4622, 0.6318, 0.5799, 0.3786],
[0.4440, 0.6546, 0.6090, 0.6039],
[0.3603, 0.5172, 0.5763, 0.1681]])
原文地址:https://blog.csdn.net/weixin_40732165/article/details/143690514
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!