项目文件夹

文件
Chen Sirui 3c387988d7 [Example] DCRNN and GaAN (#2858)
* Ready for PR

* refractor code

Co-authored-by: Ubuntu <ubuntu@ip-172-31-45-47.ap-northeast-1.compute.internal>
Co-authored-by: Tianjun Xiao <xiaotj1990327@gmail.com>
2021-04-23 22:33:39 +08:00

34 行
803 B
Python
可执行文件

import dgl
import scipy.sparse as sparse
import numpy as np
import torch.nn as nn
import torch
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):
return (x-self.mean)/self.std
def denormalize(self, x):
return x*self.std + self.mean
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:
return param['lr']