weixin_41980210 2021-03-14 19:54 采纳率: 71.4%
浏览 83
已采纳

如何在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

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

  • 写回答

3条回答 默认 最新

  • 皮皮宽 2021-03-15 09:59
    关注
    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
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?