Pytorch torch.mul使param.grad为非类型
我有一个简单的模型类,它有四个输入,每个输入都有自己的线性层。我希望输出是四个节点的乘积,但出于某种原因,无论我如何将它们相乘(使用torch.mul或*),梯度始终为非类型:Pytorch torch.mul使param.grad为非类型,pytorch,Pytorch,我有一个简单的模型类,它有四个输入,每个输入都有自己的线性层。我希望输出是四个节点的乘积,但出于某种原因,无论我如何将它们相乘(使用torch.mul或*),梯度始终为非类型: class Net(torch.nn.Module): def __init__(self, D_u, D_i, D_t, D_m): super(Net, self).__init__() self.lin_u = nn.Linear(D_u, 1) self.l
class Net(torch.nn.Module):
def __init__(self, D_u, D_i, D_t, D_m):
super(Net, self).__init__()
self.lin_u = nn.Linear(D_u, 1)
self.lin_i = nn.Linear(D_i, 1)
self.lin_t = nn.Linear(D_t, 1)
self.lin_m = nn.Linear(D_m, 1)
self.output = nn.Linear(4, 1)
def forward(self, args):
(u, i, t, m) = args
u = F.relu(self.lin_u(u))
i = F.relu(self.lin_i(i))
t = F.relu(self.lin_t(t))
m = F.relu(self.lin_m(m))
out = torch.mul(u, i)
out = torch.mul(out, t)
out = torch.mul(out, m)
return out
我已经将输入设置为requires_grad=True,我认为问题在于out不是叶子,因此没有梯度,但我不知道如何解决这个问题
编辑:
数据u_块、i_块、t_块、m_块、y_块如下所示。u_块、i_块和t_块是一个热向量
TypeError
---> param -= learning_rate * param.grad
TypeError: unsupported operand type(s) for *: 'float' and 'NoneType'
进行以下更改。您没有使用
self.output
,因此我已经发表了评论。这将使渐变为“无”,因为您在正向过程中不使用渐变,并且默认情况下该层需要_grad=True
u_block: tensor([[1., 0., 0., ..., 0., 0., 0.],
[1., 0., 0., ..., 0., 0., 0.],
[1., 0., 0., ..., 0., 0., 0.],
...,
[0., 0., 0., ..., 0., 0., 1.],
[0., 0., 0., ..., 0., 0., 1.],
[0., 0., 0., ..., 0., 0., 1.]], requires_grad=True)
i_block: tensor([[1., 0., 0.],
[1., 0., 0.],
[1., 0., 0.],
...,
[0., 1., 0.],
[0., 1., 0.],
[0., 1., 0.]], requires_grad=True)
t_block: tensor([[1., 0., 0., ..., 0., 0., 0.],
[0., 1., 0., ..., 0., 0., 0.],
[0., 0., 1., ..., 0., 0., 0.],
...,
[0., 0., 0., ..., 1., 0., 0.],
[0., 0., 0., ..., 0., 1., 0.],
[0., 0., 0., ..., 0., 0., 1.]], requires_grad=True)
m_block: tensor([[ 0.0335],
[ 0.0000],
[ 0.0000],
...,
[ 0.1515],
[-0.2261],
[-0.0402]], requires_grad=True)
y_block: tensor([[ 0.0000],
[ 0.0000],
[ 0.0000],
...,
[-0.2261],
[-0.0402],
[-0.1318]], requires_grad=True)```
我希望这能解决你的问题
还有,我有一些建议
进行以下更改。您没有使用
self.output
,因此我已经发表了评论。这将使渐变为“无”,因为您在正向过程中不使用渐变,并且默认情况下该层需要_grad=True
u_block: tensor([[1., 0., 0., ..., 0., 0., 0.],
[1., 0., 0., ..., 0., 0., 0.],
[1., 0., 0., ..., 0., 0., 0.],
...,
[0., 0., 0., ..., 0., 0., 1.],
[0., 0., 0., ..., 0., 0., 1.],
[0., 0., 0., ..., 0., 0., 1.]], requires_grad=True)
i_block: tensor([[1., 0., 0.],
[1., 0., 0.],
[1., 0., 0.],
...,
[0., 1., 0.],
[0., 1., 0.],
[0., 1., 0.]], requires_grad=True)
t_block: tensor([[1., 0., 0., ..., 0., 0., 0.],
[0., 1., 0., ..., 0., 0., 0.],
[0., 0., 1., ..., 0., 0., 0.],
...,
[0., 0., 0., ..., 1., 0., 0.],
[0., 0., 0., ..., 0., 1., 0.],
[0., 0., 0., ..., 0., 0., 1.]], requires_grad=True)
m_block: tensor([[ 0.0335],
[ 0.0000],
[ 0.0000],
...,
[ 0.1515],
[-0.2261],
[-0.0402]], requires_grad=True)
y_block: tensor([[ 0.0000],
[ 0.0000],
[ 0.0000],
...,
[-0.2261],
[-0.0402],
[-0.1318]], requires_grad=True)```
我希望这能解决你的问题
还有,我有一些建议
提供您正在传递的示例数据
get_data\u new(data)
或u块、i_块、t_块、m_块、y_块
它们都是从torch.tensor(arr.astype(np.float32),需要_grad=True)
,其中arr是一个numpy数组。提供arr
提供您正在传递的示例数据get_data\u new(数据)
或u_块、i_块、t_块、m_块、y_块
它们都是从torch.tensor创建的(arr.astype(np.float32),需要_grad=True)
,其中arr是一个numpy数组。提供arr
class Net(torch.nn.Module):
def __init__(self, D_u, D_i, D_t, D_m):
super(Net, self).__init__()
self.lin_u = nn.Linear(D_u, 1)
self.lin_i = nn.Linear(D_i, 1)
self.lin_t = nn.Linear(D_t, 1)
self.lin_m = nn.Linear(D_m, 1)
# self.output = nn.Linear(4, 1)
def forward(self, args):
(u, i, t, m) = args
u = F.relu(self.lin_u(u))
i = F.relu(self.lin_i(i))
t = F.relu(self.lin_t(t))
m = F.relu(self.lin_m(m))
out = torch.mul(u, i)
out = torch.mul(out, t)
out = torch.mul(out, m)
return out