forked from DingXiaoH/GSM-SGD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
misc.py
192 lines (159 loc) · 5.59 KB
/
misc.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import math
import os
from typing import Tuple, List, Dict
import torch
import sys
import json
import h5py
import numpy as np
import time
def cur_time():
return time.strftime('%Y,%b,%d,%X')
def log_important(message, log_file):
print(message, cur_time())
with open(log_file, 'a') as f:
print(message, cur_time(), file=f)
def extract_deps_from_weights_file(file_path):
weight_dic = read_hdf5(file_path)
if 'deps' in weight_dic:
return weight_dic['deps']
else:
return None
def representsInt(s):
try:
int(s)
return True
except ValueError:
return False
def read_hdf5(file_path):
result = {}
with h5py.File(file_path, 'r') as f:
for k in f.keys():
value = np.asarray(f[k])
if representsInt(k):
result[int(k)] = value
else:
result[str(k).replace('+','/')] = value
print('read {} arrays from {}'.format(len(result), file_path))
f.close()
return result
def save_hdf5(numpy_dict, file_path):
with h5py.File(file_path, 'w') as f:
for k,v in numpy_dict.items():
f.create_dataset(str(k).replace('/','+'), data=v)
print('saved {} arrays to {}'.format(len(numpy_dict), file_path))
f.close()
def start_exp():
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--try_arg", type=str, default='')
args = parser.parse_args()
try_arg = args.try_arg
print('the try_arg is ', try_arg)
print('we have {} torch devices'.format(torch.cuda.device_count()),
'the allocated GPU memory is {}'.format(torch.cuda.memory_allocated()))
return try_arg
def torch_accuracy(output, target, topk=(1,)) -> List[torch.Tensor]:
'''
param output, target: should be torch Variable
'''
# assert isinstance(output, torch.cuda.Tensor), 'expecting Torch Tensor'
# assert isinstance(target, torch.Tensor), 'expecting Torch Tensor'
# print(type(output))
topn = max(topk)
batch_size = output.size(0)
_, pred = output.topk(topn, 1, True, True)
pred = pred.t()
is_correct = pred.eq(target.view(1, -1).expand_as(pred))
ans = []
for i in topk:
is_correct_i = is_correct[:i].view(-1).float().sum(0, keepdim=True)
ans.append(is_correct_i.mul_(100.0 / batch_size))
return ans
class AvgMeter(object):
'''
Computing mean
'''
name = 'No name'
def __init__(self, name='No name', fmt = ':.2f'):
self.name = name
self.fmt = fmt
self.reset()
def reset(self):
self.sum = 0
self.mean = 0
self.num = 0
self.now = 0
def update(self, mean_var, count=1):
if math.isnan(mean_var):
mean_var = 1e6
print('Avgmeter getting Nan!')
self.now = mean_var
self.num += count
self.sum += mean_var * count
self.mean = float(self.sum) / self.num
def __str__(self):
print_str = self.name + '-{' + self.fmt + '}'
return print_str.format(self.mean)
def save_args(args, save_dir = None):
if save_dir == None:
param_path = os.path.join(args.resume, "params.json")
else:
param_path = os.path.join(save_dir, 'params.json')
#logger.info("[*] MODEL dir: %s" % args.resume)
#logger.info("[*] PARAM path: %s" % param_path)
with open(param_path, 'w') as fp:
json.dump(args.__dict__, fp, indent=4, sort_keys=True)
def mkdir(path):
if not os.path.exists(path):
print('creating dir {}'.format(path))
os.mkdir(path)
# def save_checkpoint(cur_iters, net, optimizer, lr_scheduler, file_name):
# checkpoint = {'cur_iters': cur_iters,
# 'state_dict': net.state_dict(),
# 'optimizer_state_dict': optimizer.state_dict(),
# 'lr_scheduler_state_dict':lr_scheduler.state_dict()}
# if os.path.exists(file_name):
# print('Overwriting {}'.format(file_name))
# torch.save(checkpoint, file_name)
# link_name = os.path.join('/', *file_name.split(os.path.sep)[:-1], 'last.checkpoint')
# #print(link_name)
# make_symlink(source = file_name, link_name=link_name)
def load_checkpoint(file_name, net = None, optimizer = None, lr_scheduler = None):
if os.path.isfile(file_name):
print("=> loading checkpoint '{}'".format(file_name))
check_point = torch.load(file_name)
if net is not None:
print('Loading network state dict')
net.load_state_dict(check_point['state_dict'])
if optimizer is not None:
print('Loading optimizer state dict')
optimizer.load_state_dict(check_point['optimizer_state_dict'])
if lr_scheduler is not None:
print('Loading lr_scheduler state dict')
lr_scheduler.load_state_dict(check_point['lr_scheduler_state_dict'])
return check_point['cur_iters']
else:
print("=> no checkpoint found at '{}'".format(file_name))
def make_symlink(source, link_name):
'''
Note: overwriting enabled!
'''
if os.path.exists(link_name):
#print("Link name already exist! Removing '{}' and overwriting".format(link_name))
os.remove(link_name)
if os.path.exists(source):
os.symlink(source, link_name)
return
else:
print('Source path not exists')
#print('SymLink Wrong!')
def add_path(path):
if path not in sys.path:
print('Adding {}'.format(path))
sys.path.append(path)
def format_metric_dict_to_line(metric_dict):
msg = ''
for key, value in metric_dict.items():
msg += '{}={:.5f},'.format(key, value)
return msg