如何更改 Pytorch 预训练模块中的激活层?

Ham*_*ard 5 python neural-network deep-learning activation-function pytorch

如何更改 Pytorch 预训练网络的激活层?这是我的代码:

print("All modules")
for child in net.children():
    if isinstance(child,nn.ReLU) or isinstance(child,nn.SELU):
        print(child)

print('Before changing activation')
for child in net.children():
    if isinstance(child,nn.ReLU) or isinstance(child,nn.SELU):
        print(child)
        child=nn.SELU()
        print(child)
print('after changing activation')
for child in net.children():
    if isinstance(child,nn.ReLU) or isinstance(child,nn.SELU):
        print(child)
Run Code Online (Sandbox Code Playgroud)

这是我的输出:

All modules
ReLU(inplace=True)
Before changing activation
ReLU(inplace=True)
SELU()
after changing activation
ReLU(inplace=True)
Run Code Online (Sandbox Code Playgroud)

Ham*_*ard 3

._modules为我解决了问题。

for name,child in net.named_children():
    if isinstance(child,nn.ReLU) or isinstance(child,nn.SELU):
        net._modules['relu'] = nn.SELU()
Run Code Online (Sandbox Code Playgroud)