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条)

报告相同问题?

悬赏问题

  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测