utils.py 2.13 KB
Newer Older
Zihao Ye's avatar
Zihao Ye committed
1
2
import csv
import re
3
4
from collections import OrderedDict

Zihao Ye's avatar
Zihao Ye committed
5
import numpy as np
6
import torch as th
Zihao Ye's avatar
Zihao Ye committed
7
8
import torch.nn as nn
import torch.optim as optim
9

Zihao Ye's avatar
Zihao Ye committed
10
11
12
13

class MetricLogger(object):
    def __init__(self, attr_names, parse_formats, save_path):
        self._attr_format_dict = OrderedDict(zip(attr_names, parse_formats))
14
        self._file = open(save_path, "w")
Zihao Ye's avatar
Zihao Ye committed
15
16
17
18
19
        self._csv = csv.writer(self._file)
        self._csv.writerow(attr_names)
        self._file.flush()

    def log(self, **kwargs):
20
21
22
23
24
25
        self._csv.writerow(
            [
                parse_format % kwargs[attr_name]
                for attr_name, parse_format in self._attr_format_dict.items()
            ]
        )
Zihao Ye's avatar
Zihao Ye committed
26
27
28
29
30
31
32
33
34
35
36
        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):
37
38
39
40
    info_str = (
        "Total Param Number: {}\n".format(torch_total_param_num(net))
        + "Params:\n"
    )
Zihao Ye's avatar
Zihao Ye committed
41
    for k, v in net.named_parameters():
42
        info_str += "\t{}: {}, {}\n".format(k, v.shape, np.prod(v.shape))
Zihao Ye's avatar
Zihao Ye committed
43
44
    info_str += str(net)
    if save_path is not None:
45
        with open(save_path, "w") as f:
Zihao Ye's avatar
Zihao Ye committed
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
            f.write(info_str)
    return info_str


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):
64
        if act == "leaky":
Zihao Ye's avatar
Zihao Ye committed
65
            return nn.LeakyReLU(0.1)
66
        elif act == "relu":
Zihao Ye's avatar
Zihao Ye committed
67
            return nn.ReLU()
68
        elif act == "tanh":
Zihao Ye's avatar
Zihao Ye committed
69
            return nn.Tanh()
70
        elif act == "sigmoid":
Zihao Ye's avatar
Zihao Ye committed
71
            return nn.Sigmoid()
72
        elif act == "softsign":
Zihao Ye's avatar
Zihao Ye committed
73
74
75
76
77
78
79
80
            return nn.Softsign()
        else:
            raise NotImplementedError
    else:
        return act


def get_optimizer(opt):
81
    if opt == "sgd":
Zihao Ye's avatar
Zihao Ye committed
82
        return optim.SGD
83
    elif opt == "adam":
Zihao Ye's avatar
Zihao Ye committed
84
85
86
        return optim.Adam
    else:
        raise NotImplementedError
87
88
89


def to_etype_name(rating):
90
    return str(rating).replace(".", "_")