Download annotations/annotation_102/TAHyper/utils.py from mcy98/RECODE-H: direct link, hf CLI and curl.
- Browser
- Download file 5.71 kB
-
https://huggingface.co/datasets/mcy98/RECODE-H/resolve/main/annotations/annotation_102/TAHyper/utils.py
- Command line
-
hf download hf://datasets/mcy98/RECODE-H/annotations/annotation_102/TAHyper/utils.py
-
curl -L -o utils.py https://huggingface.co/datasets/mcy98/RECODE-H/resolve/main/annotations/annotation_102/TAHyper/utils.py
5.71 kB
| import numpy as np | |
| import scipy.io as sio | |
| import scipy.sparse as sp | |
| import torch | |
| import torch.nn.functional as F | |
| from scipy import interp | |
| torch.manual_seed(12345) | |
| torch.cuda.manual_seed(12345) | |
| def sparse_mx_to_torch_sparse_tensor(sparse_mx,cuda=False): | |
| """Convert a scipy sparse matrix to a torch sparse tensor.""" | |
| sparse_mx = sparse_mx.tocoo().astype(np.float32) | |
| indices = torch.from_numpy( | |
| np.vstack((sparse_mx.row, sparse_mx.col)).astype(np.int64)) | |
| values = torch.from_numpy(sparse_mx.data) | |
| shape = torch.Size(sparse_mx.shape) | |
| sparse_tensor = torch.sparse.FloatTensor(indices, values, shape) | |
| if cuda: | |
| sparse_tensor = sparse_tensor.cuda() | |
| return sparse_tensor | |
| def normalize(mx): | |
| """Row-normalize sparse matrix""" | |
| rowsum = np.array(mx.sum(1)) | |
| r_inv = np.power(rowsum, -1).flatten() | |
| r_inv[np.isinf(r_inv)] = 0. | |
| r_mat_inv = sp.diags(r_inv) | |
| mx = r_mat_inv.dot(mx) | |
| return mx | |
| def load_data(path, name='BlogCatalog',exp_id='0',original_X = False, extra_str=""): | |
| # path_here = path+name+extra_str+'/'+name+exp_id+'.mat' | |
| path_here = path+name+extra_str+'/'+name+exp_id+'.mat' | |
| data = sio.loadmat(path_here) | |
| print("using data is: ", path_here) | |
| A = data['Network'] #csr matrix | |
| if not original_X: | |
| X = data['X_100'] | |
| else: | |
| X = data['Attributes'] | |
| Y1 = data['Y1'] | |
| Y0 = data['Y0'] | |
| T = data['T'] | |
| return X, A, T, Y1, Y0 | |
| def load_data_v2(path, name='BlogCatalog',exp_id='0',original_X = False, extra_str=""): | |
| # path_here = path+name+extra_str+'/'+name+exp_id+'.mat' | |
| path_here = path+name+extra_str+'/'+name+exp_id+'ICnewT'+'.mat' | |
| data = sio.loadmat(path_here) | |
| print("using data is: ", path_here) | |
| A = data['Network'] #csr matrix | |
| if not original_X: | |
| X = data['X_100'] | |
| else: | |
| X = data['Attributes'] | |
| Y1 = data['Y1'] | |
| Y0 = data['Y0'] | |
| T = data['T'] | |
| T_diff = data["T_diff"] | |
| return X, A, T, T_diff, Y1, Y0 | |
| def load_data_v3(path, name='BlogCatalog',exp_id='0',original_X = False, extra_str=""): | |
| data = sio.loadmat(path) | |
| print("using data is: ", path) | |
| A = data['Network'] #csr matrix | |
| if not original_X: | |
| X = data['X_100'] | |
| else: | |
| X = data['Attributes'] | |
| Y1 = data['Y1'] | |
| Y0 = data['Y0'] | |
| T_old = data['T'] | |
| T_new = data['T_new'] | |
| T_diff = data["T_diff"] | |
| return X, A, Y1, Y0, T_old, T_new, T_diff | |
| def load_data_taiji(path, name='BlogCatalog',exp_id='0',original_X = False, extra_str=""): | |
| data = sio.loadmat(path) | |
| A = data['Network'] #csr matrix | |
| # print(path+name+extra_str+'/'+name+exp_id+'.mat') | |
| if not original_X: | |
| X = data['X_100'] | |
| else: | |
| X = data['Attributes'] | |
| Y1 = data['Y1'] | |
| Y0 = data['Y0'] | |
| T = data['T'] | |
| return X, A, T, Y1, Y0 | |
| def wasserstein(x,y,p=0.5,lam=10,its=10,sq=False,backpropT=False,cuda=False): | |
| """return W dist between x and y""" | |
| '''distance matrix M''' | |
| nx = x.shape[0] | |
| ny = y.shape[0] | |
| x = x.squeeze() | |
| y = y.squeeze() | |
| M = pdist(x,y) #distance_matrix(x,y,p=2) | |
| '''estimate lambda and delta''' | |
| M_mean = torch.mean(M) | |
| M_drop = F.dropout(M,10.0/(nx*ny)) | |
| delta = torch.max(M_drop).detach().cpu() | |
| eff_lam = (lam/M_mean).detach() | |
| '''compute new distance matrix''' | |
| Mt = M | |
| row = delta*torch.ones(M[0:1,:].shape) | |
| col = torch.cat([delta*torch.ones(M[:,0:1].shape),torch.zeros((1,1))],0) | |
| if cuda: | |
| row = row.cuda() | |
| col = col.cuda() | |
| Mt = torch.cat([M,row],0) | |
| Mt = torch.cat([Mt,col],1) | |
| '''compute marginal''' | |
| a = torch.cat([p*torch.ones((nx,1))/nx,(1-p)*torch.ones((1,1))],0) | |
| b = torch.cat([(1-p)*torch.ones((ny,1))/ny, p*torch.ones((1,1))],0) | |
| '''compute kernel''' | |
| Mlam = eff_lam * Mt | |
| temp_term = torch.ones(1)*1e-6 | |
| if cuda: | |
| temp_term = temp_term.cuda() | |
| a = a.cuda() | |
| b = b.cuda() | |
| K = torch.exp(-Mlam) + temp_term | |
| U = K * Mt | |
| ainvK = K/a | |
| u = a | |
| for i in range(its): | |
| u = 1.0/(ainvK.matmul(b/torch.t(torch.t(u).matmul(K)))) | |
| if cuda: | |
| u = u.cuda() | |
| v = b/(torch.t(torch.t(u).matmul(K))) | |
| if cuda: | |
| v = v.cuda() | |
| upper_t = u*(torch.t(v)*K).detach() | |
| E = upper_t*Mt | |
| D = 2*torch.sum(E) | |
| if cuda: | |
| D = D.cuda() | |
| return D, Mlam | |
| def pdist(sample_1, sample_2, norm=2, eps=1e-5): | |
| """Compute the matrix of all squared pairwise distances. | |
| Arguments | |
| --------- | |
| sample_1 : torch.Tensor or Variable | |
| The first sample, should be of shape ``(n_1, d)``. | |
| sample_2 : torch.Tensor or Variable | |
| The second sample, should be of shape ``(n_2, d)``. | |
| norm : float | |
| The l_p norm to be used. | |
| Returns | |
| ------- | |
| torch.Tensor or Variable | |
| Matrix of shape (n_1, n_2). The [i, j]-th entry is equal to | |
| ``|| sample_1[i, :] - sample_2[j, :] ||_p``.""" | |
| n_1, n_2 = sample_1.size(0), sample_2.size(0) | |
| norm = float(norm) | |
| if norm == 2.: | |
| norms_1 = torch.sum(sample_1**2, dim=1, keepdim=True) | |
| norms_2 = torch.sum(sample_2**2, dim=1, keepdim=True) | |
| norms = (norms_1.expand(n_1, n_2) + | |
| norms_2.transpose(0, 1).expand(n_1, n_2)) | |
| distances_squared = norms - 2 * sample_1.mm(sample_2.t()) | |
| return torch.sqrt(eps + torch.abs(distances_squared)) | |
| else: | |
| dim = sample_1.size(1) | |
| expanded_1 = sample_1.unsqueeze(1).expand(n_1, n_2, dim) | |
| expanded_2 = sample_2.unsqueeze(0).expand(n_1, n_2, dim) | |
| differences = torch.abs(expanded_1 - expanded_2) ** norm | |
| inner = torch.sum(differences, dim=2, keepdim=False) | |
| return (eps + inner) ** (1. / norm) | |