-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunctions.py
55 lines (47 loc) · 1.5 KB
/
functions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import logging
import os
import random
import numpy as np
import torch
import torch.nn as nn
def seed_all(seed=42):
random.seed(seed)
os.environ['PYTHONHASHSEED'] = str(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed) # if you are using multi-GPU.
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic = True
def fwrite(log_file, s):
with open(log_file, 'a', buffering=1) as fp:
fp.write(s)
def get_logger(filename, verbosity=1, name=None):
level_dict = {0: logging.DEBUG, 1: logging.INFO, 2: logging.WARNING}
formatter = logging.Formatter(
"[%(asctime)s][%(filename)s][line:%(lineno)d][%(levelname)s] %(message)s"
)
logger = logging.getLogger(name)
logger.setLevel(level_dict[verbosity])
fh = logging.FileHandler(filename, "w")
fh.setFormatter(formatter)
logger.addHandler(fh)
sh = logging.StreamHandler()
sh.setFormatter(formatter)
logger.addHandler(sh)
return logger
def BPTT_attack(model, image, T):
model.set_simulation_time(T, mode='bptt')
output = model(image).mean(0)
model.set_simulation_time(T)
return output
def BPTR_attack(model, image, T):
model.set_simulation_time(T, mode='bptr')
output = model(image).mean(0)
model.set_simulation_time(T)
return output
def Act_attack(model, image, T):
model.set_simulation_time(0)
output = model(image)
model.set_simulation_time(T)
return output