經驗積累
複製層的初始化問題,使用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)
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.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) ×不同×