Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
R
Robust
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Anders Buvarp
Robust
Commits
152efc2a
Commit
152efc2a
authored
1 month ago
by
Anders Buvarp
Browse files
Options
Downloads
Patches
Plain Diff
Move real-valued CVPS to cvps.py.
parent
7d714fc7
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
python/cvps.py
+96
-1
96 additions, 1 deletion
python/cvps.py
python/cvps_real.py
+0
-97
0 additions, 97 deletions
python/cvps_real.py
with
96 additions
and
98 deletions
python/cvps.py
+
96
−
1
View file @
152efc2a
...
@@ -52,6 +52,8 @@ def cvps(sig,c_mad,n_iter,thr):
...
@@ -52,6 +52,8 @@ def cvps(sig,c_mad,n_iter,thr):
a = torch.empty(0)
a = torch.empty(0)
b = torch.empty(0)
b = torch.empty(0)
c = torch.empty(0)
c = torch.empty(0)
dump = torch.empty(0)
ma = torch.empty(0)
"""
"""
# Loop for each client
# Loop for each client
...
@@ -95,6 +97,9 @@ def cvps(sig,c_mad,n_iter,thr):
...
@@ -95,6 +97,9 @@ def cvps(sig,c_mad,n_iter,thr):
# Compute the MAD
# Compute the MAD
mad
=
1.4826
*
c_mad
*
me
mad
=
1.4826
*
c_mad
*
me
#dump = torch.cat((dump,ab))
#ma = torch.cat((ma,mad.view(1)))
# Avoid division-by-zero
# Avoid division-by-zero
if
mad
==
0.0
:
if
mad
==
0.0
:
mad
=
1e-12
mad
=
1e-12
...
@@ -173,4 +178,94 @@ def cvps(sig,c_mad,n_iter,thr):
...
@@ -173,4 +178,94 @@ def cvps(sig,c_mad,n_iter,thr):
w_ps
=
torch
.
square
(
w_ps
)
w_ps
=
torch
.
square
(
w_ps
)
# Return the projection statistics and median
# Return the projection statistics and median
return
w_ps
,
M
return
w_ps
,
M
#,dump,ma,PS
\ No newline at end of file
# Add white Gaussian noise
def
cvps_real
(
sig
,
c_mad
,
n_iter
,
thr
):
# Signal power
di
=
sig
.
shape
n_clients
=
di
[
1
]
# Allocate the projection statistics output
PS
=
torch
.
empty
((
1
,
n_clients
))
#print(sig.shape)
#print(sig[:15,:5])
# Compute the geometric median
M
=
Weiszfeld
(
sig
,
n_iter
)
# Allocate standard projections
std_proj
=
torch
.
empty
(
n_clients
,
dtype
=
torch
.
double
)
sp
=
torch
.
zeros
(
n_clients
,
n_clients
)
#dump = torch.empty(0)
#ma = torch.empty(0)
# Loop for each client
for
ux
in
range
(
n_clients
):
# Get the data from the client
s
=
sig
[:,
ux
]
# Computer the difference to the geometric median
diff
=
s
-
M
no
=
torch
.
norm
(
diff
)
# Avoid division by zero by replacing zeros with a small number
eps
=
torch
.
tensor
(
1e-12
,
dtype
=
no
.
dtype
,
device
=
no
.
device
)
no
=
torch
.
where
(
no
==
0
,
eps
,
no
)
# Compute a unit vector for each dimension
u
=
diff
/
no
# Calculate the standardized projections for each client
for
nx
in
range
(
n_clients
):
# Get the data from the client
s
=
sig
[:,
nx
]
# Projections using transpose
std_proj
[
nx
]
=
torch
.
vdot
(
s
,
u
)
# Compute the median of projections for each unit vector
m
=
torch
.
median
(
std_proj
)
# Compute difference between projections and median of projections
de
=
std_proj
-
m
# Compute the absolute value of the difference
ab
=
torch
.
abs
(
de
)
# Compute the median of the absolute value of the difference
me
=
torch
.
median
(
ab
)
# Compute the MAD
mad
=
1.4826
*
c_mad
*
me
#dump = torch.cat((dump,ab))
#ma = torch.cat((ma,mad.view(1)))
# Avoid division-by-zero
if
mad
==
0.0
:
mad
=
1e-12
# Compute standardized projections
sp
[:,
ux
]
=
ab
/
mad
# Compute the projection statistics, PS, and remove the median
PS
,
_
=
torch
.
max
(
sp
,
1
)
PS
=
PS
-
torch
.
median
(
PS
)
PS
=
torch
.
where
(
PS
==
0
,
1e-12
,
PS
)
# Because we subtracted the median from PS, take abs
PS
=
torch
.
abs
(
PS
)
# Compute the PS weights
w_ps
=
thr
/
PS
w_ps
=
torch
.
where
(
w_ps
>
1
,
1.0
,
w_ps
)
w_ps
=
torch
.
square
(
w_ps
)
# Return the projection statistics and median
return
w_ps
,
M
#,dump,ma,PS
\ No newline at end of file
This diff is collapsed.
Click to expand it.
python/cvps_real.py
deleted
100644 → 0
+
0
−
97
View file @
7d714fc7
#!/usr/bin/python3
import
numpy
as
np
import
torch
from
Weiszfeld
import
Weiszfeld
from
Weiszfeld
import
ComplexWeiszfeld
import
statistics
# Add white Gaussian noise
def
cvps_real
(
sig
,
c_mad
,
n_iter
,
thr
):
# Signal power
di
=
sig
.
shape
#p_neurons = di[0]
n_clients
=
di
[
1
]
# Allocate the projection statistics output
PS
=
torch
.
empty
((
1
,
n_clients
));
# Compute the geometric median
M
=
Weiszfeld
(
sig
,
n_iter
)
# Allocate standard projections
std_proj
=
torch
.
empty
(
n_clients
,
dtype
=
torch
.
double
)
sp
=
torch
.
zeros
(
n_clients
,
n_clients
)
#dump = torch.empty(0)
#ma = 0
# Loop for each client
for
ux
in
range
(
0
,
n_clients
):
# Get the data from the client
s
=
sig
[:,
ux
]
# Computer the difference to the geometric median
diff
=
s
-
M
no
=
torch
.
norm
(
diff
)
# Avoid division by zero by replacing zeros with a small number
eps
=
torch
.
tensor
(
1e-12
,
dtype
=
no
.
dtype
,
device
=
no
.
device
)
no
=
torch
.
where
(
no
==
0
,
eps
,
no
)
# Compute a unit vector for each dimension
u
=
diff
/
no
# Calculate the standardized projections for each client
for
nx
in
range
(
n_clients
):
# Get the data from the client
s
=
sig
[:,
nx
]
# Projections using transpose
std_proj
[
nx
]
=
torch
.
vdot
(
s
,
u
)
# Compute the median of projections for each unit vector
m
=
torch
.
median
(
std_proj
)
# Compute difference between projections and median of projections
de
=
std_proj
-
m
# Compute the absolute value of the difference
ab
=
torch
.
abs
(
de
)
#dump = torch.cat((dump,ab))
# Compute the median of the absolute value of the difference
me
=
torch
.
median
(
ab
)
# Compute the MAD
mad
=
1.4826
*
c_mad
*
me
#ma = ma + mad
# Avoid division-by-zero
if
mad
==
0.0
:
mad
=
1e-12
# Compute standardized projections
sp
[:,
ux
]
=
ab
/
mad
# Compute the projection statistics, PS, and remove the median
PS
,
_
=
torch
.
max
(
sp
,
1
)
PS
=
PS
-
torch
.
median
(
PS
)
if
PS
.
isnan
().
any
():
print
(
PS
)
# Because we subtracted the median from PS, take abs
PS
=
torch
.
abs
(
PS
)
# Compute the PS weights
w_ps
=
thr
/
PS
w_ps
=
torch
.
where
(
w_ps
>
1
,
1.0
,
w_ps
)
w_ps
=
torch
.
square
(
w_ps
)
# Return the projection statistics and median
return
w_ps
,
M
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment