Skip to content
Snippets Groups Projects
Commit 152efc2a authored by Anders Buvarp's avatar Anders Buvarp
Browse files

Move real-valued CVPS to cvps.py.

parent 7d714fc7
Branches
No related tags found
No related merge requests found
...@@ -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
#!/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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment