【Error】报错--torch.nn.Sigmoid():__init__() takes 1 positional argument but 2 were given

【Error】报错–torch.nn.Sigmoid():init() takes 1 positional argument but 2 were given

1. 错误写法

>>> import torch
>>> inp=torch.tensor(10,dtype=torch.float32)

>>> torch.nn.Sigmoid(inp)	# 报错
TypeError: __init__() takes 1 positional argument but 2 were given

2. 出错原因

使用torch.nn.Sigmoid()时,需要先对类实例化,之后才能传参调用。

3. 正确写法

>>> import torch
>>> inp=torch.tensor(10,dtype=torch.float32)

# 先对类实例化,再进行调用:
>>> sigmoid = torch.nn.Sigmoid()
>>> sigmoid(inp)
tensor(1.0000)

# 也可以换其他的用法,即:
>>> torch.sigmoid(inp)
tensor(1.0000)

你可能感兴趣的:(Python--Error,深度学习,pytorch,python)