Skip to content

Commit

Permalink
fix typos
Browse files Browse the repository at this point in the history
  • Loading branch information
Koukyosyumei committed Apr 5, 2024
1 parent 8170416 commit 0d7cab8
Show file tree
Hide file tree
Showing 13 changed files with 194 additions and 193 deletions.
4 changes: 2 additions & 2 deletions src/aijack/attack/evasion/evasion_attack.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def delta_kernel(xm):
return True

def _get_delta_p(self, xm):
"""Culculates deviation of the estimated density p(xm−1 |yc = −1)
"""Calculates deviation of the estimated density p(xm−1 |yc = −1)
Args:
xm (np.array) : an adversarial example
Expand All @@ -167,7 +167,7 @@ def _get_delta_p(self, xm):
return delta_p

def _get_grad_f(self, xm, norm="l1"):
"""Culculates deviation of objective function F
"""Calculates deviation of objective function F
Args:
xm (np.array) : an adversarial example
Expand Down
2 changes: 1 addition & 1 deletion src/aijack/attack/inversion/generator_attack.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def __init__(
target_model if type(target_model) == list else [target_model]
)

def culc_loss(self, dataloader, x_pos=0, y_pos=1, arbitrary_y=False):
def calc_loss(self, dataloader, x_pos=0, y_pos=1, arbitrary_y=False):
running_loss = 0
for data in dataloader:
x = data[x_pos]
Expand Down
16 changes: 8 additions & 8 deletions src/aijack/attack/inversion/gradientinversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def __init__(
x_shape: the input shape of target_model.
y_shape: the output shape of target_model.
optimize_label: If true, only optimize images (the label will be automatically estimated).
gradient_ignore_pos: a list of positions whihc will be ignored during the culculation of
gradient_ignore_pos: a list of positions whihc will be ignored during the calculation of
the distance between gradients
pos_of_final_fc_layer: position of gradients corresponding to the final FC layer
within the gradients received from the client.
Expand Down Expand Up @@ -175,7 +175,7 @@ def _setup_distancefunc(self, distancename):
"""Assigns a function to self.distancefunc according to distancename
Args:
distancename: name of the function to culculat the distance between the gradients.
distancename: name of the function to calculat the distance between the gradients.
currently support 'l2' or 'cossim'.
Raises:
Expand Down Expand Up @@ -225,10 +225,10 @@ def hook(model, inp, output):

return hook

def _culc_regularization_term(
def _calc_regularization_term(
self, fake_x, fake_pred, fake_label, group_fake_x, received_gradients
):
"""Culculates the regularization term
"""calculates the regularization term
Args:
fake_x: reconstructed images
Expand All @@ -238,7 +238,7 @@ def _culc_regularization_term(
received_gradients: gradients received from the client
Returns:
culculated regularization term
calculated regularization term
"""
reg_term = 0
if self.tv_reg_coef != 0:
Expand Down Expand Up @@ -295,7 +295,7 @@ def closure():
distance = self.distancefunc(
fake_gradients, received_gradients, self.gradient_ignore_pos
)
distance += self._culc_regularization_term(
distance += self._calc_regularization_term(
fake_x,
fake_pred,
fake_label,
Expand Down Expand Up @@ -343,7 +343,7 @@ def attack(
a tuple of the best reconstructed images and corresponding labels
Raises:
OverflowError: If the culculated distance become Nan
OverflowError: If the calculated distance become Nan
"""
fake_x, fake_label, optimizer = _setup_attack(
self.x_shape,
Expand Down Expand Up @@ -376,7 +376,7 @@ def attack(
fake_x[:] = fake_x.clamp(self.clamp_range[0], self.clamp_range[1])

# if torch.sum(torch.isnan(distance)).item():
# raise OverflowError("stop because the culculated distance is Nan")
# raise OverflowError("stop because the calculated distance is Nan")

if best_distance > distance:
best_fake_x = fake_x.detach().clone()
Expand Down
4 changes: 2 additions & 2 deletions src/aijack/attack/labelleakage/normattack.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ def extract_intermidiate_gradient(self, outputs):
return self.clients[self.target_client_index].grad_from_next_client

def attack(self, dataloader):
"""Culculate leak_auc on the given SplitNN model
"""Calculates leak_auc on the given SplitNN model
reference: https://arxiv.org/abs/2102.08504
Args:
dataloader (torch dataloader): dataloader for evaluation
criterion: loss function for training
device: cpu or GPU
Returns:
score: culculated leak auc
score: leak auc
"""
epoch_labels = []
epoch_g_norm = []
Expand Down
2 changes: 1 addition & 1 deletion src/aijack/attack/poison/poison_attack.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def _detect_type_of_classifier(self):
return True

def _delta_q(self, xi, xc, yi, yc):
"""Culculate deviation of q
"""Calculate deviation of q
Q = yy.T * K denotes the label - annotated version of K,
and α denotes the SVM’s dual variables corresponding
to each training point.
Expand Down
2 changes: 1 addition & 1 deletion src/aijack/collaborative/fedgems/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def train_client_on_public_dataset(self):

optimizer.zero_grad()
y_preds = client(inputs)
loss = client.culc_loss_on_public_dataset(idx, y_preds, labels)
loss = client.calc_loss_on_public_dataset(idx, y_preds, labels)
loss.backward()
optimizer.step()

Expand Down
2 changes: 1 addition & 1 deletion src/aijack/collaborative/fedgems/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def local_train(self, local_epoch, criterion, trainloader, optimizer):
self, local_epoch, criterion, trainloader, optimizer
)

def culc_loss_on_public_dataset(self, idx, y_pred, y):
def calc_loss_on_public_dataset(self, idx, y_pred, y):
y_pred_server = self.predicted_values_of_server[idx]
base_loss = self.epsilon * self.base_loss_func(y_pred, y.to(torch.int64))
kl_loss = (1 - self.epsilon) * self.kldiv_loss_func(
Expand Down
12 changes: 6 additions & 6 deletions src/aijack/defense/dp/core/rdp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ double eps_randresp(double alpha, py::dict params)
return (1 / (alpha - 1)) * logsumexp(terms, signs);
}

double culc_upperbound_of_rdp_with_Sampled_Gaussian_Mechanism_int(int alpha,
double calc_upperbound_of_rdp_with_Sampled_Gaussian_Mechanism_int(int alpha,
py::dict params,
double sampling_rate)
{
Expand All @@ -90,7 +90,7 @@ double culc_upperbound_of_rdp_with_Sampled_Gaussian_Mechanism_int(int alpha,
return log_a / (alpha - 1);
}

double culc_upperbound_of_rdp_with_Sampled_Gaussian_Mechanism_frac(double alpha,
double calc_upperbound_of_rdp_with_Sampled_Gaussian_Mechanism_frac(double alpha,
py::dict params,
double sampling_rate)
{
Expand Down Expand Up @@ -153,24 +153,24 @@ double culc_upperbound_of_rdp_with_Sampled_Gaussian_Mechanism_frac(double alpha,
return _log_add(log_a0, log_a1) / (alpha - 1);
}

double culc_upperbound_of_rdp_with_Sampled_Gaussian_Mechanism(double alpha,
double calc_upperbound_of_rdp_with_Sampled_Gaussian_Mechanism(double alpha,
py::dict params,
double sampling_rate,
const std::function<double(double, py::dict)> &_eps)
{
if (fmod(alpha, 1) == 0.0)
{
return culc_upperbound_of_rdp_with_Sampled_Gaussian_Mechanism_int(
return calc_upperbound_of_rdp_with_Sampled_Gaussian_Mechanism_int(
(int)alpha, params, sampling_rate);
}
else
{
return culc_upperbound_of_rdp_with_Sampled_Gaussian_Mechanism_frac(
return calc_upperbound_of_rdp_with_Sampled_Gaussian_Mechanism_frac(
alpha, params, sampling_rate);
}
}

double culc_tightupperbound_lowerbound_of_rdp_with_theorem6and8_of_zhu_2019(int alpha,
double calc_tightupperbound_lowerbound_of_rdp_with_theorem6and8_of_zhu_2019(int alpha,
py::dict params,
double sampling_rate,
const std::function<double(double, py::dict)> &_eps)
Expand Down
4 changes: 2 additions & 2 deletions src/aijack/defense/dp/manager/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from .accountant import ( # noqa: F401
GeneralMomentAccountant,
culc_tightupperbound_lowerbound_of_rdp_with_theorem6and8_of_zhu_2019,
culc_upperbound_of_rdp_with_Sampled_Gaussian_Mechanism,
calc_tightupperbound_lowerbound_of_rdp_with_theorem6and8_of_zhu_2019,
calc_upperbound_of_rdp_with_Sampled_Gaussian_Mechanism,
)
from .client import DPSGDClientManager # noqa: F401
from .dp_manager import DPSGDManager # noqa: F401
34 changes: 17 additions & 17 deletions src/aijack/defense/dp/manager/accountant.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
_greedy_search_frac,
_ternary_search,
_ternary_search_int,
culc_tightupperbound_lowerbound_of_rdp_with_theorem6and8_of_zhu_2019,
culc_upperbound_of_rdp_with_Sampled_Gaussian_Mechanism,
calc_tightupperbound_lowerbound_of_rdp_with_theorem6and8_of_zhu_2019,
calc_upperbound_of_rdp_with_Sampled_Gaussian_Mechanism,
eps_gaussian,
eps_laplace,
)

from .rdp import (
culc_upperbound_of_rdp_with_Sampled_Gaussian_Mechanism as culc_upperbound_of_rdp_with_Sampled_Gaussian_Mechanism_py,
calc_upperbound_of_rdp_with_Sampled_Gaussian_Mechanism as calc_upperbound_of_rdp_with_Sampled_Gaussian_Mechanism_py,
)


Expand All @@ -33,7 +33,7 @@ def __init__(
self.max_iterations = max_iterations

self.steps_info = []
self._culc_bound_of_rdp = None
self._calc_bound_of_rdp = None

if search == "ternary":
self.search = _ternary_search
Expand All @@ -46,7 +46,7 @@ def __init__(

self._cache = {}

def _culc_upperbound_of_rdp_onestep(self, alpha, noise_params, sampling_rate):
def _calc_upperbound_of_rdp_onestep(self, alpha, noise_params, sampling_rate):
key = hash(
f"{alpha}_{list(noise_params.keys())[0]}_{list(noise_params.values())[0]}_{sampling_rate}"
)
Expand All @@ -56,18 +56,18 @@ def _culc_upperbound_of_rdp_onestep(self, alpha, noise_params, sampling_rate):
elif sampling_rate == 1:
result = self.eps_func(alpha, noise_params)
else:
result = self._culc_bound_of_rdp(
result = self._calc_bound_of_rdp(
alpha, noise_params, sampling_rate, self.eps_func
)
self._cache[key] = result
return result
else:
return self._cache[key]

def _culc_upperbound_of_rdp(self, lam, steps_info):
def _calc_upperbound_of_rdp(self, lam, steps_info):
rdp = 0.0
for noise_params, sampling_rate, num_steps in steps_info:
rdp += num_steps * self._culc_upperbound_of_rdp_onestep(
rdp += num_steps * self._calc_upperbound_of_rdp_onestep(
lam, noise_params, sampling_rate
)
return rdp
Expand Down Expand Up @@ -135,7 +135,7 @@ def get_noise_multiplier(
def get_delta(self, epsilon):
optimal_lam = self.search(
lambda order: (order - 1)
* (self._culc_upperbound_of_rdp(order - 1, self.steps_info) - epsilon),
* (self._calc_upperbound_of_rdp(order - 1, self.steps_info) - epsilon),
self.order_min,
self.order_max,
self.orders,
Expand All @@ -145,7 +145,7 @@ def get_delta(self, epsilon):

min_delta = np.exp(
(optimal_lam - 1)
* (self._culc_upperbound_of_rdp(optimal_lam - 1, self.steps_info) - epsilon)
* (self._calc_upperbound_of_rdp(optimal_lam - 1, self.steps_info) - epsilon)
)

return min_delta
Expand All @@ -155,7 +155,7 @@ def get_epsilon(self, delta):

def estimate_eps(order):
return (
self._culc_upperbound_of_rdp(order, self.steps_info)
self._calc_upperbound_of_rdp(order, self.steps_info)
- (np.log(order) + np.log(delta)) / (order - 1)
+ np.log((order - 1) / order)
)
Expand Down Expand Up @@ -208,14 +208,14 @@ def _set_noise_type(self, noise_type):

def _set_upperbound_func(self, backend, bound_type):
if backend == "cpp" and bound_type == "rdp_upperbound_closedformula":
self._culc_bound_of_rdp = (
culc_upperbound_of_rdp_with_Sampled_Gaussian_Mechanism
self._calc_bound_of_rdp = (
calc_upperbound_of_rdp_with_Sampled_Gaussian_Mechanism
)
elif backend == "python" and bound_type == "rdp_upperbound_closedformula":
self._culc_bound_of_rdp = (
culc_upperbound_of_rdp_with_Sampled_Gaussian_Mechanism_py
self._calc_bound_of_rdp = (
calc_upperbound_of_rdp_with_Sampled_Gaussian_Mechanism_py
)
elif backend == "cpp" and bound_type == "rdp_tight_upperbound":
self._culc_bound_of_rdp = (
culc_tightupperbound_lowerbound_of_rdp_with_theorem6and8_of_zhu_2019
self._calc_bound_of_rdp = (
calc_tightupperbound_lowerbound_of_rdp_with_theorem6and8_of_zhu_2019
)
24 changes: 12 additions & 12 deletions src/aijack/defense/dp/manager/rdp.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,21 @@ def eps_randresp(alpha, params):
)


def culc_upperbound_of_rdp_with_Sampled_Gaussian_Mechanism(
def calc_upperbound_of_rdp_with_Sampled_Gaussian_Mechanism(
alpha, params, sampling_rate, _eps
):
"""Compute log(A_alpha) for any positive finite alpha."""
if float(alpha).is_integer():
return culc_upperbound_of_rdp_with_Sampled_Gaussian_Mechanism_int(
return calc_upperbound_of_rdp_with_Sampled_Gaussian_Mechanism_int(
int(alpha), params, sampling_rate
)
else:
return culc_upperbound_of_rdp_with_Sampled_Gaussian_Mechanism_float(
return calc_upperbound_of_rdp_with_Sampled_Gaussian_Mechanism_float(
alpha, params, sampling_rate
)


def culc_upperbound_of_rdp_with_Sampled_Gaussian_Mechanism_int(
def calc_upperbound_of_rdp_with_Sampled_Gaussian_Mechanism_int(
alpha, params, sampling_rate
):
"""Renyi Differential Privacy of the Sampled Gaussian Mechanism
Expand All @@ -81,7 +81,7 @@ def culc_upperbound_of_rdp_with_Sampled_Gaussian_Mechanism_int(
return float(log_a) / (alpha - 1)


def culc_upperbound_of_rdp_with_Sampled_Gaussian_Mechanism_float(
def calc_upperbound_of_rdp_with_Sampled_Gaussian_Mechanism_float(
alpha, params, sampling_rate
):
"""Compute log(A_alpha) for fractional alpha. 0 < q < 1."""
Expand Down Expand Up @@ -120,7 +120,7 @@ def culc_upperbound_of_rdp_with_Sampled_Gaussian_Mechanism_float(
return _log_add(log_a0, log_a1) / (alpha - 1)


def culc_upperbound_of_rdp_with_theorem27_of_wang_2019(
def calc_upperbound_of_rdp_with_theorem27_of_wang_2019(
alpha, params, sampling_rate, _eps
):
def B(el):
Expand Down Expand Up @@ -176,19 +176,19 @@ def logAbsB(el):
return (1 / (alpha - 1)) * logsumexp(terms, b=signs)


def culc_first_term_of_general_upper_bound_of_rdp(alpha, sampling_rate):
def calc_first_term_of_general_upper_bound_of_rdp(alpha, sampling_rate):
return ((alpha - 1) * np.log(1 - sampling_rate)) + np.log(
alpha * sampling_rate - sampling_rate + 1
)


def culc_general_upperbound_of_rdp_with_theorem5_of_zhu_2019(
def calc_general_upperbound_of_rdp_with_theorem5_of_zhu_2019(
alpha, params, sampling_rate, _eps
):
terms = []
signs = []

first = culc_first_term_of_general_upper_bound_of_rdp(alpha, sampling_rate)
first = calc_first_term_of_general_upper_bound_of_rdp(alpha, sampling_rate)
terms.append(first)
signs.append(1)

Expand All @@ -213,13 +213,13 @@ def culc_general_upperbound_of_rdp_with_theorem5_of_zhu_2019(
return (1 / (alpha - 1)) * logsumexp(terms, b=signs)


def culc_tightupperbound_lowerbound_of_rdp_with_theorem6and8_of_zhu_2019(
def calc_tightupperbound_lowerbound_of_rdp_with_theorem6and8_of_zhu_2019(
alpha, params, sampling_rate, _eps
):
terms = []
signs = []

first = culc_first_term_of_general_upper_bound_of_rdp(alpha, sampling_rate)
first = calc_first_term_of_general_upper_bound_of_rdp(alpha, sampling_rate)
terms.append(first)
signs.append(1)

Expand All @@ -234,7 +234,7 @@ def culc_tightupperbound_lowerbound_of_rdp_with_theorem6and8_of_zhu_2019(
return (1 / (alpha - 1)) * logsumexp(terms, b=signs)


def culc_tightupperbound_lowerbound_of_rdp_with_theorem6and8_of_zhu_2019_with_tau_estimation(
def calc_tightupperbound_lowerbound_of_rdp_with_theorem6and8_of_zhu_2019_with_tau_estimation(
alpha, params, sampling_rate, _eps, tau=10
):
terms = []
Expand Down
Loading

0 comments on commit 0d7cab8

Please sign in to comment.