-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathutils.py
More file actions
31 lines (26 loc) · 985 Bytes
/
utils.py
File metadata and controls
31 lines (26 loc) · 985 Bytes
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
import torch
import os
import shutil
def distance(x_adv, x, norm='l2'):
diff = (x_adv - x).view(x.size(0), -1)
if norm == 'l2':
out = torch.sqrt(torch.sum(diff * diff)).item()
return out
elif norm == 'linf':
out = torch.sum(torch.max(torch.abs(diff), 1)[0]).item()
return out
def pick_gpu_lowest_memory():
import gpustat
stats = gpustat.GPUStatCollection.new_query()
ids = map(lambda gpu: int(gpu.entry['index']), stats)
ratios = map(lambda gpu: float(gpu.memory_used)/float(gpu.memory_total), stats)
bestGPU = min(zip(ids, ratios), key=lambda x: x[1])[0]
return bestGPU
def create_exp_dir(path, scripts_to_save=None):
if not os.path.exists(path):
os.makedirs(path)
print('Experiment dir : {}'.format(path))
if scripts_to_save is not None:
for script in scripts_to_save:
dst_file = os.path.join(path, os.path.basename(script))
shutil.copyfile(script, dst_file)