如何在torch神经网络最后一层乘以一个数组

class Actor(nn.Module):
    def __init__(self, 6, 20, 6):
        super(Actor, self).__init__()
        self.linear1 = nn.Linear(6, 20)
        self.linear2 = nn.Linear(20, 20)
        self.linear3 = nn.Linear(20, 6)

    def forward(self, s):
        x = F.relu(self.linear1(s))
        x = F.relu(self.linear2(x))
        x = torch.tanh(self.linear3(x))

        return x
以上为原神经网络

我现在有一个数组为limit = [1,2,3,4,5,6]

class Actor(nn.Module):
    def __init__(self, 6, 20, 6):
        super(Actor, self).__init__()
        self.linear1 = nn.Linear(6, 20)
        self.linear2 = nn.Linear(20, 20)
        self.linear3 = nn.Linear(20, 6)

    def forward(self, s, limit):
        x = F.relu(self.linear1(s))
        x = F.relu(self.linear2(x))
        x = torch.tanh(self.linear3(x))

        x = np.multiply(limit,action)

        return x

我想变成这样 结果会报错  求各位大佬告知应如何修改

limit = torch.tensor([1,2,3,4,5,6]) 


class Actor(nn.Module):
    def __init__(self, 6, 20, 6):
        super(Actor, self).__init__()
        self.linear1 = nn.Linear(6, 20)
        self.linear2 = nn.Linear(20, 20)
        self.linear3 = nn.Linear(20, 6) 
    def forward(self, s, limit):
        x = F.relu(self.linear1(s))
        x = F.relu(self.linear2(x))
        x = torch.tanh(self.linear3(x))
        limit = limit.repeat(x.size()[0],1)  
        x = x * limit
 
        return x

 

大兄弟,pytorch里是tensor变量,np是numpy,不能处理变量的

点乘的话,你的limit是【batch_size, size】的形式,所以需要先扩展action,

action = action.repeat(limit.size()[0],1,1)   

然后直接 limit * action