Skip to content

Commit 55f82ab

Browse files
committed
对张量运算应尽量避免使用循环,原版的 model_norm 使用了 for 导致效率偏低且可读性差,
为此我们优化了原版 model_norm 方法,增强了可读性且运行效率提高了一倍,若在 GPU 环境之中运行代码, 性能提升会更加明显。 Signed-off-by: volmodaoist <[email protected]>
1 parent 8090c9a commit 55f82ab

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

chapter15_Differential_Privacy/models.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,13 @@ def get_model(name="vgg16", pretrained=True):
2626
else:
2727
return model
2828

29+
2930
def model_norm(model_1, model_2):
30-
squared_sum = 0
31-
for name, layer in model_1.named_parameters():
32-
# print(torch.mean(layer.data), torch.mean(model_2.state_dict()[name].data))
33-
squared_sum += torch.sum(torch.pow(layer.data - model_2.state_dict()[name].data, 2))
34-
return math.sqrt(squared_sum)
31+
params_1 = torch.cat([param.view(-1) for param in model_1.parameters()])
32+
params_2 = torch.cat([param.view(-1) for param in model_2.parameters()])
33+
34+
return torch.norm(params_1 - params_2, p = 2)
35+
36+
def quick_model_norm(model_1, model_2):
37+
diffs = [(p1 - p2).view(-1) for p1, p2 in zip(model_1.parameters(), model_2.parameters())]
38+
return torch.norm(torch.cat(diffs), p = 2)

0 commit comments

Comments
 (0)