How to change the activation layer of a Pytorch pretrained network? Here is my code :
JavaScript
x
16
16
1
print("All modules")
2
for child in net.children():
3
if isinstance(child,nn.ReLU) or isinstance(child,nn.SELU):
4
print(child)
5
6
print('Before changing activation')
7
for child in net.children():
8
if isinstance(child,nn.ReLU) or isinstance(child,nn.SELU):
9
print(child)
10
child=nn.SELU()
11
print(child)
12
print('after changing activation')
13
for child in net.children():
14
if isinstance(child,nn.ReLU) or isinstance(child,nn.SELU):
15
print(child)
16
Here is my output:
JavaScript
1
8
1
All modules
2
ReLU(inplace=True)
3
Before changing activation
4
ReLU(inplace=True)
5
SELU()
6
after changing activation
7
ReLU(inplace=True)
8
Advertisement
Answer
._modules
solves the problem for me.
JavaScript
1
4
1
for name,child in net.named_children():
2
if isinstance(child,nn.ReLU) or isinstance(child,nn.SELU):
3
net._modules['relu'] = nn.SELU()
4