Python 使用PyTorch手动指定权重

Python 使用PyTorch手动指定权重,python,neural-network,pytorch,Python,Neural Network,Pytorch,我使用Python3.8和PyTorch 1.7手动分配和更改神经网络的权重和偏差。作为一个例子,我定义了一个LeNet-300-100完全连接的神经网络来在MNIST数据集上进行训练。类定义的代码是: class LeNet300(nn.Module): def __init__(self): super(LeNet300, self).__init__() # Define layers- self.fc1 = nn.

我使用Python3.8和PyTorch 1.7手动分配和更改神经网络的权重和偏差。作为一个例子,我定义了一个LeNet-300-100完全连接的神经网络来在MNIST数据集上进行训练。类定义的代码是:

class LeNet300(nn.Module):
    def __init__(self):
        super(LeNet300, self).__init__()
        
        # Define layers-
        self.fc1 = nn.Linear(in_features = input_size, out_features = 300)
        self.fc2 = nn.Linear(in_features = 300, out_features = 100)
        self.output = nn.Linear(in_features = 100, out_features = 10)
        
        self.weights_initialization()
    
    
    def forward(self, x):
        out = F.relu(self.fc1(x))
        out = F.relu(self.fc2(out))
        return self.output(out)
    
    
    def weights_initialization(self):
        '''
        When we define all the modules such as the layers in '__init__()'
        method above, these are all stored in 'self.modules()'.
        We go through each module one by one. This is the entire network,
        basically.
        '''
        for m in self.modules():
            if isinstance(m, nn.Linear):
                nn.init.xavier_normal_(m.weight)
                nn.init.constant_(m.bias, 0)
尝试更改此模型的权重的步骤-

# Instantiate model-
mask_model = LeNet300()
为了将每个层中的所有权重分配给一(1),我使用代码-

with torch.no_grad():
    for layer in mask_model.state_dict():
        mask_model.state_dict()[layer] = nn.parameter.Parameter(torch.ones_like(mask_model.state_dict()[layer]))

# Sanity check-
mask_model.state_dict()['fc1.weight']
for param in mask_model.parameters():
    # print(param.shape)
    param = nn.parameter.Parameter(torch.ones_like(param))
此输出显示权重不等于1

我也试过代码-

with torch.no_grad():
    for layer in mask_model.state_dict():
        mask_model.state_dict()[layer] = nn.parameter.Parameter(torch.ones_like(mask_model.state_dict()[layer]))

# Sanity check-
mask_model.state_dict()['fc1.weight']
for param in mask_model.parameters():
    # print(param.shape)
    param = nn.parameter.Parameter(torch.ones_like(param))
但这并不奏效


帮助?

我用一种非常简单的方法(只使用了
fill()

代码如下:

import torch
import torch.nn as nn
class LeNet300(nn.Module):
    def __init__(self):
        super(LeNet300, self).__init__()
        
        # Define layers-
        self.fc1 = nn.Linear(in_features = 28, out_features = 300)
        self.fc2 = nn.Linear(in_features = 300, out_features = 100)
        self.output = nn.Linear(in_features = 100, out_features = 10)
        
        self.weights_initialization()
    
    
    def forward(self, x):
        out = F.relu(self.fc1(x))
        out = F.relu(self.fc2(out))
        return self.output(out)
    
    
    def weights_initialization(self):
        '''
        When we define all the modules such as the layers in '__init__()'
        method above, these are all stored in 'self.modules()'.
        We go through each module one by one. This is the entire network,
        basically.
        '''
        for m in self.modules():
            if isinstance(m, nn.Linear):
                nn.init.xavier_normal_(m.weight)
                nn.init.constant_(m.bias, 0)


mask_model = LeNet300()


with torch.no_grad():
    for layer in mask_model.state_dict():
        print(layer)
        #print(torch.ones_like(mask_model.state_dict()[layer].data))
        mask_model.state_dict()[layer].data.fill_(1)


mask_model.state_dict()['fc1.weight']
#   tensor([[1., 1., 1.,  ..., 1., 1., 1.],
#        [1., 1., 1.,  ..., 1., 1., 1.],
#        [1., 1., 1.,  ..., 1., 1., 1.],
#        ...,
#        [1., 1., 1.,  ..., 1., 1., 1.],
#        [1., 1., 1.,  ..., 1., 1., 1.],
#        [1., 1., 1.,  ..., 1., 1., 1.]])