有些时候需要将神经网络的(部分)权重进行初始化,代码:
import torch
import torch.nn as nn
# 创建一个全连接层模型
class MylcModel(nn.Module):
def __init__(self):
super(MylcModel, self).__init__()
self.fc = nn.Linear(10, 5) # 创建一个全连接层
# 创建一个卷积层模型
class MyconvModel(nn.Module):
def __init__(self):
super(MyconvModel, self).__init__()
self.conv = nn.Linear(10, 5) # 创建一个全连接层
# 初始化模型
lc_model = MylcModel()
conv_model = MyconvModel()
# 初始化前权重
print("全连接层权重:", lc_model.fc.weight)
print("卷积层权重:", conv_model.conv.weight)
#初始化后
nn.init.constant_(lc_model.fc.weight, 0.1)
nn.init.constant_(lc_model.fc.bias, 0.2)
nn.init.constant_(conv_model.conv.weight, 0.2)
nn.init.constant_(conv_model.conv.bias, 0.4)
print("全连接层权重:", lc_model.fc.weight)
print("卷积层层权重:", conv_model.conv.weight)