简体   繁体   English

如何在pytorch中改变NN的权重

[英]How to mutate weights of a NN in pytorch

Im playing around with genetic algorithms with pytorch and I'm looking for a more efficient way of mutating the weights of the network (applying a small modification to them)我正在使用 pytorch 玩遗传算法,我正在寻找一种更有效的方法来改变网络的权重(对它们进行小的修改)

Right now I have a suboptimal solution where I loop through the parameters and apply a random modification.现在我有一个次优的解决方案,我循环遍历参数并应用随机修改。

child_agent = network()
for param in child_agent.parameters():
        if len(param.shape) == 4:  # weights of Conv2D
            for i0 in range(param.shape[0]):
                for i1 in range(param.shape[1]):
                    for i2 in range(param.shape[2]):
                        for i3 in range(param.shape[3]):
                            param[i0][i1][i2][i3] += mutation_power * np.random.randn()
        elif len(param.shape) == 2:  # weights of linear layer
            for i0 in range(param.shape[0]):
                for i1 in range(param.shape[1]):
                    param[i0][i1] += mutation_power * np.random.randn()
        elif len(param.shape) == 1:  # biases of linear layer or conv layer
            for i0 in range(param.shape[0]):
                param[i0] += mutation_power * np.random.randn()

This solution is bound to my architecture and needs recoding If I decide to add more layers.如果我决定添加更多层,则此解决方案与我的架构绑定并且需要重新编码。 Is there any way to do this more efficiently and clean?有什么方法可以更有效和更清洁地做到这一点? Preferable that it works regardless of how the architecture of my network looks like.无论我的网络架构如何,它都可以正常工作。

Thanks谢谢

pytorch and numpy are tensor oriented, eg you make operations on multiple items contained within multidimensional array-like object. pytorchnumpy是面向tensor的,例如您对包含在多维数组类对象中的多个项目进行操作。

You can change your whole code to this single line:您可以将整个代码更改为这一行:

import torch

child_agent = network()
for param in child_agent.parameters():
    param.data += mutation.power * torch.randn_like(param)

randn_like (docs here ) creates random normal tensor with the same shape as param . randn_like此处为文档)创建与param形状相同的随机法向张量。

Also, if this parameter requires grad (which it probably does), you should modify it's data field.此外,如果此参数需要grad (它可能需要),您应该修改它的data字段。

MCVE : MCVE :

import torch

mutation_power = 0.4

child_agent = torch.nn.Sequential(
    torch.nn.Conv2d(1, 3, 3, padding=1), torch.nn.Linear(10, 20)
)

for param in child_agent.parameters():
    param.data += mutation_power * torch.randn_like(param)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM