From 55f82abd0a0dc9dbca7a5424727cd58a6eb8fd2d Mon Sep 17 00:00:00 2001 From: volmodaoist <1304548501@qq.com> Date: Sun, 28 Jul 2024 02:09:26 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AF=B9=E5=BC=A0=E9=87=8F=E8=BF=90=E7=AE=97?= =?UTF-8?q?=E5=BA=94=E5=B0=BD=E9=87=8F=E9=81=BF=E5=85=8D=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E5=BE=AA=E7=8E=AF=EF=BC=8C=E5=8E=9F=E7=89=88=E7=9A=84=20model?= =?UTF-8?q?=5Fnorm=20=E4=BD=BF=E7=94=A8=E4=BA=86=20for=20=E5=AF=BC?= =?UTF-8?q?=E8=87=B4=E6=95=88=E7=8E=87=E5=81=8F=E4=BD=8E=E4=B8=94=E5=8F=AF?= =?UTF-8?q?=E8=AF=BB=E6=80=A7=E5=B7=AE=EF=BC=8C=20=E4=B8=BA=E6=AD=A4?= =?UTF-8?q?=E6=88=91=E4=BB=AC=E4=BC=98=E5=8C=96=E4=BA=86=E5=8E=9F=E7=89=88?= =?UTF-8?q?=20model=5Fnorm=20=E6=96=B9=E6=B3=95=EF=BC=8C=E5=A2=9E=E5=BC=BA?= =?UTF-8?q?=E4=BA=86=E5=8F=AF=E8=AF=BB=E6=80=A7=E4=B8=94=E8=BF=90=E8=A1=8C?= =?UTF-8?q?=E6=95=88=E7=8E=87=E6=8F=90=E9=AB=98=E4=BA=86=E4=B8=80=E5=80=8D?= =?UTF-8?q?=EF=BC=8C=E8=8B=A5=E5=9C=A8=20GPU=20=E7=8E=AF=E5=A2=83=E4=B9=8B?= =?UTF-8?q?=E4=B8=AD=E8=BF=90=E8=A1=8C=E4=BB=A3=E7=A0=81=EF=BC=8C=20?= =?UTF-8?q?=E6=80=A7=E8=83=BD=E6=8F=90=E5=8D=87=E4=BC=9A=E6=9B=B4=E5=8A=A0?= =?UTF-8?q?=E6=98=8E=E6=98=BE=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: volmodaoist <1304548501@qq.com> --- chapter15_Differential_Privacy/models.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/chapter15_Differential_Privacy/models.py b/chapter15_Differential_Privacy/models.py index c8e0a86..b91cb02 100644 --- a/chapter15_Differential_Privacy/models.py +++ b/chapter15_Differential_Privacy/models.py @@ -26,9 +26,13 @@ def get_model(name="vgg16", pretrained=True): else: return model + def model_norm(model_1, model_2): - squared_sum = 0 - for name, layer in model_1.named_parameters(): - # print(torch.mean(layer.data), torch.mean(model_2.state_dict()[name].data)) - squared_sum += torch.sum(torch.pow(layer.data - model_2.state_dict()[name].data, 2)) - return math.sqrt(squared_sum) \ No newline at end of file + params_1 = torch.cat([param.view(-1) for param in model_1.parameters()]) + params_2 = torch.cat([param.view(-1) for param in model_2.parameters()]) + + return torch.norm(params_1 - params_2, p = 2) + +def quick_model_norm(model_1, model_2): + diffs = [(p1 - p2).view(-1) for p1, p2 in zip(model_1.parameters(), model_2.parameters())] + return torch.norm(torch.cat(diffs), p = 2) \ No newline at end of file