У меня есть следующий пользовательский модуль:
Код: Выделить всё
class CustomPara(nn.Module):
def __init__(self, num_blocks, in_planes, out_planes, kernel_size):
super(CustomPara, self).__init__()
self.coefficient_shape = (num_blocks,1,1,1,1)
blocks = [torch.Tensor(out_planes, in_planes, kernel_size, kernel_size) for _ in range(num_blocks)]
for i in range(num_blocks): init.kaiming_normal_(blocks[i])
self.blocks = nn.Parameter(torch.stack(blocks)) # this is what we will freeze later
def forward(self, coefficients):
final_blocks = (self.blocks*coefficients).sum(0)
return final_blocks
Подробнее здесь: https://stackoverflow.com/questions/781 ... parameters