[ pytorch ] ——模型設計專題

經驗積累

複製層的初始化問題,使用deepcopy

import torch
import torch.nn as nn
import copy

class MyModule(nn.Module):
    def __init__(self):
        super(MyModule, self).__init__()
        self.layer = nn.Linear(10, 10,bias=False)
        
        # copy layer
        self.copy_layer01 = copy.copy(self.layer)
        self.copy_layer02 = copy.copy(self.layer)
        self.deep_copy_layer03 = copy.deepcopy(self.layer)

        # nn.init.normal_(self.layer.weight, 0, 20.0)
        nn.init.normal_(self.copy_layer01.weight, 0, 20.0)
        nn.init.normal_(self.copy_layer02.weight, 0, 20.0)
        nn.init.normal_(self.deep_copy_layer03.weight, 0, 20.0)
        print(list(self.copy_layer01.parameters())[0][0][0])
        print(list(self.copy_layer02.parameters())[0][0][0])
        print(list(self.deep_copy_layer03.parameters())[0][0][0])

    def forward(self, x):

        return x
【結果】
tensor(16.5385)
tensor(16.5385)
tensor(41.5840) ×不同×
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章