【问题标题】:How can I update the parameters of a neural network in PyTorch?如何在 PyTorch 中更新神经网络的参数?
【发布时间】:2018-09-01 23:03:31
【问题描述】:

假设我想将 PyTorch(继承自 torch.nn.Module 的类的实例)中神经网络的所有参数乘以 0.9。我该怎么做?

【问题讨论】:

    标签: python pytorch


    【解决方案1】:

    net 成为神经网络nn.Module 的一个实例。 然后,将所有参数乘以0.9

    state_dict = net.state_dict()
    
    for name, param in state_dict.items():
        # Transform the parameter as required.
        transformed_param = param * 0.9
    
        # Update the parameter.
        param.copy_(transformed_param)
    

    如果您只想更新 权重 而不是每个参数:

    state_dict = net.state_dict()
    
    for name, param in state_dict.items():
        # Don't update if this is not a weight.
        if not "weight" in name:
            continue
        
        # Transform the parameter as required.
        transformed_param = param * 0.9
    
        # Update the parameter.
        param.copy_(transformed_param)
    

    【讨论】:

    • 这有意义吗?我希望一些向量操作来执行这个操作,否则 python 会降低速度,而且这可以在 GPU 上工作吗?
    • @Gulzar 性能影响几乎不存在。 state_dict 是对大张量(例如 16x512x64x64)的引用的小集合(例如 10)。 * 0.9 操作由通常是“矢量化”的外部 C++ 库运行。从 Python 调用 * 0.9 操作非常便宜(每次调用 1 微秒 * 10 次调用 = 10 微秒),因为 Python 不执行任何实际计算。
    • 顺便说一句,难道不能通过param.copy_(transformed_param) 避免再次查找state_dict[name] 吗?我不认为state_dict 会覆盖__setattr__ 无论如何...实际上它只是一个OrderedDict,只有在调用net.state_dict() 时才会创建。
    【解决方案2】:

    实现此目的的另一种方法是使用tensor.parameters()

    初始化模块:

    >>> a = torch.nn.Linear(2, 2)
    >>> a.state_dict()
    OrderedDict([('weight',
                  tensor([[-0.1770, -0.2151],
                          [-0.6543,  0.6637]])),
                 ('bias', tensor([-0.0524,  0.6807]))])
    

    更改参数:

    for p in a.parameters():
        p.data *= 0
    

    看效果:

    >>> a.state_dict()
    OrderedDict([('weight',
                  tensor([[-0., -0.],
                          [-0., 0.]])),
                 ('bias', tensor([-0., 0.]))])
    

    【讨论】:

      猜你喜欢
      • 2020-12-17
      • 2018-12-16
      • 2021-03-11
      • 2019-03-08
      • 1970-01-01
      • 1970-01-01
      • 2023-02-17
      • 2019-01-04
      • 1970-01-01
      相关资源
      最近更新 更多