File size: 1,298 Bytes
0ca2ffc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch_geometric.utils import mask_feature


class MaskNodeFeat(nn.Module):
    def __init__(self, p=0.05, fill_value='mean'):
        super().__init__()
        self.p = p
        self.fill_value = fill_value

    def forward(self, x, edge_index, edge_attr=None, batch=None):
        if self.fill_value == 'zero':
            fill_value = 0.0
        else:
            fill_value = getattr(torch, self.fill_value)(x)
        x = mask_feature(x, p=self.p, fill_value=fill_value)
        return x, edge_index, edge_attr, batch, None
    
    
class NoisyNodeFeat(nn.Module):
    def __init__(self, p=0.05):
        super().__init__()
        self.p = p

    def forward(self, x, edge_index, edge_attr=None, batch=None):
        if x.size(0) > 1:
            std = x.float().std(dim=0)
        else:
            std = torch.ones(x.size(-1))
        x = x + self.p * std * torch.randn(x.size()).to(x.device)
        return x, edge_index, edge_attr, batch, None
    

class NodeFeatShift(nn.Module):
    def __init__(self, p=0.05):
        super().__init__()
        self.p = p

    def forward(self, x, edge_index, edge_attr=None, batch=None):
        x = x + self.p * x.std()
        return x, edge_index, edge_attr, batch, None