utils.py 809 Bytes
Newer Older
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
1
import dgl
Chen Sirui's avatar
Chen Sirui committed
2
import numpy as np
3
import scipy.sparse as sparse
Chen Sirui's avatar
Chen Sirui committed
4
import torch
5
6
import torch.nn as nn

Chen Sirui's avatar
Chen Sirui committed
7
8
9
10
11
12
13
14

class NormalizationLayer(nn.Module):
    def __init__(self, mean, std):
        self.mean = mean
        self.std = std

    # Here we shall expect mean and std be scaler
    def normalize(self, x):
15
        return (x - self.mean) / self.std
Chen Sirui's avatar
Chen Sirui committed
16
17

    def denormalize(self, x):
18
        return x * self.std + self.mean
Chen Sirui's avatar
Chen Sirui committed
19
20
21
22
23
24
25
26
27
28
29
30
31
32


def masked_mae_loss(y_pred, y_true):
    mask = (y_true != 0).float()
    mask /= mask.mean()
    loss = torch.abs(y_pred - y_true)
    loss = loss * mask
    # trick for nans: https://discuss.pytorch.org/t/how-to-set-nan-in-tensor-to-0/3918/3
    loss[loss != loss] = 0
    return loss.mean()


def get_learning_rate(optimizer):
    for param in optimizer.param_groups:
33
        return param["lr"]