如何在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)

这是我的输出:

All modules
ReLU(inplace=True)
Before changing activation
ReLU(inplace=True)
SELU()
after changing activation
ReLU(inplace=True)
汉达德

._modules 为我解决了问题。

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在 Pytorch 中的 EfficieNet 预训练模型中添加最后一个分类层?

如何在pytorch中获得预训练的VGG16的特定层输出

如何从pytorch中的预训练CNN获取特定层的输出

如何在Keras中找到预训练的InceptionResNetV2模型不同层中的激活形状-Tensorflow 2.0

如何在Pytorch中将预训练的FC层转换为CONV层

如何更改pytorch预训练模型中的类数

如何在 pytorch 中的修改后的 vgg19 网络中加载预训练权重?

如何在 pytorch 中加载预训练的 googlenet 模型

如何在Keras中缓存层激活?

如何在Keras中更改预训练模型的默认下载目录?

预训练的bert模型中的冻结层

如何在pytorch中更改模块的部分参数设备类型?

如何连接预训练的嵌入层和输入层

在keras中的预训练密集层之间添加辍学层

如何在预训练的 MobileNetV3 模型的顶部添加额外的层?

如何在内置的预训练模型中放置自定义层?

如何在预训练的 BERT 模型之上添加多类多标签层?

BERT如何在Windows Anaconda中运行预训练的模型

如何在python中打开预训练模型

如何在 keras 模型中启用给定层可训练?

在预训练的VGG16模型中激活辍学

在Keras中更改预训练的AlexNet分类

在预训练的Keras模型中替换嵌入层

在手电筒中,如何在训练时间内修复预训练的嵌入?

如何从Pytorch中的预训练模型加载保存的令牌生成器

在使用多个输入层训练的keras模型中,如何在预测时忽略某些输入层?

PyTorch-如何在训练中获得学习率?

如何使用VGG-16中的预训练特征作为Keras中GlobalAveragePooling2D()层的输入

如何在pytorch中实现低维嵌入层