Created
July 19, 2020 15:28
-
-
Save codeKgu/54f7e779a76d45a27d8f902100dfac7a to your computer and use it in GitHub Desktop.
snippets for TextGCN blog
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scipy.sparse as sp | |
import numpy as np | |
def init_node_feats(self, type, device): | |
if type == 'one_hot_init': | |
num_nodes = self.graph.shape[0] | |
identity = sp.identity(num_nodes) | |
ind0, ind1, values = sp.find(identity) | |
inds = np.stack((ind0, ind1), axis=0) | |
self.node_feats = torch.sparse_coo_tensor(inds, values, device=device, dtype=torch.float) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def forward(self, x, edge_index, edge_weight=None): | |
""" | |
for the PyTorch Geometric GCN implementation | |
see https://pytorch-geometric.readthedocs.io/en/latest/_modules/torch_geometric/nn/conv/gcn_conv.html#GCNConv | |
""" | |
if x.is_sparse: | |
x = torch.sparse.mm(x, self.weight) | |
else: | |
x = torch.matmul(x, self.weight) | |
if not self.cached or self.cached_result is None: | |
edge_index, norm = GCNConv.norm(edge_index, x.size(0), edge_weight, | |
self.improved, x.dtype) | |
self.cached_result = edge_index, norm | |
edge_index, norm = self.cached_result | |
return self.propagate(edge_index, x=x, norm=norm) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
string = re.sub(r"http[s]?\:\/\/.[a-zA-Z0-9\.\/\_?=%&#\-\+!]+", " ", string) | |
string = re.sub(r"[^A-Za-z0-9()_+,!?:\'\`]", " ", string) # replace all non alpha numeric characters |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment