-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathutil.py
More file actions
192 lines (144 loc) · 5.22 KB
/
util.py
File metadata and controls
192 lines (144 loc) · 5.22 KB
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 csv
import json
from collections import OrderedDict
import torch
import numpy as np
import logging
import sys
import os
from datetime import datetime
from pytz import timezone, utc
from torch import optim, nn
from torch.utils.tensorboard import SummaryWriter
import shutil
def custom_time(*args):
utc_dt = utc.localize(datetime.utcnow())
my_tz = timezone("Asia/Shanghai")
converted = utc_dt.astimezone(my_tz)
return converted.timetuple()
def change_optimizer_device(optimizer, device, dtype):
for state in optimizer.state.values():
for k, v in state.items():
if isinstance(v, torch.Tensor):
state[k] = v.to(device).type(dtype)
def change_triplet_data_type(triplet: np.ndarray):
user = triplet[:, 0].astype(np.int64)
item = triplet[:, 1].astype(np.int64)
rating = triplet[:, 2].astype(np.float32)
return [user, item, rating]
def change_tensor_device(device, *tensors):
result = [x.to(device) for x in tensors]
return result
def np_to_pt_tensor(device, *arrays):
return [torch.from_numpy(x).to(device) for x in arrays]
def create_dirs(file_path):
dir_path = os.path.dirname(file_path)
if not os.path.exists(dir_path):
os.makedirs(dir_path)
def get_logger(name, log_file_path=None, mode='w'):
logger = logging.getLogger(name)
logger.setLevel(level=logging.DEBUG)
# StreamHandler
stream_handler = logging.StreamHandler(sys.stdout)
stream_handler.setLevel(level=logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(message)s', datefmt="%Y-%m-%d %H:%M:%S")
formatter.converter = custom_time
# logging.Formatter.converter = customTime
stream_handler.setFormatter(formatter)
logger.addHandler(stream_handler)
# FileHandler
if log_file_path:
dir_path = os.path.dirname(log_file_path)
if not os.path.exists(dir_path):
os.makedirs(dir_path)
file_handler = logging.FileHandler(log_file_path, mode=mode)
file_handler.setLevel(level=logging.INFO)
# formatter = logging.Formatter('%(asctime)s - %(name)s - %(message)s')
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
return logger
def get_tensorboard_writer(log_dir, model='w'):
if model == 'w' and os.path.exists(log_dir):
shutil.rmtree(log_dir, ignore_errors=True)
writer = SummaryWriter(log_dir)
return writer
def get_args_str(args):
attr = getattr(args, '__dict__')
attr = dict(attr)
attr = {k: v for k, v in attr.items() if not k.startswith('_')}
attr = json.dumps(attr, indent=4)
return attr
def args_to_dict(args):
attr = getattr(args, '__dict__')
attr = dict(attr)
attr = {k: v for k, v in attr.items() if not k.startswith('_')}
return attr
def get_activation(act):
"""Get the activation based on the act string
Parameters
----------
act: str or callable function
Returns
-------
ret: callable function
"""
if act is None:
return lambda x: x
if isinstance(act, str):
if act == 'leaky':
return nn.LeakyReLU(0.1)
elif act == 'relu':
return nn.ReLU()
elif act == 'tanh':
return nn.Tanh()
elif act == 'sigmoid':
return nn.Sigmoid()
elif act == 'softsign':
return nn.Softsign()
else:
raise NotImplementedError
else:
return act
def get_optimizer(opt):
if opt == 'SGD':
return optim.SGD
elif opt == 'Adam':
return optim.Adam
elif opt == 'AdamW':
return optim.AdamW
else:
raise NotImplementedError
def to_etype_name(rating):
return str(rating).replace('.', '_')
class MetricLogger(object):
def __init__(self, attr_names, parse_formats, save_path):
self._attr_format_dict = OrderedDict(zip(attr_names, parse_formats))
self._file = open(save_path, 'w')
self._csv = csv.writer(self._file)
self._csv.writerow(attr_names)
self._file.flush()
def log(self, **kwargs):
self._csv.writerow([parse_format % kwargs[attr_name]
for attr_name, parse_format in self._attr_format_dict.items()])
self._file.flush()
def close(self):
self._file.close()
def torch_total_param_num(net):
return sum([np.prod(p.shape) for p in net.parameters()])
def torch_net_info(net, save_path=None):
info_str = 'Total Param Number: {}\n'.format(torch_total_param_num(net)) +\
'Params:\n'
for k, v in net.named_parameters():
info_str += '\t{}: {}, {}\n'.format(k, v.shape, np.prod(v.shape))
info_str += str(net)
if save_path is not None:
with open(save_path, 'w') as f:
f.write(info_str)
return info_str
def coo_matrix_to_sparse_tensor(m):
values = m.data
indices = np.vstack((m.row, m.col))
i = torch.LongTensor(indices)
v = torch.FloatTensor(values)
shape = m.shape
return torch.sparse_coo_tensor(i, v, torch.Size(shape), dtype=torch.float32)